Profiling test script execution (Python)

Last edited on

Using the profile module provided by Python it is extremely easy to perform profiling of a command:

import profile

def main():
    profile.run("_main()")

def _main():
    for i in range(10):
        test.log("%s" % i)
        snooze(0.2)
test.py

Result:

R:          25 function calls in 2.012 CPU seconds
R:    Ordered by: standard name
R:    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
R:        10    0.002    0.000    0.002    0.000 :0(log)
R:         1    0.000    0.000    0.000    0.000 :0(range)
R:         1    0.002    0.002    0.002    0.002 :0(setprofile)
R:        10    2.007    0.201    2.007    0.201 :0(snooze)
R:         1    0.000    0.000    2.010    2.010 <string>:1(?)
R:         1    0.000    0.000    2.012    2.012 profile:0(_main())
R:         0    0.000             0.000          profile:0(profiler)
R:         1    0.000    0.000    2.009    2.009 test.py:6(_main)
Profiling Result (output in Runner/Server Log view)

However, interpreting these results correctly may not be trivial. How to interpret the results in general is explained at The Python Profilers .