blob: 33d6a069c6fd5efded84abd44909dac55e12ecd4 [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
Fred Drake621b4432004-01-16 17:30:16 +000017\begin{notice}[warning]
18 The \module{hotshot} profiler does not yet work well with threads.
19 It is useful to use an unthreaded script to run the profiler over
20 the code you're interested in measuring if at all possible.
21\end{notice}
22
23
24\begin{classdesc}{Profile}{logfile\optional{, lineevents\optional{,
25 linetimings}}}
Fred Drakefcd845a2003-04-09 04:06:37 +000026The profiler object. The argument \var{logfile} is the name of a log
27file to use for logged profile data. The argument \var{lineevents}
28specifies whether to generate events for every source line, or just on
29function call/return. It defaults to \code{0} (only log function
30call/return). The argument \var{linetimings} specifies whether to
31record timing information. It defaults to \code{1} (store timing
Anthony Baxterb3303ef2003-04-07 12:19:15 +000032information).
Anthony Baxterb3303ef2003-04-07 12:19:15 +000033\end{classdesc}
34
Fred Drakefcd845a2003-04-09 04:06:37 +000035
Anthony Baxterb3303ef2003-04-07 12:19:15 +000036\subsection{Profile Objects \label{hotshot-objects}}
37
38Profile objects have the following methods:
39
40\begin{methoddesc}{addinfo}{key, value}
41Add an arbitrary labelled value to the profile output.
42\end{methoddesc}
43
44\begin{methoddesc}{close}{}
45Close the logfile and terminate the profiler.
46\end{methoddesc}
Fred Drakefcd845a2003-04-09 04:06:37 +000047
Anthony Baxterb3303ef2003-04-07 12:19:15 +000048\begin{methoddesc}{fileno}{}
49Return the file descriptor of the profiler's log file.
50\end{methoddesc}
51
52\begin{methoddesc}{run}{cmd}
Fred Drakefcd845a2003-04-09 04:06:37 +000053Profile an \keyword{exec}-compatible string in the script environment.
54The globals from the \refmodule[main]{__main__} module are used as
Anthony Baxterb3303ef2003-04-07 12:19:15 +000055both the globals and locals for the script.
56\end{methoddesc}
57
58\begin{methoddesc}{runcall}{func, *args, **keywords}
59Profile a single call of a callable.
Anthony Baxterb3303ef2003-04-07 12:19:15 +000060Additional positional and keyword arguments may be passed
61along; the result of the call is returned, and exceptions are
Raymond Hettinger68804312005-01-01 00:28:46 +000062allowed to propagate cleanly, while ensuring that profiling is
Anthony Baxterb3303ef2003-04-07 12:19:15 +000063disabled on the way out.
64\end{methoddesc}
65
66
67\begin{methoddesc}{runctx}{cmd, globals, locals}
Fred Drakefcd845a2003-04-09 04:06:37 +000068Evaluate an \keyword{exec}-compatible string in a specific environment.
Anthony Baxterb3303ef2003-04-07 12:19:15 +000069The string is compiled before profiling begins.
70\end{methoddesc}
71
72\begin{methoddesc}{start}{}
73Start the profiler.
74\end{methoddesc}
75
76\begin{methoddesc}{stop}{}
77Stop the profiler.
78\end{methoddesc}
79
Anthony Baxterb3303ef2003-04-07 12:19:15 +000080
Fred Drakefcd845a2003-04-09 04:06:37 +000081\subsection{Using hotshot data}
82
83\declaremodule{standard}{hotshot.stats}
Anthony Baxterb3303ef2003-04-07 12:19:15 +000084\modulesynopsis{Statistical analysis for Hotshot}
85
86\versionadded{2.2}
87
Fred Drakefcd845a2003-04-09 04:06:37 +000088This module loads hotshot profiling data into the standard \module{pstats}
Anthony Baxterb3303ef2003-04-07 12:19:15 +000089Stats objects.
90
91\begin{funcdesc}{load}{filename}
Fred Drakefcd845a2003-04-09 04:06:37 +000092Load hotshot data from \var{filename}. Returns an instance
Anthony Baxterb3303ef2003-04-07 12:19:15 +000093of the \class{pstats.Stats} class.
94\end{funcdesc}
95
96\begin{seealso}
Fred Drakefcd845a2003-04-09 04:06:37 +000097 \seemodule{profile}{The \module{profile} module's \class{Stats} class}
Anthony Baxterb3303ef2003-04-07 12:19:15 +000098\end{seealso}
99
Fred Drakefcd845a2003-04-09 04:06:37 +0000100
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000101\subsection{Example Usage \label{hotshot-example}}
102
Fred Drakefcd845a2003-04-09 04:06:37 +0000103Note that this example runs the python ``benchmark'' pystones. It can
Anthony Baxtercb8ed532003-04-07 12:21:56 +0000104take some time to run, and will produce large output files.
105
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000106\begin{verbatim}
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000107>>> import hotshot, hotshot.stats, test.pystone
108>>> prof = hotshot.Profile("stones.prof")
109>>> benchtime, stones = prof.runcall(test.pystone.pystones)
110>>> prof.close()
111>>> stats = hotshot.stats.load("stones.prof")
112>>> stats.strip_dirs()
113>>> stats.sort_stats('time', 'calls')
114>>> stats.print_stats(20)
115 850004 function calls in 10.090 CPU seconds
116
117 Ordered by: internal time, call count
118
119 ncalls tottime percall cumtime percall filename:lineno(function)
120 1 3.295 3.295 10.090 10.090 pystone.py:79(Proc0)
121 150000 1.315 0.000 1.315 0.000 pystone.py:203(Proc7)
122 50000 1.313 0.000 1.463 0.000 pystone.py:229(Func2)
123 .
124 .
125 .
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000126\end{verbatim}