Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 1 | \section{\module{hotshot} --- |
| 2 | High performance logging profiler} |
| 3 | |
| 4 | \declaremodule{standard}{hotshot} |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 5 | \modulesynopsis{High performance logging profiler, mostly written in C.} |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 6 | \moduleauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
| 7 | \sectionauthor{Anthony Baxter}{anthony@interlink.com.au} |
| 8 | |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 9 | \versionadded{2.2} |
| 10 | |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 11 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 12 | This module provides a nicer interface to the \module{_hotshot} C module. |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 13 | Hotshot is a replacement for the existing \refmodule{profile} module. As it's |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 14 | written mostly in C, it should result in a much smaller performance impact |
| 15 | than the existing \refmodule{profile} module. |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 16 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 17 | \begin{classdesc}{Profile}{logfile\optional{, |
| 18 | lineevents\code{=0}\optional{, |
| 19 | linetimings\code{=1}}}} |
| 20 | The profiler object. The argument \var{logfile} is the name of a log |
| 21 | file to use for logged profile data. The argument \var{lineevents} |
| 22 | specifies whether to generate events for every source line, or just on |
| 23 | function call/return. It defaults to \code{0} (only log function |
| 24 | call/return). The argument \var{linetimings} specifies whether to |
| 25 | record timing information. It defaults to \code{1} (store timing |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 26 | information). |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 27 | \end{classdesc} |
| 28 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 29 | |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 30 | \subsection{Profile Objects \label{hotshot-objects}} |
| 31 | |
| 32 | Profile objects have the following methods: |
| 33 | |
| 34 | \begin{methoddesc}{addinfo}{key, value} |
| 35 | Add an arbitrary labelled value to the profile output. |
| 36 | \end{methoddesc} |
| 37 | |
| 38 | \begin{methoddesc}{close}{} |
| 39 | Close the logfile and terminate the profiler. |
| 40 | \end{methoddesc} |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 41 | |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 42 | \begin{methoddesc}{fileno}{} |
| 43 | Return the file descriptor of the profiler's log file. |
| 44 | \end{methoddesc} |
| 45 | |
| 46 | \begin{methoddesc}{run}{cmd} |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 47 | Profile an \keyword{exec}-compatible string in the script environment. |
| 48 | The globals from the \refmodule[main]{__main__} module are used as |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 49 | both the globals and locals for the script. |
| 50 | \end{methoddesc} |
| 51 | |
| 52 | \begin{methoddesc}{runcall}{func, *args, **keywords} |
| 53 | Profile a single call of a callable. |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 54 | Additional positional and keyword arguments may be passed |
| 55 | along; the result of the call is returned, and exceptions are |
| 56 | allowed to propogate cleanly, while ensuring that profiling is |
| 57 | disabled on the way out. |
| 58 | \end{methoddesc} |
| 59 | |
| 60 | |
| 61 | \begin{methoddesc}{runctx}{cmd, globals, locals} |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 62 | Evaluate an \keyword{exec}-compatible string in a specific environment. |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 63 | The string is compiled before profiling begins. |
| 64 | \end{methoddesc} |
| 65 | |
| 66 | \begin{methoddesc}{start}{} |
| 67 | Start the profiler. |
| 68 | \end{methoddesc} |
| 69 | |
| 70 | \begin{methoddesc}{stop}{} |
| 71 | Stop the profiler. |
| 72 | \end{methoddesc} |
| 73 | |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 74 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 75 | \subsection{Using hotshot data} |
| 76 | |
| 77 | \declaremodule{standard}{hotshot.stats} |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 78 | \modulesynopsis{Statistical analysis for Hotshot} |
| 79 | |
| 80 | \versionadded{2.2} |
| 81 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 82 | This module loads hotshot profiling data into the standard \module{pstats} |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 83 | Stats objects. |
| 84 | |
| 85 | \begin{funcdesc}{load}{filename} |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 86 | Load hotshot data from \var{filename}. Returns an instance |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 87 | of the \class{pstats.Stats} class. |
| 88 | \end{funcdesc} |
| 89 | |
| 90 | \begin{seealso} |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 91 | \seemodule{profile}{The \module{profile} module's \class{Stats} class} |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 92 | \end{seealso} |
| 93 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 94 | |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 95 | \subsection{Example Usage \label{hotshot-example}} |
| 96 | |
Fred Drake | fcd845a | 2003-04-09 04:06:37 +0000 | [diff] [blame] | 97 | Note that this example runs the python ``benchmark'' pystones. It can |
Anthony Baxter | cb8ed53 | 2003-04-07 12:21:56 +0000 | [diff] [blame] | 98 | take some time to run, and will produce large output files. |
| 99 | |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 100 | \begin{verbatim} |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 101 | >>> import hotshot, hotshot.stats, test.pystone |
| 102 | >>> prof = hotshot.Profile("stones.prof") |
| 103 | >>> benchtime, stones = prof.runcall(test.pystone.pystones) |
| 104 | >>> prof.close() |
| 105 | >>> stats = hotshot.stats.load("stones.prof") |
| 106 | >>> stats.strip_dirs() |
| 107 | >>> stats.sort_stats('time', 'calls') |
| 108 | >>> stats.print_stats(20) |
| 109 | 850004 function calls in 10.090 CPU seconds |
| 110 | |
| 111 | Ordered by: internal time, call count |
| 112 | |
| 113 | ncalls tottime percall cumtime percall filename:lineno(function) |
| 114 | 1 3.295 3.295 10.090 10.090 pystone.py:79(Proc0) |
| 115 | 150000 1.315 0.000 1.315 0.000 pystone.py:203(Proc7) |
| 116 | 50000 1.313 0.000 1.463 0.000 pystone.py:229(Func2) |
| 117 | . |
| 118 | . |
| 119 | . |
Anthony Baxter | b3303ef | 2003-04-07 12:19:15 +0000 | [diff] [blame] | 120 | \end{verbatim} |