blob: 2bb5bce39771c7e0ec11b9898dd31d92f2d91e3b [file] [log] [blame]
Anthony Baxterb3303ef2003-04-07 12:19:15 +00001\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
14This module provides a nicer interface to the \code{_hotshot} C module.
15Hotshot is a replacement for the existing \refmodule{profile} module. As it's
16written mostly in C, it should result in a much smaller performance impact
17than the existing profile module.
18
19\begin{classdesc}{Profile}{logfile, \optional{, lineevents=0, linetimings=1}}
20
21The profiler object. The argument \var{logfile} is the name of a log file
22to use for logged profile data. The argument \var{lineevents} specifies whether
23to generate events for every source line, or just on function call/return. It
24defaults to 0 (only log function call/return). The argument \var{linetimings}
25specifies whether to record timing information. It defaults to 1 (store timing
26information).
27
28\end{classdesc}
29
30\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}
41
42%
43\begin{methoddesc}{fileno}{}
44Return the file descriptor of the profiler's log file.
45\end{methoddesc}
46
47\begin{methoddesc}{run}{cmd}
48Profile an exec-compatible string in the script environment.
49
50The globals from the \module{__main__} module are used as
51both the globals and locals for the script.
52\end{methoddesc}
53
54\begin{methoddesc}{runcall}{func, *args, **keywords}
55Profile a single call of a callable.
56
57Additional positional and keyword arguments may be passed
58along; the result of the call is returned, and exceptions are
59allowed to propogate cleanly, while ensuring that profiling is
60disabled on the way out.
61\end{methoddesc}
62
63
64\begin{methoddesc}{runctx}{cmd, globals, locals}
65Evaluate an exec-compatible string in a specific environment.
66
67The string is compiled before profiling begins.
68\end{methoddesc}
69
70\begin{methoddesc}{start}{}
71Start the profiler.
72\end{methoddesc}
73
74\begin{methoddesc}{stop}{}
75Stop 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
85This module loads hotshot profiling data into the standard \module{pstats}
86Stats objects.
87
88\begin{funcdesc}{load}{filename}
89Load hotshot data from \var{filename}. Returns an instance
90of 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
Anthony Baxtercb8ed532003-04-07 12:21:56 +000099Note that this example runs the python "benchmark" pystones. It can
100take some time to run, and will produce large output files.
101
Anthony Baxterb3303ef2003-04-07 12:19:15 +0000102\begin{verbatim}
103
104>>> import hotshot, hotshot.stats, test.pystone
105>>> prof = hotshot.Profile("stones.prof")
106>>> benchtime, stones = prof.runcall(test.pystone.pystones)
107>>> prof.close()
108>>> stats = hotshot.stats.load("stones.prof")
109>>> stats.strip_dirs()
110>>> stats.sort_stats('time', 'calls')
111>>> stats.print_stats(20)
112 850004 function calls in 10.090 CPU seconds
113
114 Ordered by: internal time, call count
115
116 ncalls tottime percall cumtime percall filename:lineno(function)
117 1 3.295 3.295 10.090 10.090 pystone.py:79(Proc0)
118 150000 1.315 0.000 1.315 0.000 pystone.py:203(Proc7)
119 50000 1.313 0.000 1.463 0.000 pystone.py:229(Func2)
120 .
121 .
122 .
123
124\end{verbatim}
125
126