Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling 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?

like image 233
sharjeel Avatar asked Mar 02 '10 09:03

sharjeel


People also ask

What is Django profiling?

django-profiler is util for profiling python code mainly in django projects but can be used also on ordinary python code. It counts sql queries a measures time of code execution. It logs its output via standard python logging library and uses logger profiling .

What is Django silk?

Silk is a live profiling and inspection tool for the Django framework. It primarily consists of: Middleware for intercepting Requests/Responses. A wrapper around SQL execution for profiling of database queries. A context manager/decorator for profiling blocks of code and functions either manually or dynamically.

What are Middlewares in Django?

Middleware is a framework of hooks into Django's request/response processing. It's a light, low-level “plugin” system for globally altering Django's input or output. Each middleware component is responsible for doing some specific function.


2 Answers

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.

like image 183
Silver Light Avatar answered Sep 29 '22 19:09

Silver Light


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 
like image 31
Tomasz Zieliński Avatar answered Sep 29 '22 17:09

Tomasz Zieliński