/Doc/library/hotshot.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 152 lines · 91 code · 61 blank · 0 comment · 0 complexity · 0bf1ee37fb3a426f1cb29a8c5bea04f8 MD5 · raw file

  1. :mod:`hotshot` --- High performance logging profiler
  2. ====================================================
  3. .. module:: hotshot
  4. :synopsis: High performance logging profiler, mostly written in C.
  5. .. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
  6. .. sectionauthor:: Anthony Baxter <anthony@interlink.com.au>
  7. .. versionadded:: 2.2
  8. This module provides a nicer interface to the :mod:`_hotshot` C module. Hotshot
  9. is a replacement for the existing :mod:`profile` module. As it's written mostly
  10. in C, it should result in a much smaller performance impact than the existing
  11. :mod:`profile` module.
  12. .. note::
  13. The :mod:`hotshot` module focuses on minimizing the overhead while profiling, at
  14. the expense of long data post-processing times. For common usage it is
  15. recommended to use :mod:`cProfile` instead. :mod:`hotshot` is not maintained and
  16. might be removed from the standard library in the future.
  17. .. versionchanged:: 2.5
  18. The results should be more meaningful than in the past: the timing core
  19. contained a critical bug.
  20. .. note::
  21. The :mod:`hotshot` profiler does not yet work well with threads. It is useful to
  22. use an unthreaded script to run the profiler over the code you're interested in
  23. measuring if at all possible.
  24. .. class:: Profile(logfile[, lineevents[, linetimings]])
  25. The profiler object. The argument *logfile* is the name of a log file to use for
  26. logged profile data. The argument *lineevents* specifies whether to generate
  27. events for every source line, or just on function call/return. It defaults to
  28. ``0`` (only log function call/return). The argument *linetimings* specifies
  29. whether to record timing information. It defaults to ``1`` (store timing
  30. information).
  31. .. _hotshot-objects:
  32. Profile Objects
  33. ---------------
  34. Profile objects have the following methods:
  35. .. method:: Profile.addinfo(key, value)
  36. Add an arbitrary labelled value to the profile output.
  37. .. method:: Profile.close()
  38. Close the logfile and terminate the profiler.
  39. .. method:: Profile.fileno()
  40. Return the file descriptor of the profiler's log file.
  41. .. method:: Profile.run(cmd)
  42. Profile an :keyword:`exec`\ -compatible string in the script environment. The
  43. globals from the :mod:`__main__` module are used as both the globals and locals
  44. for the script.
  45. .. method:: Profile.runcall(func, *args, **keywords)
  46. Profile a single call of a callable. Additional positional and keyword arguments
  47. may be passed along; the result of the call is returned, and exceptions are
  48. allowed to propagate cleanly, while ensuring that profiling is disabled on the
  49. way out.
  50. .. method:: Profile.runctx(cmd, globals, locals)
  51. Evaluate an :keyword:`exec`\ -compatible string in a specific environment. The
  52. string is compiled before profiling begins.
  53. .. method:: Profile.start()
  54. Start the profiler.
  55. .. method:: Profile.stop()
  56. Stop the profiler.
  57. Using hotshot data
  58. ------------------
  59. .. module:: hotshot.stats
  60. :synopsis: Statistical analysis for Hotshot
  61. .. versionadded:: 2.2
  62. This module loads hotshot profiling data into the standard :mod:`pstats` Stats
  63. objects.
  64. .. function:: load(filename)
  65. Load hotshot data from *filename*. Returns an instance of the
  66. :class:`pstats.Stats` class.
  67. .. seealso::
  68. Module :mod:`profile`
  69. The :mod:`profile` module's :class:`Stats` class
  70. .. _hotshot-example:
  71. Example Usage
  72. -------------
  73. Note that this example runs the python "benchmark" pystones. It can take some
  74. time to run, and will produce large output files. ::
  75. >>> import hotshot, hotshot.stats, test.pystone
  76. >>> prof = hotshot.Profile("stones.prof")
  77. >>> benchtime, stones = prof.runcall(test.pystone.pystones)
  78. >>> prof.close()
  79. >>> stats = hotshot.stats.load("stones.prof")
  80. >>> stats.strip_dirs()
  81. >>> stats.sort_stats('time', 'calls')
  82. >>> stats.print_stats(20)
  83. 850004 function calls in 10.090 CPU seconds
  84. Ordered by: internal time, call count
  85. ncalls tottime percall cumtime percall filename:lineno(function)
  86. 1 3.295 3.295 10.090 10.090 pystone.py:79(Proc0)
  87. 150000 1.315 0.000 1.315 0.000 pystone.py:203(Proc7)
  88. 50000 1.313 0.000 1.463 0.000 pystone.py:229(Func2)
  89. .
  90. .
  91. .