runprofileserver.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. Profiler choice
  36. ---------------
  37. *runprofileserver* supports two profilers: *hotshot* and *cProfile*. Both come
  38. with the standard Python library but *cProfile* is more recent and may not be
  39. available on all systems. For this reason, *hotshot* is the default profiler.
  40. However, *hotshot* `is not maintained anymore <https://docs.python.org/2/library/profile.html#introduction-to-the-profilers>`_
  41. and using *cProfile* is usually the recommended way.
  42. If it is available on your system, you can use it with the option ``--use-cprofile``.
  43. Example::
  44. $ mkdir /tmp/my-profile-data
  45. $ ./manage.py runprofileserver --use-cprofile --prof-path=/tmp/my-profile-data
  46. If you used the default profiler but are not able to open the profiling results
  47. with the ``pstats`` module or with your profiling GUI of choice because of an
  48. error "*ValueError: bad marshal data (unknown type code)*", try using *cProfile*
  49. instead.
  50. KCacheGrind
  51. -----------
  52. Recent versions of *runprofileserver* have an option to save the profile data
  53. into a KCacheGrind compatible format. So you can use the excellent KCacheGrind
  54. tool for analyzing the profile data.
  55. Example::
  56. $ mkdir /tmp/my-profile-data
  57. $ ./manage.py runprofileserver --kcachegrind --prof-path=/tmp/my-profile-data
  58. Validating models...
  59. 0 errors found
  60. Django version X.Y.Z, using settings 'complete_project.settings'
  61. Development server is running at http://127.0.0.1:8000/
  62. Quit the server with CONTROL-C.
  63. [13/Nov/2008 06:29:38] "GET / HTTP/1.1" 200 41107
  64. [13/Nov/2008 06:29:39] "GET /site_media/base.css?743 HTTP/1.1" 200 17227
  65. [13/Nov/2008 06:29:39] "GET /site_media/logo.png HTTP/1.1" 200 3474
  66. [13/Nov/2008 06:29:39] "GET /site_media/jquery.js HTTP/1.1" 200 31033
  67. [13/Nov/2008 06:29:39] "GET /site_media/heading.png HTTP/1.1" 200 247
  68. [13/Nov/2008 06:29:39] "GET /site_media/base.js HTTP/1.1" 200 751
  69. <ctrl-c>
  70. $ kcachegrind /tmp/my-profile-data/root.12574391.592.prof
  71. Here is a screenshot of how the above commands might look in KCacheGrind:
  72. http://trbs.net/media/misc/django-runprofileserver-kcachegrind-full.jpg
  73. Links
  74. -----
  75. * http://code.djangoproject.com/wiki/ProfilingDjango
  76. * http://www.rkblog.rk.edu.pl/w/p/django-profiling-hotshot-and-kcachegrind/
  77. * http://code.djangoproject.com/browser/django/trunk/django/bin/profiling/gather_profile_stats.py
  78. * http://www.oluyede.org/blog/2007/03/07/profiling-django/
  79. * http://simonwillison.net/2008/May/22/debugging/