Context manager for profiling with cProfile.
- Parameters
-
filename | filename to which to write profile (profiling disabled if None or empty) |
log | log object for logging the profile operations |
If profiling is enabled, the context manager returns the cProfile.Profile object (otherwise it returns None), which allows additional control over profiling. You can obtain this using the "as" clause, e.g.:
with profile(filename) as prof:
runYourCodeHere()
The output cumulative profile can be printed with a command-line like:
python -c 'import pstats; pstats.Stats("<filename>").sort_stats("cumtime").print_stats(30)'
Definition at line 66 of file cmdLineTask.py.
67 def profile(filename, log=None):
68 """!Context manager for profiling with cProfile
70 @param filename filename to which to write profile (profiling disabled if None or empty)
71 @param log log object for logging the profile operations
73 If profiling is enabled, the context manager returns the cProfile.Profile object (otherwise
74 it returns None), which allows additional control over profiling. You can obtain this using
75 the "as" clause, e.g.:
77 with profile(filename) as prof:
80 The output cumulative profile can be printed with a command-line like:
82 python -c 'import pstats; pstats.Stats("<filename>").sort_stats("cumtime").print_stats(30)'
88 from cProfile
import Profile
91 log.info(
"Enabling cProfile profiling")
95 profile.dump_stats(filename)
97 log.info(
"cProfile stats written to %s" % filename)
def profile
Context manager for profiling with cProfile.