django.core.handlers.profiler-hotshot provides profiling support for Django when using mod_python. Hotshot generates logs that can be also used by aplication like kcachegrind. Read Django profiling with hotshot and kcachegrind for more details.
Here's a similar solution for the internal development webserver. Apply the included patch to django/core/management.py and start Django with manage.py runserver. Each request will create a .prof file in your /tmp directory. You can then open a Python shell and print the statistics:
>>> import hotshot.stats
>>> stats = hotshot.stats.load("stones.prof")
>>> stats.strip_dirs()
>>> stats.sort_stats('time', 'calls')
>>> stats.print_stats(20)
You can also use gather_profile_stats.py in the Django distribution to aggregate the generated profilings and open them with pstats.Stats
Using WSGI to profile
An alternative approach is to run django from a boring WSGI server and profile that. For example using wsgiref you can create a really simple WSGI server for running django:
from wsgiref.simple_server import make_server
from django.core.handlers.wsgi import WSGIHandler
httpd = make_server('', 8000, WSGIHandler())
httpd.serve_forever()
Run this using:
$ DJANGO_SETTINGS_MODULE=mysite.settings python myserver.py
To profile this run using cProfile (or profile):
$ DJANGO_SETTINGS_MODULE=mysite.settings python -m cProfile myserver.py
The default invocation will print the profile stats when you kill the server, it's probably more useful to write to a file and use pstats to read it:
$ DJANGO_SETTINGS_MODULE=mysite.settings python -m cProfile -o mysite.profile myserver.py
... lots of requests ...
$ python
>>> import pstats
>>> s = pstats.Stats("mysite.profile")
>>> s.sort_stats("time").print_stats(20)
Attachments
- runserver-profiler.diff (1.3 kB) -
patch for management.py enables profiling for runserver
, added by akaihola on 04/03/06 08:33:45. - runserver-profiler2.diff (1.2 kB) -
updated the patch against the latest revision
, added by LawrenceOluyede on 03/05/07 12:16:06. - runserver-profiler2.2.diff (1.2 kB) -
updated the patch against the latest revision and made the path configurable
, added by LawrenceOluyede on 03/07/07 05:11:59. - profiler-cprofile.py (0.5 kB) -
Adapted profile handler to use cProfile module. Place in django/core/handlers and set PythonHandler? in apache conf
, added by chrisj on 03/02/08 21:16:07. - runserver-profiler3.diff (2.3 kB) -
New patch against r7569. Extend runserver command with '--profile' argument. Will run djangos development server with profiling output into systems temporary directory.
, added by Stefan Tjarks <stefan AT tjarks DOT de> on 06/08/08 18:14:50.
