Django 的性能问题

发布于 2025-01-07 11:28:22 字数 2463 浏览 3 评论 0原文

我正在尝试找出 Django 遇到的一些性能问题。从我点击刷新到浏览器得到响应似乎有 600-800 毫秒的延迟。

我设置了一个简单的视图和配置文件中间件,这是视图和结果:

视图函数:

def test(request):
    return HttpResponse("It works")

配置文件结果(我使用 http://www.djangosnippets.org/snippets/186/):

9 function calls in 0.000 CPU seconds

Ordered by: internal time, call count

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    0.000    0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/http/__init__.py:487(__init__)
    1    0.000    0.000    0.000    0.000 /home/mysite/mysite/mysite/map/views.py:19(test)
    1    0.000    0.000    0.000    0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/http/__init__.py:532(__setitem__)
    3    0.000    0.000    0.000    0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/http/__init__.py:517(_convert_to_ascii)
    2    0.000    0.000    0.000    0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/utils/functional.py:274(__getattr__)
    1    0.000    0.000    0.000    0.000 /usr/lib/python2.6/Cookie.py:573(__init__)
    0    0.000             0.000          profile:0(profiler)


---- By file ----

 tottime
0.0%   0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/utils/functional.py
0.0%   0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/http/__init__.py
0.0%   0.000 /usr/lib/python2.6/Cookie.py
0.0%   0.000 /home/mysite/mysite/mysite/map/views.py

---- By group ---

  tottime
0.0%   0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/utils
0.0%   0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/http
0.0%   0.000 /usr/lib/python2.6
0.0%   0.000 /home/mysite/mysite/mysite/map

因此,分析器不会返回任何数字,但 Chrome 报告有 647 毫秒的延迟从请求资源到实际获得任何响应。我对服务器的 ping 时间约为 50 毫秒。有什么想法可以更好地进行分析,以便我可以看到 Django 中的哪个位置导致了速度下降?

我的 WSGI 配置。我将 Cherokee 与 uwsgi 一起使用。

import os
import sys

path = '/home/mysite/mysite/mysite/'
if path not in sys.path:
    sys.path.append(path)



os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

我在不同的计算机以及不同的网络上看到了这种延迟(20 毫秒内)。当我只使用manage.py runserver 时,我也看到了它。

I'm trying to track down some performance issues I have had with Django. There seems to be a 600-800 ms delay from the time I click refresh to the time the browser gets a response.

I set up a simple view and the profile middleware and this is the view and results:

the view function:

def test(request):
    return HttpResponse("It works")

The profile results( i used http://www.djangosnippets.org/snippets/186/):

9 function calls in 0.000 CPU seconds

Ordered by: internal time, call count

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    0.000    0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/http/__init__.py:487(__init__)
    1    0.000    0.000    0.000    0.000 /home/mysite/mysite/mysite/map/views.py:19(test)
    1    0.000    0.000    0.000    0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/http/__init__.py:532(__setitem__)
    3    0.000    0.000    0.000    0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/http/__init__.py:517(_convert_to_ascii)
    2    0.000    0.000    0.000    0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/utils/functional.py:274(__getattr__)
    1    0.000    0.000    0.000    0.000 /usr/lib/python2.6/Cookie.py:573(__init__)
    0    0.000             0.000          profile:0(profiler)


---- By file ----

 tottime
0.0%   0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/utils/functional.py
0.0%   0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/http/__init__.py
0.0%   0.000 /usr/lib/python2.6/Cookie.py
0.0%   0.000 /home/mysite/mysite/mysite/map/views.py

---- By group ---

  tottime
0.0%   0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/utils
0.0%   0.000 /usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/http
0.0%   0.000 /usr/lib/python2.6
0.0%   0.000 /home/mysite/mysite/mysite/map

So with that, the profiler isn't returning any numbers, yet Chrome reports a 647 ms delay from requesting the resource to actually getting any response. My ping time to the server is about 50 ms. Any ideas how I can get better profiling so I can see where in Django is causing this slowdown?

My WSGI config. I'm using Cherokee with uwsgi.

import os
import sys

path = '/home/mysite/mysite/mysite/'
if path not in sys.path:
    sys.path.append(path)



os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

I see this delay(within 20 ms) on different computers as well as different networks. I'm also seeing it when I just use manage.py runserver.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆头 2025-01-14 11:28:22

由于多种原因,Django 可能会变慢。主要原因之一可能是不合适或没有使用 Memcached。浏览文档以了解拥有缓存的优缺点。您还可以找到这篇文章有用。

Django might be slowing down due to several reasons. One of the main reason might be inappropriate or no use of Memcached. Go through the docs to get the idea about the pros and cons of having a cache. You may also find this article useful.

满天都是小星星 2025-01-14 11:28:22

看起来你的 python 解释器会为每个请求重新加载。我的猜测:延迟发生在分析之前。如果您使用 mod_wsgi,您的最大请求设置是多少?你的 wsgi 配置是什么样的?

或者,您使用了大量 JavaScript,而延迟出现在您的浏览器中。

Looks like you python interpreter gets reloaded for every request. My guess: The delay happens before the profiling. If you use mod_wsgi, what is your maximum-requests setting? What does your wsgi configuration look like?

Or, you use a lot of JavaScript and the delay is in your browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文