![](/img/trans.png)
[英]Python profile: How to get the time for each individual execution (no aggregation)
[英]How to profile django application with respect to execution time?
我的Django应用程序非常慢,我想知道花时间:
我尝试了Django-debug-toolbar
但是找不到可以让我分解加载时间的面板。
我的要求:
django-debug-toolbar
做到吗? [什么小组? ]
任何其他可以做到这一点的django-app?
django-debug-toolbar
2.0 默认情况下, django-debug-toolbar
2.0在设置DEBUG_TOOLBAR_PANELS
包含'debug_toolbar.panels.profiling.ProfilingPanel'
。 您可以通过勾选工具栏中的“性能分析”复选框并刷新页面来查看此性能分析信息。
django-debug-toolbar
: 您可以尝试使用django-debug-toolbar的profiling面板(确保使用github中的应用程序的最新版本)。 在settings.py中启用这样的面板:
DEBUG_TOOLBAR_PANELS = (
'debug_toolbar.panels.version.VersionDebugPanel',
'debug_toolbar.panels.timer.TimerDebugPanel',
'debug_toolbar.panels.profiling.ProfilingDebugPanel',
)
django-debug-toolbar的自述文件中没有记录这个面板的存在。 这就是为什么我首先回答这个问题。
终于找到了一种方法来描述我的django webapp:
以下的2个片段的django提供middleware
该配置文件的整个流,并输出请求是否已prof
在GET keys
:
http://djangosnippets.org/snippets/727/ [使用cProfile]
http://djangosnippets.org/snippets/186/ [使用hotshot]
简单简单的描述 - 节省了我的一天!
我建议改为编写一些集成测试,或者至少使用内置的测试客户端来自动化请求并在视图中放入大量的调试语句
Django有一个内置的测试客户端:
from django.test.client import Client
c = Client()
response = c.post('/slow_url/')
然后在你看来:
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})
在进行小的更改时,自动执行请求并能够一次又一次地复制它们的过程就是如何改进代码。 如果您测量运行每个功能所需的时间,则可以计算出CPU时间。 在实际咀嚼资源的部分,不需要花很长时间磨练。
它不是分析,但我通常只是使用视图来计算执行时间,它也适用于需要用户登录的视图,它在一个简单的页面中显示执行时间
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})
我认为这是一个好的开始,希望它会帮助某人
django-silk可以提供帮助。
pip install django-silk
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
然后,您可以在您的互联网浏览器中浏览/silk
以查找其中所请求的页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.