zoukankan      html  css  js  c++  java
  • django性能分析工具之silk

    在开发过程中,有很多时候需要了解程序性能瓶颈,比如程序执行时间、网络耗时、数据库连接时间等;
    那接下来,django silk就派上用场了!

    安装

    https://github.com/jazzband/django-silk

    pip install django-silk
    # or 
    pip install https://github.com/jazzband/silk/archive/3.0.1.tar.gz

    settings.py

    MIDDLEWARE = [
        ...
        'silk.middleware.SilkyMiddleware',
        ...
    ]
    
    INSTALLED_APPS = (
        ...
        'silk'
    )

    urls.py

    urlpatterns += [url(r'^silk/', include('silk.urls', namespace='silk'))]

    迁移

    python manage.py makemigrations
    
    python manage.py migrate
    
    python manage.py collectstatic

    性能报告

    启动项目后,访问:项目网址/silk/,可查看性能报告,访问项目页面后,查看报告;
    摘要报告:

    请求列表:

    请求详细:

    请求的sql部分:

    查看某一条sql执行情况:

    需要程序详细的跟踪记录和执行情况,需要添加装饰器,以联系上下文来参考:
    settings设置:

    # 使用Python的内置cProfile分析器
    SILKY_PYTHON_PROFILER = True
    
    # 生成.prof文件,silk产生的程序跟踪记录,详细记录来执行来哪个文件,哪一行,用了多少时间等信息
    SILKY_PYTHON_PROFILER_BINARY = True
    
    # .prof文件保存路径
    SILKY_PYTHON_PROFILER_RESULT_PATH = '/data/profiles/'

    函数加上装饰器

    from silk.profiling.profiler import silk_profile
    
    @silk_profile(name='user login') # name在Profiling页面区分不同请求名称
    def test(request):
        pass

    访问程序后,查看
    分析情况:

    使用标准库PSTATS查看.PROF文件

    profiles文件也可以用python标准库中的pstats查看,大致就是长这样:

  • 相关阅读:
    CentOS7 离线安装fastDFS、jdk1.8、mysql5.7、nginx、libreOffice
    java生成随机验证码
    Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
    js获取本地IP
    CentOS6.5下Oracle11.2安装
    IE浏览器不兼容indexOf问题
    有关LocalAlloc,LocalReAlloc,LocalFree,GlobalAlloc,GlobalReAlloc,GlobalFree的模糊点总结
    03UseTls
    03EventDemo
    lockFunctionDemo
  • 原文地址:https://www.cnblogs.com/xingxia/p/django_silk.html
Copyright © 2011-2022 走看看