22 """Utilities for measuring execution time.
24 __all__ = [
"logInfo",
"timeMethod"]
35 """Log ``(name, value)`` pairs to ``obj.metadata`` and ``obj.log``
39 obj : `lsst.pipe.base.Task`-type
40 A `~lsst.pipe.base.Task` or any other object with these two attributes:
42 - ``metadata`` an instance of `lsst.daf.base.PropertyList`` (or other
43 object with ``add(name, value)`` method).
44 - ``log`` an instance of `lsst.log.Log`.
47 A sequence of ``(name, value)`` pairs, with value typically numeric.
49 Log level (an `lsst.log` level constant, such as `lsst.log.Log.DEBUG`).
52 for name, value
in pairs:
56 obj.metadata.addLongLong(name, value)
58 obj.metadata.add(name, value)
59 strList.append(f
"{name}={value}")
60 log(
"timer." + obj.log.getName(), logLevel,
"; ".join(strList))
63 def logInfo(obj, prefix, logLevel=Log.DEBUG):
64 """Log timer information to ``obj.metadata`` and ``obj.log``.
68 obj : `lsst.pipe.base.Task`-type
69 A `~lsst.pipe.base.Task` or any other object with these two attributes:
71 - ``metadata`` an instance of `lsst.daf.base.PropertyList`` (or other
72 object with ``add(name, value)`` method).
73 - ``log`` an instance of `lsst.log.Log`.
76 Name prefix, the resulting entries are ``CpuTime``, etc.. For example
77 timeMethod uses ``prefix = Start`` when the method begins and
78 ``prefix = End`` when the method ends.
80 Log level (an `lsst.log` level constant, such as `lsst.log.Log.DEBUG`).
86 - ``Utc``: UTC date in ISO format (only in metadata since log entries have
88 - ``CpuTime``: System + User CPU time (seconds). This should only be used
89 in differential measurements; the time reference point is undefined.
90 - ``MaxRss``: maximum resident set size.
92 All logged resource information is only for the current process; child
93 processes are excluded.
95 cpuTime = time.process_time()
96 utcStr = datetime.datetime.utcnow().isoformat()
97 res = resource.getrusage(resource.RUSAGE_SELF)
98 obj.metadata.add(name=prefix +
"Utc", value=utcStr)
101 (prefix +
"CpuTime", cpuTime),
102 (prefix +
"UserTime", res.ru_utime),
103 (prefix +
"SystemTime", res.ru_stime),
104 (prefix +
"MaxResidentSetSize", int(res.ru_maxrss)),
105 (prefix +
"MinorPageFaults", int(res.ru_minflt)),
106 (prefix +
"MajorPageFaults", int(res.ru_majflt)),
107 (prefix +
"BlockInputs", int(res.ru_inblock)),
108 (prefix +
"BlockOutputs", int(res.ru_oublock)),
109 (prefix +
"VoluntaryContextSwitches", int(res.ru_nvcsw)),
110 (prefix +
"InvoluntaryContextSwitches", int(res.ru_nivcsw)),
117 """Decorator to measure duration of a task method.
126 Writes various measures of time and possibly memory usage to the task's
127 metadata; all items are prefixed with the function name.
131 This decorator only works with instance methods of Task, or any class
132 with these attributes:
134 - ``metadata``: an instance of `lsst.daf.base.PropertyList` (or other
135 object with ``add(name, value)`` method).
136 - ``log``: an instance of `lsst.log.Log`.
142 .. code-block:: python
144 import lsst.pipe.base as pipeBase
145 class FooTask(pipeBase.Task):
149 def run(self, ...): # or any other instance method you want to time
153 @functools.wraps(func)
154 def wrapper(self, *args, **keyArgs):
155 logInfo(obj=self, prefix=func.__name__ +
"Start")
157 res = func(self, *args, **keyArgs)
159 logInfo(obj=self, prefix=func.__name__ +
"End")
def logPairs(obj, pairs, logLevel=Log.DEBUG)
def logInfo(obj, prefix, logLevel=Log.DEBUG)