runprofileserver.rst 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. RunProfileServer
  2. ================
  3. *We recommend that before you start profiling any language or
  4. framework you learn enough about it so that you feel comfortable with digging
  5. into its internals.*
  6. *Without sufficient knowledge it will not only be (very)
  7. hard but you're likely to make wrong assumptions (and fixes). As a rule of thumb,
  8. clean, well written code will help you a lot more than overzealous
  9. micro-optimizations will.*
  10. *This document is work in progress. If you feel you can help with
  11. better/clearer or additional information about profiling Django please leave a
  12. comment.*
  13. Introduction
  14. ------------
  15. *runprofileserver* starts Django's runserver command with hotshot/profiling
  16. tools enabled. It will save .prof files containing the profiling information
  17. into the --prof-path directory. Note that for each request made one profile
  18. data file is saved.
  19. By default the profile-data-files are saved in /tmp use the --prof-path option
  20. to specify your own target directory. Saving the data in a meaningful directory
  21. structure helps to keep your profile data organized and keeps /tmp uncluttered.
  22. (Yes this probably malfunctions systems such as Windows where /tmp does not exist)
  23. To define profile filenames use --prof-file option. Default format
  24. is "{path}.{duration:06d}ms.{time}" (Python
  25. `Format Specification <http://docs.python.org/3/library/string.html#formatspec>`_
  26. is used).
  27. Examples:
  28. * "{time}-{path}-{duration}ms" - to order profile-data-files by request time
  29. * "{duration:06d}ms.{path}.{time}" - to order by request duration
  30. gather_profile_stats.py
  31. -----------------------
  32. Django comes packed with a tool to aggregate these different prof files into
  33. one aggregated profile file. This tool is called *gather_profile_stats.py* and
  34. is located inside the *bin* directory of your Django distribution.
  35. KCacheGrind
  36. -----------
  37. Recent versions of *runprofileserver* have an option to save the profile data
  38. into a KCacheGrind compatible format. So you can use the excellent KCacheGrind
  39. tool for analyzing the profile data.
  40. Example::
  41. $ mkdir /tmp/my-profile-data
  42. $ ./manage.py runprofileserver --kcachegrind --prof-path=/tmp/my-profile-data
  43. Validating models...
  44. 0 errors found
  45. Django version 1.0-post-release-SVN-SVN-unknown, using settings 'complete_project.settings'
  46. Development server is running at http://127.0.0.1:8000/
  47. Quit the server with CONTROL-C.
  48. [13/Nov/2008 06:29:38] "GET / HTTP/1.1" 200 41107
  49. [13/Nov/2008 06:29:39] "GET /site_media/base.css?743 HTTP/1.1" 200 17227
  50. [13/Nov/2008 06:29:39] "GET /site_media/logo.png HTTP/1.1" 200 3474
  51. [13/Nov/2008 06:29:39] "GET /site_media/jquery.js HTTP/1.1" 200 31033
  52. [13/Nov/2008 06:29:39] "GET /site_media/heading.png HTTP/1.1" 200 247
  53. [13/Nov/2008 06:29:39] "GET /site_media/base.js HTTP/1.1" 200 751
  54. <ctrl-c>
  55. $ kcachegrind /tmp/my-profile-data/root.12574391.592.prof
  56. Here is a screenshot of how the above commands might look in KCacheGrind:
  57. http://trbs.net/media/misc/django-runprofileserver-kcachegrind-full.jpg
  58. Links
  59. -----
  60. * http://code.djangoproject.com/wiki/ProfilingDjango
  61. * http://www.rkblog.rk.edu.pl/w/p/django-profiling-hotshot-and-kcachegrind/
  62. * http://code.djangoproject.com/browser/django/trunk/django/bin/profiling/gather_profile_stats.py
  63. * http://www.oluyede.org/blog/2007/03/07/profiling-django/
  64. * http://simonwillison.net/2008/May/22/debugging/