ó [Z-Lc@s dZyddlZWnek r5edƒ‚nXddlZddlZddddgZd„Zd efd „ƒYZeƒZ iZ d efd „ƒYZ d „Z d„Z d„Zd„Zd„Zd„Zdd„Zdefd„ƒYZd„ZdS(s\ statprof is intended to be a fairly simple statistical profiler for python. It was ported directly from a statistical profiler for guile, also named statprof, available from guile-lib [0]. [0] http://wingolog.org/software/guile-lib/statprof/ To start profiling, call statprof.start(): >>> start() Then run whatever it is that you want to profile, for example: >>> import test.pystone; test.pystone.pystones() Then stop the profiling and print out the results: >>> stop() >>> display() % cumulative self time seconds seconds name 26.72 1.40 0.37 pystone.py:79:Proc0 13.79 0.56 0.19 pystone.py:133:Proc1 13.79 0.19 0.19 pystone.py:208:Proc8 10.34 0.16 0.14 pystone.py:229:Func2 6.90 0.10 0.10 pystone.py:45:__init__ 4.31 0.16 0.06 pystone.py:53:copy ... All of the numerical data with the exception of the calls column is statistically approximate. In the following column descriptions, and in all of statprof, "time" refers to execution time (both user and system), not wall clock time. % time The percent of the time spent inside the procedure itself (not counting children). cumulative seconds The total number of seconds spent in the procedure, including children. self seconds The total number of seconds spent in the procedure itself (not counting children). name The name of the procedure. By default statprof keeps the data collected from previous runs. If you want to clear the collected data, call reset(): >>> reset() reset() can also be used to change the sampling frequency. For example, to tell statprof to sample 50 times a second: >>> reset(50) This means that statprof will sample the call stack after every 1/50 of a second of user + system time spent running on behalf of the python process. When your process is idle (for example, blocking in a read(), as is the case at the listener), the clock does not advance. For this reason statprof is not currently not suitable for profiling io-bound operations. The profiler uses the hash of the code object itself to identify the procedures, so it won't confuse different procedures with the same name. They will show up as two different rows in the output. Right now the profiler is quite simplistic. I cannot provide call-graphs or other higher level information. What you see in the table is pretty much all there is. Patches are welcome :-) Threading --------- Because signals only get delivered to the main thread in Python, statprof only profiles the main thread. However because the time reporting function uses per-process timers, the results can be significantly off if other threads' work patterns are not similar to the main thread's work patterns. Implementation notes -------------------- The profiler works by setting the unix profiling signal ITIMER_PROF to go off after the interval you define in the call to reset(). When the signal fires, a sampling routine is run which looks at the current procedure that's executing, and then crawls up the stack, and for each frame encountered, increments that frame's code object's sample count. Note that if a procedure is encountered multiple times on a given stack, it is only counted once. After the sampling is complete, the profiler resets profiling timer to fire again after the appropriate interval. Meanwhile, the profiler keeps track, via os.times(), how much CPU time (system and user -- which is also what ITIMER_PROF tracks), has elapsed while code has been executing within a start()/stop() block. The profiler also tries to avoid counting or timing its own code as much as possible. iÿÿÿÿNsìstatprof requires the itimer python extension. To install it, enter the following commands from a terminal: wget http://www.cute.fi/~torppa/py-itimer/py-itimer.tar.gz tar zxvf py-itimer.tar.gz cd py-itimer sudo python setup.py install tstarttstoptresettdisplaycCstjƒ}|d|dS(Nii(tosttimes(R((sC/opt/freeware/lib/python2.7/site-packages/Cheetah/Utils/statprof.pytclock“s t ProfileStatecBs)eZdd„Zdd„Zd„ZRS(cCs|j|ƒdS(N(R(tselft frequency((sC/opt/freeware/lib/python2.7/site-packages/Cheetah/Utils/statprof.pyt__init__œscCsxd|_d|_d|_|r1d||_nt|dƒsPdd|_nd|_d|_t|_ d|_ dS(Ngigð?tsample_intervalgY@( taccumulated_timetNonetlast_start_timet sample_countR thasattrtremaining_prof_timet profile_leveltFalset count_callst gc_time_taken(RR ((sC/opt/freeware/lib/python2.7/site-packages/Cheetah/Utils/statprof.pyRŸs      cCs|j||j7_dS(N(R R(Rt stop_time((sC/opt/freeware/lib/python2.7/site-packages/Cheetah/Utils/statprof.pytaccumulate_time·sN(t__name__t __module__R R RR(((sC/opt/freeware/lib/python2.7/site-packages/Cheetah/Utils/statprof.pyR›s  tCallDatacBseZd„ZRS(cCsM|j|_|j|_|j|_d|_d|_d|_|t |((sC/opt/freeware/lib/python2.7/site-packages/Cheetah/Utils/statprof.pyR s        cCs%d|j|j|j|jfGHdS(Ns%6.2f %9.2f %9.2f %s(R?R@RAR(R((sC/opt/freeware/lib/python2.7/site-packages/Cheetah/Utils/statprof.pyRs (RRR R(((sC/opt/freeware/lib/python2.7/site-packages/Cheetah/Utils/statprof.pyR<s cCsàtjdkrdGHdSgtjƒD]}t|ƒ^q%}g|D]}|j|j|f^qD}|jdtƒg|D]}|d^q|}ddGHd dGHx|D]}|j ƒq«WdGHdtjGHdtj GHdS(NisNo samples recorded.treverseis%5.5s %10.10s %7.7s %-8.8ss% t cumulativeRts%5.5s %9.9s %8.8s %-8.8sttimetsecondsRs---sSample count: %dsTotal time: %f seconds(s% RJsselfRK(stimeRMRMsname( R(RR$t itervaluesR<RAR@tsortR*RR (txtl((sC/opt/freeware/lib/python2.7/site-packages/Cheetah/Utils/statprof.pyRs%(    (t__doc__R0t ImportErrorR6Rt__all__RtobjectRR(R$RR'R/R4R5RRR RR<R(((sC/opt/freeware/lib/python2.7/site-packages/Cheetah/Utils/statprof.pytxs,