blob: 98e0b6dcce918ca06085b1e201aa85ac0ee23340 [file] [log] [blame]
Anthony Baxterb3303ef2003-04-07 12:19:15 +00001\section{\module{hotshot} ---
2 High performance logging profiler}
3
4\declaremodule{standard}{hotshot}
Fred Drakefcd845a2003-04-09 04:06:37 +00005\modulesynopsis{High performance logging profiler, mostly written in C.}
Anthony Baxterb3303ef2003-04-07 12:19:15 +00006\moduleauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
7\sectionauthor{Anthony Baxter}{anthony@interlink.com.au}
8
Anthony Baxterb3303ef2003-04-07 12:19:15 +00009\versionadded{2.2}
10
Anthony Baxterb3303ef2003-04-07 12:19:15 +000011
Fred Drakefcd845a2003-04-09 04:06:37 +000012This module provides a nicer interface to the \module{_hotshot} C module.
Anthony Baxterb3303ef2003-04-07 12:19:15 +000013Hotshot is a replacement for the existing \refmodule{profile} module. As it's
Fred Drakefcd845a2003-04-09 04:06:37 +000014written mostly in C, it should result in a much smaller performance impact
15than the existing \refmodule{profile} module.
Anthony Baxterb3303ef2003-04-07 12:19:15 +000016
Armin Rigoa871ef22006-02-08 12:53:56 +000017\begin{notice}[note]
18 The \module{hotshot} module focuses on minimizing the overhead
19 while profiling, at the expense of long data post-processing times.
20 For common usages it is recommended to use \module{cProfile} instead.
21 \module{hotshot} is not maintained and might be removed from the
22 standard library in the future.
23\end{notice}
24
25\versionchanged[the results should be more meaningful than in the
26past: the timing core contained a critical bug]{2.5}
27
Fred Drake621b4432004-01-16 17:30:16 +000028\begin{notice}[warning]
29 The \module{hotshot} profiler does not yet work well with threads.
30 It is useful to use an unthreaded script to run the profiler over
31 the code you're interested in measuring if at all possible.
32\end{notice}
33
34
35\begin{classdesc}{Profile}{logfile\optional{, lineevents\optional{,
36 linetimings}}}
Fred Drakefcd845a2003-04-09 04:06:37 +000037The profiler object. The argument \var{logfile} is the name of a log
38file to use for logged profile data. The argument \var{lineevents}
39specifies whether to generate events for every source line, or just on
40function call/return. It defaults to \code{0} (only log function
41call/return). The argument \var{linetimings} specifies whether to
42record timing information. It defaults to \code{1} (store timing
Anthony Baxterb3303ef2003-04-07 12:19:15 +000043information).
Anthony Baxterb3303ef2003-04-07 12:19:15 +000044\end{classdesc}
45
Fred Drakefcd845a2003-04-09 04:06:37 +000046
Anthony Baxterb3303ef2003-04-07 12:19:15 +000047\subsection{Profile Objects \label{hotshot-objects}}
48
49Profile objects have the following methods:
50
51\begin{methoddesc}{addinfo}{key, value}
52Add an arbitrary labelled value to the profile output.
53\end{methoddesc}
54
55\begin{methoddesc}{close}{}
56Close the logfile and terminate the profiler.
57\end{methoddesc}
Fred Drakefcd845a2003-04-09 04:06:37 +000058
Anthony Baxterb3303ef2003-04-07 12:19:15 +000059\begin{methoddesc}{fileno}{}
60Return the file descriptor of the profiler's log file.
61\end{methoddesc}
62
63\begin{methoddesc}{run}{cmd}
Fred Drakefcd845a2003-04-09 04:06:37 +000064Profile an \keyword{exec}-compatible string in the script environment.
65The globals from the \refmodule[main]{__main__} module are used as
Anthony Baxterb3303ef2003-04-07 12:19:15 +000066both the globals and locals for the script.
67\end{methoddesc}
68
69\begin{methoddesc}{runcall}{func, *args, **keywords}
70Profile a single call of a callable.
Anthony Baxterb3303ef2003-04-07 12:19:15 +000071Additional positional and keyword arguments may be passed
72along; the result of the call is returned, and exceptions are
Raymond Hettinger68804312005-01-01 00:28:46 +000073allowed to propagate cleanly, while ensuring that profiling is
Anthony Baxterb3303ef2003-04-07 12:19:15 +000074disabled on the way out.
75\end{methoddesc}
76
77
78\begin{methoddesc}{runctx}{cmd, globals, locals}
Fred Drakefcd845a2003-04-09 04:06:37 +000079Evaluate an \keyword{exec}-compatible string in a specific environment.
Anthony Baxterb3303ef2003-04-07 12:19:15 +000080The string is compiled before profiling begins.
81\end{methoddesc}
82
83\begin{methoddesc}{start}{}
84Start the profiler.
85\end{methoddesc}
86
87\begin{methoddesc}{stop}{}
88Stop the profiler.
89\end{methoddesc}
90
Anthony Baxterb3303ef2003-04-07 12:19:15 +000091
Fred Drakefcd845a2003-04-09 04:06:37 +000092\subsection{Using hotshot data}
93
94\declaremodule{standard}{hotshot.stats}
Anthony Baxterb3303ef2003-04-07 12:19:15 +000095\modulesynopsis{Statistical analysis for Hotshot}
96
97\versionadded{2.2}
98
Fred Drakefcd845a2003-04-09 04:06:37 +000099This module loads hotshot profiling data into the standard \module{pstats}
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000100Stats objects.
101
102\begin{funcdesc}{load}{filename}
Fred Drakefcd845a2003-04-09 04:06:37 +0000103Load hotshot data from \var{filename}. Returns an instance
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000104of the \class{pstats.Stats} class.
105\end{funcdesc}
106
107\begin{seealso}
Fred Drakefcd845a2003-04-09 04:06:37 +0000108 \seemodule{profile}{The \module{profile} module's \class{Stats} class}
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000109\end{seealso}
110
Fred Drakefcd845a2003-04-09 04:06:37 +0000111
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000112\subsection{Example Usage \label{hotshot-example}}
113
Fred Drakefcd845a2003-04-09 04:06:37 +0000114Note that this example runs the python ``benchmark'' pystones. It can
Anthony Baxtercb8ed532003-04-07 12:21:56 +0000115take some time to run, and will produce large output files.
116
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000117\begin{verbatim}
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000118>>> import hotshot, hotshot.stats, test.pystone
119>>> prof = hotshot.Profile("stones.prof")
120>>> benchtime, stones = prof.runcall(test.pystone.pystones)
121>>> prof.close()
122>>> stats = hotshot.stats.load("stones.prof")
123>>> stats.strip_dirs()
124>>> stats.sort_stats('time', 'calls')
125>>> stats.print_stats(20)
126 850004 function calls in 10.090 CPU seconds
127
128 Ordered by: internal time, call count
129
130 ncalls tottime percall cumtime percall filename:lineno(function)
131 1 3.295 3.295 10.090 10.090 pystone.py:79(Proc0)
132 150000 1.315 0.000 1.315 0.000 pystone.py:203(Proc7)
133 50000 1.313 0.000 1.463 0.000 pystone.py:229(Func2)
134 .
135 .
136 .
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000137\end{verbatim}