kagxin / blog

个人博客:技术、随笔、生活
https://github.com/kagxin/blog/issues
7 stars 0 forks source link

drf 序列化类性能上的一些建议 #26

Open kagxin opened 5 years ago

kagxin commented 5 years ago

序列化类的性能排序

性能排序

serializers.ModelSerializer 小于 ReadOnlyModelSerializer meta中指定read_only字段 小于 serializers.Serializer 小于 ReadOnlySerializer field中增加readonly=True参数

ref: https://hakibenita.com/django-rest-framework-slow

https://github.com/encode/django-rest-framework/commit/c2293e9f251b1f215825186a7bcbf5a006df0cb0 https://github.com/django/django/commit/a2c31e12da272acc76f3a3a0157fae9a7f6477ac 建议看英文原版文件中有pr地址 https://mp.weixin.qq.com/s/rw54cNzfvmBOTVaXrGqkgA

kagxin commented 5 years ago

python 序列化工具性能排名

Library Many Objects (seconds) One Object (seconds) Relative
Custom 0.0256822 0.013052 1
lima 0.0383852 0.0197146 1.49996
serpy 0.104605 0.0706699 4.52507
Strainer 0.114318 0.0885544 5.23755
Toasted Marshmallow 0.292227 0.135766 11.0495
Lollipop 1.23894 0.419955 42.8277
Marshmallow 2.13818 0.749287 74.5457
kim 2.46978 1.32616 97.9997
Django REST Framework 2.53841 1.75292 110.789

ref: https://github.com/voidfiles/python-serialization-benchmark https://voidfiles.github.io/python-serialization-benchmark/

kagxin commented 4 years ago

django log SQL信息

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.db.backends': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'DEBUG',
        },
    }
}