1 from __future__
import absolute_import, division
23 """Utilities for measuring execution time.
32 __all__ = [
"logInfo",
"timeMethod"]
35 """!Log (name, value) pairs to obj.metadata and obj.log
37 @param obj a \ref task.Task "Task", or any other object with these two attributes:
38 * metadata an instance of lsst.daf.base.PropertyList (or other object with add(name, value) method)
39 * log an instance of lsst.pex.logging.Log
40 @param pairs a collection of (name, value) pairs
41 @param logLevel log level (an lsst.pex.logging.Log level constant, such as lsst.pex.logging.Log.DEBUG)
44 for name, value
in pairs:
46 obj.metadata.add(name, value)
48 obj.log.fatal(
"%s.metadata.add(name=%r, value=%r) failed with error=%s" % \
49 (type(obj).__name__, name, value, e))
50 strList.append(
"%s=%s" % (name, value))
51 obj.log.log(logLevel,
"; ".join(strList))
53 def logInfo(obj, prefix, logLevel=Log.DEBUG):
54 """!Log timer information to obj.metadata and obj.log
56 @param obj a \ref task.Task "Task", or any other object with these two attributes:
57 * metadata an instance of lsst.daf.base.PropertyList (or other object with add(name, value) method)
58 * log an instance of lsst.pex.logging.Log
59 @param prefix name prefix, the resulting entries are <prefix>CpuTime, etc.
60 For example timeMethod uses prefix = <methodName>Start
61 when the method begins and prefix = <methodName>End when the method ends.
62 @param logLevel log level (an lsst.pex.logging.Log level, constant such as lsst.pex.logging.Log.DEBUG)
66 * Utc: UTC date in ISO format (only in metadata since log entries have timestamps)
67 * CpuTime: CPU time (seconds)
68 * MaxRss: maximum resident set size
69 All logged resource information is only for the current process; child processes are excluded
71 cpuTime = time.clock()
72 utcStr = datetime.datetime.utcnow().isoformat()
73 res = resource.getrusage(resource.RUSAGE_SELF)
74 obj.metadata.add(name = prefix +
"Utc", value = utcStr)
77 (prefix +
"CpuTime", cpuTime),
78 (prefix +
"UserTime", res.ru_utime),
79 (prefix +
"SystemTime", res.ru_stime),
80 (prefix +
"MaxResidentSetSize", long(res.ru_maxrss)),
81 (prefix +
"MinorPageFaults", long(res.ru_minflt)),
82 (prefix +
"MajorPageFaults", long(res.ru_majflt)),
83 (prefix +
"BlockInputs", long(res.ru_inblock)),
84 (prefix +
"BlockOutputs", long(res.ru_oublock)),
85 (prefix +
"VoluntaryContextSwitches", long(res.ru_nvcsw)),
86 (prefix +
"InvoluntaryContextSwitches", long(res.ru_nivcsw)),
92 """!Decorator to measure duration of a task method
94 Writes various measures of time and possibly memory usage to the task's metadata;
95 all items are prefixed with the function name.
99 import lsst.pipe.base as pipeBase
100 class FooTask(pipeBase.Task):
104 def run(self, ...): # or any other instance method you want to time
108 @param func the method to wrap
110 @warning This decorator only works with instance methods of Task, or any class with these attributes:
111 * metadata: an instance of lsst.daf.base.PropertyList (or other object with add(name, value) method)
112 * log: an instance of lsst.pex.logging.Log
114 @functools.wraps(func)
115 def wrapper(self, *args, **keyArgs):
116 logInfo(obj = self, prefix = func.__name__ +
"Start")
118 res = func(self, *args, **keyArgs)
120 logInfo(obj = self, prefix = func.__name__ +
"End")
def logPairs
Log (name, value) pairs to obj.metadata and obj.log.
def logInfo
Log timer information to obj.metadata and obj.log.
def timeMethod
Decorator to measure duration of a task method.