instrumenting Python code

brueckd at tbye.com brueckd at tbye.com
Wed Mar 27 10:56:45 EST 2002


On Wed, 27 Mar 2002, Skip Montanaro wrote:

>     >> I was wondering if there is an easy way to instrument Python code
[snip]
> Or, in 2.2, the new hotshot module, which is a lot more efficient at
> profiling than the profile module.

The hotshot module doesn't appear to have any documentation yet. There is
a unit test that's kinda confusing; below is a simplification of how to
use the module:

import hotshot, os
import hotshot.log
from hotshot.log import ENTER, EXIT, LINE

WHAT_NAMES = {ENTER:'ENTER',EXIT:'EXIT',LINE:'LINE'}

def ProfIt(callable):
    outf = 'out.temp'
    prof = hotshot.Profile(outf, 1, 1)
    prof.runcall(callable)
    prof.close()

    log = hotshot.log.LogReader(outf)
    print 'What\tFunc (where)\ttDelta'
    for event in log:
        what, (filename, lineno, funcname), tdelta = event
        print '%s\t%s (%s:%d)\t%d' % (WHAT_NAMES[what], funcname,
                                      filename, lineno, tdelta)

    os.remove(outf)

def f():
    y = 2
    x = 1
def g():
    f()

ProfIt(g)

The output from this is:

What    Func (where)    tDelta
ENTER   g (hot.py:24)   59
LINE    g (hot.py:24)   12
LINE    g (hot.py:25)   7
ENTER   f (hot.py:21)   21
LINE    f (hot.py:21)   6
LINE    f (hot.py:22)   6
LINE    f (hot.py:23)   7
EXIT    f (hot.py:21)   8
EXIT    g (hot.py:24)   11

My question for Skip: is there no good way to profile a multithreaded
application?

-Dave





More information about the Python-list mailing list