| | 15 | |
| | 16 | == Using WSGI to profile == |
| | 17 | |
| | 18 | 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: |
| | 19 | |
| | 20 | {{{ |
| | 21 | from wsgiref.simple_server import make_server |
| | 22 | from django.core.handlers.wsgi import WSGIHandler |
| | 23 | httpd = make_server('', 8000, WSGIHandler()) |
| | 24 | httpd.serve_forever() |
| | 25 | }}} |
| | 26 | |
| | 27 | Run this using: |
| | 28 | |
| | 29 | {{{ |
| | 30 | $ DJANGO_SETTINGS_MODULE=mysite.settings python myserver.py |
| | 31 | }}} |
| | 32 | |
| | 33 | To profile this run using cProfile (or profile): |
| | 34 | |
| | 35 | {{{ |
| | 36 | $ DJANGO_SETTINGS_MODULE=mysite.settings python -m cProfile myserver.py |
| | 37 | }}} |
| | 38 | |
| | 39 | 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: |
| | 40 | |
| | 41 | {{{ |
| | 42 | $ DJANGO_SETTINGS_MODULE=mysite.settings python -m cProfile -o mysite.profile myserver.py |
| | 43 | ... lots of requests ... |
| | 44 | |
| | 45 | $ python |
| | 46 | >>> import pstats |
| | 47 | >>> s = pstats.Stats("mysite.profile") |
| | 48 | >>> s.sort_stats("time").print_stats(20) |
| | 49 | }}} |
| | 50 | |