有没有办法分析我的 Django 应用程序?

发布于 2024-08-23 15:05:21 字数 83 浏览 5 评论 0原文

我的 Django 应用程序的生产速度变得非常慢。可能是由于一些复杂或未索引的查询。

有没有类似 django 的方式来分析我的应用程序?

My Django application has become painfully slow on the production. Probably it is due to some complex or unindexed queries.

Is there any django-ish way to profile my application?

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

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

发布评论

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

评论(9

陌路黄昏 2024-08-30 15:05:21

尝试使用 Django 调试工具栏。它将显示每个页面上执行了哪些查询以及它们花费了多少时间。这是一个非常有用、强大且易于使用的工具。

另外,请阅读文档中数据库访问优化中有关 Django 性能的建议。

以及 Django 性能技巧
雅各布·卡普兰-莫斯。

Try the Django Debug Toolbar. It will show you what queries are executed on each page and how much time they take. It's a really useful, powerful and easy to use tool.

Also, read recommendations about Django performance in Database access optimization from the documentation.

And Django performance tips by
Jacob Kaplan-Moss.

旧故 2024-08-30 15:05:21

只需在谷歌上输入“django-profiling”,您就会得到这些链接(以及更多):

http:// /code.djangoproject.com/wiki/ProfilingDjango

http://code.google .com/p/django-profiling/

http://www.rkblog.rk.edu.pl/w/p/django-profiling-hotshot-and-kcachegrind/

就我个人而言,我正在使用中间件方法 - 即每个用户都可以切换存储在会话中的“分析”标志,如果我的分析中间件注意到已设置标志,它将使用 Python 的 hotshot 模块如下:

def process_view(self, request, view_func, view_args, view_kwargs):

     # setup things here, along with: settings.DEBUG=True 
     # to get a SQL dump in connection.queries

     profiler = hotshot.Profile(fname)
     response = profiler.runcall(view_func, request, *view_args, **view_kwargs)
     profiler.close()

     # process results

     return response

编辑:用于分析 SQL 查询 http:// Konstantin 提到的 /github.com/robhudson/django-debug-toolbar 是一件好事 - 但如果你的查询真的很慢(可能因为有成百上千个查询),那么你会疯狂地等待加载到浏览器中之前需要花费大量时间 - 然后由于速度缓慢而难以浏览。此外,django-debug-toolbar 在设计上无法提供对 AJAX 请求内部结构的有用见解。

EDIT2: django-extensions 内置了一个很棒的分析命令:

https://github.com/django-extensions/django-extensions/blob /master/docs/runprofileserver.rst

只要这样做,瞧:

$ mkdir /tmp/my-profile-data
$ ./manage.py runprofileserver --kcachegrind --prof-path=/tmp/my-profile-data

Just type "django-profiling" on google, you'll get these links (and more):

http://code.djangoproject.com/wiki/ProfilingDjango

http://code.google.com/p/django-profiling/

http://www.rkblog.rk.edu.pl/w/p/django-profiling-hotshot-and-kcachegrind/

Personally I'm using the middleware approach - i.e. each user can toggle a "profiling" flag stored in a session, and if my profiling middleware notices that a flag has been set, it uses Python's hotshot module like this:

def process_view(self, request, view_func, view_args, view_kwargs):

     # setup things here, along with: settings.DEBUG=True 
     # to get a SQL dump in connection.queries

     profiler = hotshot.Profile(fname)
     response = profiler.runcall(view_func, request, *view_args, **view_kwargs)
     profiler.close()

     # process results

     return response

EDIT: For profiling SQL queries http://github.com/robhudson/django-debug-toolbar mentioned by Konstantin is a nice thing - but if your queries are really slow (probably because there are hundreds or thousands of them), then you'll be waiting insane amount of time until it gets loaded into a browser - and then it'll be hard to browse due to slowness. Also, django-debug-toolbar is by design unable to give useful insight into the internals of AJAX requests.

EDIT2: django-extensions has a great profiling command built in:

https://github.com/django-extensions/django-extensions/blob/master/docs/runprofileserver.rst

Just do this and voila:

$ mkdir /tmp/my-profile-data
$ ./manage.py runprofileserver --kcachegrind --prof-path=/tmp/my-profile-data
骑趴 2024-08-30 15:05:21

无耻的插件,但我最近做了 https://github.com/django-silk/silk为此目的。它有点类似于 django 工具栏,但具有历史记录、代码分析和对所有内容的更细粒度的控制。

Shameless plug here, but I recently made https://github.com/django-silk/silk for this purpose. It's somewhat similar to django toolbar but with history, code profiling and more fine grained control over everything.

陌上芳菲 2024-08-30 15:05:21

要分析数据访问(这是大多数时候出现瓶颈的地方),请查看 django-live-profiler。与 Django 调试工具栏不同,它同时收集所有请求的数据,您可以在生产中运行它,而不会产生太多性能开销或暴露应用程序内部结构。

查看此屏幕截图

For profiling data access (which is where the bottleneck is most of the time) check out django-live-profiler. Unlike Django Debug Toolbar it collects data across all requests simultaneously and you can run it in production without too much performance overhead or exposing your app internals.

Check out this screenshot

硬不硬你别怂 2024-08-30 15:05:21

我最近需要分析一个 Django 应用程序,并尝试了其中的许多建议。 我最终使用 pyinstrument 代替可以使用中间件的单个更新添加到 Django 应用程序list 并提供基于堆栈的计时视图。

快速总结一下我使用其他一些工具的经验:

与我尝试过的其他工具相比,pyinstrument 的安装和使用要容易得多。

I needed to profile a Django app recently and tried many of these suggestions. I ended up using pyinstrument instead, which can be added to a Django app using a single update to the middleware list and provides a stack-based view of the timings.

Quick summary of my experience with some other tools:

  • Django Debug Toolbar is great if you the issue is due to SQL queries and works well in combination with pyinstrument
  • django-silk works well, but requires adding a context manager or decorator to each part of the stack where you want sub-request timings. It also provides an easy way to access cProfile timings and automatically displays ajax timings, both of which can be really helpful.
  • djdt-flamegraph looked promising, but the page never actually rendered on my system.

Compared to the other tools I tried, pyinstrument was dramatically easier to install and to use.

梦里寻她 2024-08-30 15:05:21

对于所有 KCacheGrind 粉丝来说,我发现将 shell 与 Django 出色的测试 Client 结合使用来动态生成配置文件日志非常容易,尤其是在生产中。我现在已经多次使用这种技术,因为它操作简单——不需要烦人的中间件或第三方 Django 应用程序!

例如,要分析似乎运行缓慢的特定视图,您可以打开 shell 并输入以下代码:

from django.test import Client
import hotshot

c = Client()
profiler = hotshot.Profile("yourprofile.prof")  # saves a logfile to your pwd
profiler.runcall(c.get, "/pattern/matching/your/view/")
profiler.close()

为了可视化生成的日志,我使用了 hotshot2cachegrind:

但还有其他选项:

For all you KCacheGrind fans, I find it's very easy to use the shell in tandem with Django's fantastic test Client for generating profile logs on-the-fly, especially in production. I've used this technique now on several occasions because it has a light touch — no pesky middleware or third-party Django applications are required!

For example, to profile a particular view that seems to be running slow, you could crack open the shell and type this code:

from django.test import Client
import hotshot

c = Client()
profiler = hotshot.Profile("yourprofile.prof")  # saves a logfile to your pwd
profiler.runcall(c.get, "/pattern/matching/your/view/")
profiler.close()

To visualize the resulting log, I've used hotshot2cachegrind:

But there are other options as well:

忆离笙 2024-08-30 15:05:21

当视图不是 HTML(例如 JSON)时,请使用简单的中间件方法进行分析。

以下是几个示例:

https://gist.github.com/1229685 - 捕获所有 sql 调用进入视图

https://gist.github.com/1229681 - 分析用于创建看法

When the views are not HTML, for example JSON, use simple middleware methods for profiling.

Here are a couple examples:

https://gist.github.com/1229685 - capture all sql calls went into the view

https://gist.github.com/1229681 - profile all method calls used to create the view

涫野音 2024-08-30 15:05:21

您可以使用 line_profiler

它允许显示代码的逐行分析以及每行旁边的时间(当一行被点击多次时,时间也会被总结)。

输入图片这里的描述

它与非 Django python 代码一起使用,但实际上在 Django 上使用它有一个小技巧:https ://stackoverflow.com/a/68163807/1937033

You can use line_profiler.

It allows to display a line-by-line analysis of your code with the time alongside of each line (When a line is hit several times, the time is summed up also).

enter image description here

It's used with not-Django python code but there's a little trick to use it on Django in fact: https://stackoverflow.com/a/68163807/1937033

心凉怎暖 2024-08-30 15:05:21

我正在使用 Silk 进行 Django 应用程序的实时分析和检查。这是一个很棒的工具。你可以看一下。

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

I am using silk for live profiling and inspection of Django application. This is a great tool. You can have a look on it.

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

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