1 | \section{\module{hotshot} ---
|
---|
2 | High performance logging profiler}
|
---|
3 |
|
---|
4 | \declaremodule{standard}{hotshot}
|
---|
5 | \modulesynopsis{High performance logging profiler, mostly written in C.}
|
---|
6 | \moduleauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
|
---|
7 | \sectionauthor{Anthony Baxter}{anthony@interlink.com.au}
|
---|
8 |
|
---|
9 | \versionadded{2.2}
|
---|
10 |
|
---|
11 |
|
---|
12 | This module provides a nicer interface to the \module{_hotshot} C module.
|
---|
13 | Hotshot is a replacement for the existing \refmodule{profile} module. As it's
|
---|
14 | written mostly in C, it should result in a much smaller performance impact
|
---|
15 | than the existing \refmodule{profile} module.
|
---|
16 |
|
---|
17 | \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
|
---|
26 | past: the timing core contained a critical bug]{2.5}
|
---|
27 |
|
---|
28 | \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}}}
|
---|
37 | The profiler object. The argument \var{logfile} is the name of a log
|
---|
38 | file to use for logged profile data. The argument \var{lineevents}
|
---|
39 | specifies whether to generate events for every source line, or just on
|
---|
40 | function call/return. It defaults to \code{0} (only log function
|
---|
41 | call/return). The argument \var{linetimings} specifies whether to
|
---|
42 | record timing information. It defaults to \code{1} (store timing
|
---|
43 | information).
|
---|
44 | \end{classdesc}
|
---|
45 |
|
---|
46 |
|
---|
47 | \subsection{Profile Objects \label{hotshot-objects}}
|
---|
48 |
|
---|
49 | Profile objects have the following methods:
|
---|
50 |
|
---|
51 | \begin{methoddesc}{addinfo}{key, value}
|
---|
52 | Add an arbitrary labelled value to the profile output.
|
---|
53 | \end{methoddesc}
|
---|
54 |
|
---|
55 | \begin{methoddesc}{close}{}
|
---|
56 | Close the logfile and terminate the profiler.
|
---|
57 | \end{methoddesc}
|
---|
58 |
|
---|
59 | \begin{methoddesc}{fileno}{}
|
---|
60 | Return the file descriptor of the profiler's log file.
|
---|
61 | \end{methoddesc}
|
---|
62 |
|
---|
63 | \begin{methoddesc}{run}{cmd}
|
---|
64 | Profile an \keyword{exec}-compatible string in the script environment.
|
---|
65 | The globals from the \refmodule[main]{__main__} module are used as
|
---|
66 | both the globals and locals for the script.
|
---|
67 | \end{methoddesc}
|
---|
68 |
|
---|
69 | \begin{methoddesc}{runcall}{func, *args, **keywords}
|
---|
70 | Profile a single call of a callable.
|
---|
71 | Additional positional and keyword arguments may be passed
|
---|
72 | along; the result of the call is returned, and exceptions are
|
---|
73 | allowed to propagate cleanly, while ensuring that profiling is
|
---|
74 | disabled on the way out.
|
---|
75 | \end{methoddesc}
|
---|
76 |
|
---|
77 |
|
---|
78 | \begin{methoddesc}{runctx}{cmd, globals, locals}
|
---|
79 | Evaluate an \keyword{exec}-compatible string in a specific environment.
|
---|
80 | The string is compiled before profiling begins.
|
---|
81 | \end{methoddesc}
|
---|
82 |
|
---|
83 | \begin{methoddesc}{start}{}
|
---|
84 | Start the profiler.
|
---|
85 | \end{methoddesc}
|
---|
86 |
|
---|
87 | \begin{methoddesc}{stop}{}
|
---|
88 | Stop the profiler.
|
---|
89 | \end{methoddesc}
|
---|
90 |
|
---|
91 |
|
---|
92 | \subsection{Using hotshot data}
|
---|
93 |
|
---|
94 | \declaremodule{standard}{hotshot.stats}
|
---|
95 | \modulesynopsis{Statistical analysis for Hotshot}
|
---|
96 |
|
---|
97 | \versionadded{2.2}
|
---|
98 |
|
---|
99 | This module loads hotshot profiling data into the standard \module{pstats}
|
---|
100 | Stats objects.
|
---|
101 |
|
---|
102 | \begin{funcdesc}{load}{filename}
|
---|
103 | Load hotshot data from \var{filename}. Returns an instance
|
---|
104 | of the \class{pstats.Stats} class.
|
---|
105 | \end{funcdesc}
|
---|
106 |
|
---|
107 | \begin{seealso}
|
---|
108 | \seemodule{profile}{The \module{profile} module's \class{Stats} class}
|
---|
109 | \end{seealso}
|
---|
110 |
|
---|
111 |
|
---|
112 | \subsection{Example Usage \label{hotshot-example}}
|
---|
113 |
|
---|
114 | Note that this example runs the python ``benchmark'' pystones. It can
|
---|
115 | take some time to run, and will produce large output files.
|
---|
116 |
|
---|
117 | \begin{verbatim}
|
---|
118 | >>> 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 | .
|
---|
137 | \end{verbatim}
|
---|