简体   繁体   English

如何在执行时间方面描述django应用程序?

[英]How to profile django application with respect to execution time?

My Django application is insanely slow, I want to figure out what is taking time : 我的Django应用程序非常慢,我想知道花时间:

I tried Django-debug-toolbar but was unable to find a panel that can give me the break-up of the load time. 我尝试了Django-debug-toolbar但是找不到可以让我分解加载时间的面板。

My requirements: 我的要求:

  • A stack-trace type output with time of execution for each module called to render the page. 堆栈跟踪类型输出,每个模块的执行时间被调用以呈现页面。
  • I want to realize what part of the whole page rendering process is taking the time ? 我想知道整个页面渲染过程的哪一部分花时间?
  • Also, what part is consuming how much CPU [ MOST IMPORTANT ] ? 另外,哪些部分消耗了多少CPU [最重要]?

Can django-debug-toolbar do that ? django-debug-toolbar做到吗? [ What panel ? [什么小组? ] ]

Any other django-app that can do that ? 任何其他可以做到这一点的django-app?

django-debug-toolbar 2.0 django-debug-toolbar 2.0

By default, django-debug-toolbar 2.0 includes 'debug_toolbar.panels.profiling.ProfilingPanel' in the settings DEBUG_TOOLBAR_PANELS . 默认情况下, django-debug-toolbar 2.0在设置DEBUG_TOOLBAR_PANELS包含'debug_toolbar.panels.profiling.ProfilingPanel' You can view this profiling information by ticking the "Profiling" checkbox in the toolbar and refreshing the page. 您可以通过勾选工具栏中的“性能分析”复选框并刷新页面来查看此性能分析信息。

Old versions of django-debug-toolbar : 旧版本的django-debug-toolbar

You can try the profiling panel of the django-debug-toolbar (make sure you use the application's latest version from github ). 您可以尝试使用django-debug-toolbar的profiling面板(确保使用github中的应用程序的最新版本)。 Enable the panel like so in your settings.py: 在settings.py中启用这样的面板:

DEBUG_TOOLBAR_PANELS = (
  'debug_toolbar.panels.version.VersionDebugPanel',
  'debug_toolbar.panels.timer.TimerDebugPanel',
  'debug_toolbar.panels.profiling.ProfilingDebugPanel',
)

This existence of this panel is not documented on the readme of django-debug-toolbar; django-debug-toolbar的自述文件中没有记录这个面板的存在。 that's why I answer here in the first place. 这就是为什么我首先回答这个问题。

Finally figured out a way to profile my django webapp : 终于找到了一种方法来描述我的django webapp:

Following 2 django snippets provide middleware that profile the whole flow and outputs if request has prof in GET keys : 以下的2个片段的django提供middleware该配置文件的整个流,并输出请求是否已profGET keys

Plain and simple profiling - Saved my day ! 简单简单的描述 - 节省了我的一天!

I would recommend writing some integration tests instead, or at least using the built in testing client to automate requests and put lots of debugging statements in the views 我建议改为编写一些集成测试,或者至少使用内置的测试客户端来自动化请求并在视图中放入大量的调试语句

Django has a built in testing client: Django有一个内置的测试客户端:

from django.test.client import Client
c = Client()
response = c.post('/slow_url/')

And then in your view: 然后在你看来:

def slow_url(request):
    start = time.time()
    print 'Started db query'
    result = SomeComplexModel.objects.all()
    print 'Finished db query, took ', time.time() - start
    return render('some_complex_template.html', {'result': result})  

Automating the process of making requests and being able to replicate them again and again while you make small changes is how you will improve your code. 在进行小的更改时,自动执行请求并能够一次又一次地复制它们的过程就是如何改进代码。 CPU time can be worked out if you measure the time it takes to run each function. 如果您测量运行每个功能所需的时间,则可以计算出CPU时间。 It won't take you long to hone in on the part that is actually chewing up resources. 在实际咀嚼资源的部分,不需要花很长时间磨练。

It's not profiling , but i generally simply use a view to calculate the execution time , it works also for views that need user login, it displays execution time in a simple page 它不是分析,但我通常只是使用视图来计算执行时间,它也适用于需要用户登录的视图,它在一个简单的页面中显示执行时间

 def test(request):
     from django.test.client import Client
     import time

     c = Client()

     #client login if needed
     response = c.post('/login/', {'username': 'admin', 'password': 'password'})

     start = time.time()
     response = c.get('/pagetotest/')

     #print response
     #print 'Finished, time: ', time.time() - start # output to console
     end=time.time() - start
     return render(request,'loadingtime.html',{'time':end})

it's a good start i think , hope it will help someone 我认为这是一个好的开始,希望它会帮助某人

django-silk can help. django-silk可以提供帮助。

  • Install it with: pip install django-silk 安装方式: pip install django-silk
  • Add it in settings.py : settings.py添加它:
MIDDLEWARE = [
    ...
    'silk.middleware.SilkyMiddleware',
    ...
]

INSTALLED_APPS = (
    ...
    'silk'
)
  • Add the route in urls.py : urls.py添加路由:
urlpatterns += [url(r'^silk/', include('silk.urls', namespace='silk'))]
  • Finally, create the adequate tables in the database with: 最后,在数据库中创建适当的表:
python manage.py makemigrations
python manage.py migrate

You can then browse in your internet browser to /silk to find the page requested there. 然后,您可以在您的互联网浏览器中浏览/silk以查找其中所请求的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM