blob: 05bc067ed1a4ce6ce6ff095caeed74e9f114094d [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 Drakefcd845a2003-04-09 04:06:37 +000017\begin{classdesc}{Profile}{logfile\optional{,
18 lineevents\code{=0}\optional{,
19 linetimings\code{=1}}}}
20The profiler object. The argument \var{logfile} is the name of a log
21file to use for logged profile data. The argument \var{lineevents}
22specifies whether to generate events for every source line, or just on
23function call/return. It defaults to \code{0} (only log function
24call/return). The argument \var{linetimings} specifies whether to
25record timing information. It defaults to \code{1} (store timing
Anthony Baxterb3303ef2003-04-07 12:19:15 +000026information).
Anthony Baxterb3303ef2003-04-07 12:19:15 +000027\end{classdesc}
28
Fred Drakefcd845a2003-04-09 04:06:37 +000029
Anthony Baxterb3303ef2003-04-07 12:19:15 +000030\subsection{Profile Objects \label{hotshot-objects}}
31
32Profile objects have the following methods:
33
34\begin{methoddesc}{addinfo}{key, value}
35Add an arbitrary labelled value to the profile output.
36\end{methoddesc}
37
38\begin{methoddesc}{close}{}
39Close the logfile and terminate the profiler.
40\end{methoddesc}
Fred Drakefcd845a2003-04-09 04:06:37 +000041
Anthony Baxterb3303ef2003-04-07 12:19:15 +000042\begin{methoddesc}{fileno}{}
43Return the file descriptor of the profiler's log file.
44\end{methoddesc}
45
46\begin{methoddesc}{run}{cmd}
Fred Drakefcd845a2003-04-09 04:06:37 +000047Profile an \keyword{exec}-compatible string in the script environment.
48The globals from the \refmodule[main]{__main__} module are used as
Anthony Baxterb3303ef2003-04-07 12:19:15 +000049both the globals and locals for the script.
50\end{methoddesc}
51
52\begin{methoddesc}{runcall}{func, *args, **keywords}
53Profile a single call of a callable.
Anthony Baxterb3303ef2003-04-07 12:19:15 +000054Additional positional and keyword arguments may be passed
55along; the result of the call is returned, and exceptions are
56allowed to propogate cleanly, while ensuring that profiling is
57disabled on the way out.
58\end{methoddesc}
59
60
61\begin{methoddesc}{runctx}{cmd, globals, locals}
Fred Drakefcd845a2003-04-09 04:06:37 +000062Evaluate an \keyword{exec}-compatible string in a specific environment.
Anthony Baxterb3303ef2003-04-07 12:19:15 +000063The string is compiled before profiling begins.
64\end{methoddesc}
65
66\begin{methoddesc}{start}{}
67Start the profiler.
68\end{methoddesc}
69
70\begin{methoddesc}{stop}{}
71Stop the profiler.
72\end{methoddesc}
73
Anthony Baxterb3303ef2003-04-07 12:19:15 +000074
Fred Drakefcd845a2003-04-09 04:06:37 +000075\subsection{Using hotshot data}
76
77\declaremodule{standard}{hotshot.stats}
Anthony Baxterb3303ef2003-04-07 12:19:15 +000078\modulesynopsis{Statistical analysis for Hotshot}
79
80\versionadded{2.2}
81
Fred Drakefcd845a2003-04-09 04:06:37 +000082This module loads hotshot profiling data into the standard \module{pstats}
Anthony Baxterb3303ef2003-04-07 12:19:15 +000083Stats objects.
84
85\begin{funcdesc}{load}{filename}
Fred Drakefcd845a2003-04-09 04:06:37 +000086Load hotshot data from \var{filename}. Returns an instance
Anthony Baxterb3303ef2003-04-07 12:19:15 +000087of the \class{pstats.Stats} class.
88\end{funcdesc}
89
90\begin{seealso}
Fred Drakefcd845a2003-04-09 04:06:37 +000091 \seemodule{profile}{The \module{profile} module's \class{Stats} class}
Anthony Baxterb3303ef2003-04-07 12:19:15 +000092\end{seealso}
93
Fred Drakefcd845a2003-04-09 04:06:37 +000094
Anthony Baxterb3303ef2003-04-07 12:19:15 +000095\subsection{Example Usage \label{hotshot-example}}
96
Fred Drakefcd845a2003-04-09 04:06:37 +000097Note that this example runs the python ``benchmark'' pystones. It can
Anthony Baxtercb8ed532003-04-07 12:21:56 +000098take some time to run, and will produce large output files.
99
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000100\begin{verbatim}
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000101>>> 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 Baxterb3303ef2003-04-07 12:19:15 +0000120\end{verbatim}