Context manager for profiling with cProfile.
Parameters
----------
filename : `str`
Filename to which to write profile (profiling disabled if `None` or empty).
log : `lsst.log.Log`, optional
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 49 of file cmdLineTask.py.
49 def profile(filename, log=None):
50 """Context manager for profiling with cProfile.
56 Filename to which to write profile (profiling disabled if `None` or empty).
57 log : `lsst.log.Log`, optional
58 Log object for logging the profile operations.
60 If profiling is enabled, the context manager returns the cProfile.Profile object (otherwise
61 it returns None), which allows additional control over profiling. You can obtain this using
62 the "as" clause, e.g.:
64 with profile(filename) as prof:
67 The output cumulative profile can be printed with a command-line like::
69 python -c 'import pstats; pstats.Stats("<filename>").sort_stats("cumtime").print_stats(30)'
75 from cProfile
import Profile
78 log.info(
"Enabling cProfile profiling")
82 profile.dump_stats(filename)
84 log.info(
"cProfile stats written to %s", filename)