22 """Utilities for measuring execution time. 24 from __future__
import absolute_import, division
30 from lsst.log
import Log, log
32 __all__ = [
"logInfo",
"timeMethod"]
36 """!Log (name, value) pairs to obj.metadata and obj.log 38 @param obj a \ref task.Task "Task", or any other object with these two attributes: 39 * metadata an instance of lsst.daf.base.PropertyList (or other object with add(name, value) method) 40 * log an instance of lsst.log.Log 41 @param pairs a collection of (name, value) pairs 42 @param logLevel log level (an lsst.log level constant, such as lsst.log.Log.DEBUG) 45 for name, value
in pairs:
47 obj.metadata.add(name, value)
48 except Exception
as e:
49 obj.log.fatal(
"%s.metadata.add(name=%r, value=%r) failed with error=%s",
50 type(obj).__name__, name, value, e)
51 strList.append(
"%s=%s" % (name, value))
52 log(obj.log.getName(), logLevel,
"; ".join(strList))
55 def logInfo(obj, prefix, logLevel=Log.DEBUG):
56 """!Log timer information to obj.metadata and obj.log 58 @param obj a \ref task.Task "Task", or any other object with these two attributes: 59 * metadata an instance of lsst.daf.base.PropertyList (or other object with add(name, value) method) 60 * log an instance of lsst.log.Log 61 @param prefix name prefix, the resulting entries are <prefix>CpuTime, etc. 62 For example timeMethod uses prefix = <methodName>Start 63 when the method begins and prefix = <methodName>End when the method ends. 64 @param logLevel log level (an lsst.log level, constant such as lsst.log.Log.DEBUG) 68 * Utc: UTC date in ISO format (only in metadata since log entries have timestamps) 69 * CpuTime: CPU time (seconds) 70 * MaxRss: maximum resident set size 71 All logged resource information is only for the current process; child processes are excluded 73 cpuTime = time.clock()
74 utcStr = datetime.datetime.utcnow().isoformat()
75 res = resource.getrusage(resource.RUSAGE_SELF)
76 obj.metadata.add(name=prefix +
"Utc", value=utcStr)
79 (prefix +
"CpuTime", cpuTime),
80 (prefix +
"UserTime", res.ru_utime),
81 (prefix +
"SystemTime", res.ru_stime),
82 (prefix +
"MaxResidentSetSize", int(res.ru_maxrss)),
83 (prefix +
"MinorPageFaults", int(res.ru_minflt)),
84 (prefix +
"MajorPageFaults", int(res.ru_majflt)),
85 (prefix +
"BlockInputs", int(res.ru_inblock)),
86 (prefix +
"BlockOutputs", int(res.ru_oublock)),
87 (prefix +
"VoluntaryContextSwitches", int(res.ru_nvcsw)),
88 (prefix +
"InvoluntaryContextSwitches", int(res.ru_nivcsw)),
95 """!Decorator to measure duration of a task method 97 Writes various measures of time and possibly memory usage to the task's metadata; 98 all items are prefixed with the function name. 102 import lsst.pipe.base as pipeBase 103 class FooTask(pipeBase.Task): 107 def run(self, ...): # or any other instance method you want to time 111 @param func the method to wrap 113 @warning This decorator only works with instance methods of Task, or any class with these attributes: 114 * metadata: an instance of lsst.daf.base.PropertyList (or other object with add(name, value) method) 115 * log: an instance of lsst.log.Log 117 @functools.wraps(func)
118 def wrapper(self, *args, **keyArgs):
119 logInfo(obj=self, prefix=func.__name__ +
"Start")
121 res = func(self, *args, **keyArgs)
123 logInfo(obj=self, prefix=func.__name__ +
"End")
def logInfo(obj, prefix, logLevel=Log.DEBUG)
Log timer information to obj.metadata and obj.log.
def logPairs(obj, pairs, logLevel=Log.DEBUG)
Log (name, value) pairs to obj.metadata and obj.log.
def timeMethod(func)
Decorator to measure duration of a task method.