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} |
| 5 | \moduleauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
| 6 | \sectionauthor{Anthony Baxter}{anthony@interlink.com.au} |
| 7 | |
| 8 | |
| 9 | \versionadded{2.2} |
| 10 | |
| 11 | \modulesynopsis{High performance logging profiler, mostly written in C.} |
| 12 | |
| 13 | |
| 14 | This module provides a nicer interface to the \code{_hotshot} C module. |
| 15 | Hotshot is a replacement for the existing \refmodule{profile} module. As it's |
| 16 | written mostly in C, it should result in a much smaller performance impact |
| 17 | than the existing profile module. |
| 18 | |
| 19 | \begin{classdesc}{Profile}{logfile, \optional{, lineevents=0, linetimings=1}} |
| 20 | |
| 21 | The profiler object. The argument \var{logfile} is the name of a log file |
| 22 | to use for logged profile data. The argument \var{lineevents} specifies whether |
| 23 | to generate events for every source line, or just on function call/return. It |
| 24 | defaults to 0 (only log function call/return). The argument \var{linetimings} |
| 25 | specifies whether to record timing information. It defaults to 1 (store timing |
| 26 | information). |
| 27 | |
| 28 | \end{classdesc} |
| 29 | |
| 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} |
| 41 | |
| 42 | % |
| 43 | \begin{methoddesc}{fileno}{} |
| 44 | Return the file descriptor of the profiler's log file. |
| 45 | \end{methoddesc} |
| 46 | |
| 47 | \begin{methoddesc}{run}{cmd} |
| 48 | Profile an exec-compatible string in the script environment. |
| 49 | |
| 50 | The globals from the \module{__main__} module are used as |
| 51 | both the globals and locals for the script. |
| 52 | \end{methoddesc} |
| 53 | |
| 54 | \begin{methoddesc}{runcall}{func, *args, **keywords} |
| 55 | Profile a single call of a callable. |
| 56 | |
| 57 | Additional positional and keyword arguments may be passed |
| 58 | along; the result of the call is returned, and exceptions are |
| 59 | allowed to propogate cleanly, while ensuring that profiling is |
| 60 | disabled on the way out. |
| 61 | \end{methoddesc} |
| 62 | |
| 63 | |
| 64 | \begin{methoddesc}{runctx}{cmd, globals, locals} |
| 65 | Evaluate an exec-compatible string in a specific environment. |
| 66 | |
| 67 | The string is compiled before profiling begins. |
| 68 | \end{methoddesc} |
| 69 | |
| 70 | \begin{methoddesc}{start}{} |
| 71 | Start the profiler. |
| 72 | \end{methoddesc} |
| 73 | |
| 74 | \begin{methoddesc}{stop}{} |
| 75 | Stop the profiler. |
| 76 | \end{methoddesc} |
| 77 | |
| 78 | \subsection{Using hotshot data} |
| 79 | \declaremodule{standard}{hotshot.stats} |
| 80 | |
| 81 | \modulesynopsis{Statistical analysis for Hotshot} |
| 82 | |
| 83 | \versionadded{2.2} |
| 84 | |
| 85 | This module loads hotshot profiling data into the standard \module{pstats} |
| 86 | Stats objects. |
| 87 | |
| 88 | \begin{funcdesc}{load}{filename} |
| 89 | Load hotshot data from \var{filename}. Returns an instance |
| 90 | of the \class{pstats.Stats} class. |
| 91 | \end{funcdesc} |
| 92 | |
| 93 | \begin{seealso} |
| 94 | \seemodule{profile}{The profile module's \class{Stats} class. } |
| 95 | \end{seealso} |
| 96 | |
| 97 | \subsection{Example Usage \label{hotshot-example}} |
| 98 | |
| 99 | \begin{verbatim} |
| 100 | |
| 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 | . |
| 120 | |
| 121 | \end{verbatim} |
| 122 | |
| 123 | |