LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Functions | Variables
lsst.pipe.base.timer Namespace Reference

Functions

def logPairs
 Log (name, value) pairs to obj.metadata and obj.log. More...
 
def logInfo
 Log timer information to obj.metadata and obj.log. More...
 
def timeMethod
 Decorator to measure duration of a task method. More...
 

Variables

list __all__ = ["logInfo", "timeMethod"]
 

Function Documentation

def lsst.pipe.base.timer.logInfo (   obj,
  prefix,
  logLevel = Log.DEBUG 
)

Log timer information to obj.metadata and obj.log.

Parameters
obja Task, or any other object with these two attributes:
  • metadata an instance of lsst.daf.base.PropertyList (or other object with add(name, value) method)
  • log an instance of lsst.pex.logging.Log
prefixname prefix, the resulting entries are <prefix>CpuTime, etc. For example timeMethod uses prefix = <methodName>Start when the method begins and prefix = <methodName>End when the method ends.
logLevellog level (an lsst.pex.logging.Log level, constant such as lsst.pex.logging.Log.DEBUG)

Logged items include:

  • Utc: UTC date in ISO format (only in metadata since log entries have timestamps)
  • CpuTime: CPU time (seconds)
  • MaxRss: maximum resident set size All logged resource information is only for the current process; child processes are excluded

Definition at line 53 of file timer.py.

53 
54 def logInfo(obj, prefix, logLevel=Log.DEBUG):
55  """!Log timer information to obj.metadata and obj.log
56 
57  @param obj a \ref task.Task "Task", or any other object with these two attributes:
58  * metadata an instance of lsst.daf.base.PropertyList (or other object with add(name, value) method)
59  * log an instance of lsst.pex.logging.Log
60  @param prefix name prefix, the resulting entries are <prefix>CpuTime, etc.
61  For example timeMethod uses prefix = <methodName>Start
62  when the method begins and prefix = <methodName>End when the method ends.
63  @param logLevel log level (an lsst.pex.logging.Log level, constant such as lsst.pex.logging.Log.DEBUG)
64 
65 
66  Logged items include:
67  * Utc: UTC date in ISO format (only in metadata since log entries have timestamps)
68  * CpuTime: CPU time (seconds)
69  * MaxRss: maximum resident set size
70  All logged resource information is only for the current process; child processes are excluded
71  """
72  cpuTime = time.clock()
73  utcStr = datetime.datetime.utcnow().isoformat()
74  res = resource.getrusage(resource.RUSAGE_SELF)
75  obj.metadata.add(name = prefix + "Utc", value = utcStr) # log messages already have timestamps
76  logPairs(obj = obj,
77  pairs = [
78  (prefix + "CpuTime", cpuTime),
79  (prefix + "UserTime", res.ru_utime),
80  (prefix + "SystemTime", res.ru_stime),
81  (prefix + "MaxResidentSetSize", long(res.ru_maxrss)),
82  (prefix + "MinorPageFaults", long(res.ru_minflt)),
83  (prefix + "MajorPageFaults", long(res.ru_majflt)),
84  (prefix + "BlockInputs", long(res.ru_inblock)),
85  (prefix + "BlockOutputs", long(res.ru_oublock)),
86  (prefix + "VoluntaryContextSwitches", long(res.ru_nvcsw)),
87  (prefix + "InvoluntaryContextSwitches", long(res.ru_nivcsw)),
88  ],
89  logLevel = logLevel,
90  )
def logPairs
Log (name, value) pairs to obj.metadata and obj.log.
Definition: timer.py:34
def logInfo
Log timer information to obj.metadata and obj.log.
Definition: timer.py:53
def lsst.pipe.base.timer.logPairs (   obj,
  pairs,
  logLevel = Log.DEBUG 
)

Log (name, value) pairs to obj.metadata and obj.log.

Parameters
obja Task, or any other object with these two attributes:
  • metadata an instance of lsst.daf.base.PropertyList (or other object with add(name, value) method)
  • log an instance of lsst.pex.logging.Log
pairsa collection of (name, value) pairs
logLevellog level (an lsst.pex.logging.Log level constant, such as lsst.pex.logging.Log.DEBUG)

Definition at line 34 of file timer.py.

34 
35 def logPairs(obj, pairs, logLevel=Log.DEBUG):
36  """!Log (name, value) pairs to obj.metadata and obj.log
37 
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.pex.logging.Log
41  @param pairs a collection of (name, value) pairs
42  @param logLevel log level (an lsst.pex.logging.Log level constant, such as lsst.pex.logging.Log.DEBUG)
43  """
44  strList = []
45  for name, value in pairs:
46  try:
47  obj.metadata.add(name, value)
48  except Exception, 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  obj.log.log(logLevel, "; ".join(strList))
def logPairs
Log (name, value) pairs to obj.metadata and obj.log.
Definition: timer.py:34
def lsst.pipe.base.timer.timeMethod (   func)

Decorator to measure duration of a task method.

Writes various measures of time and possibly memory usage to the task's metadata; all items are prefixed with the function name.

To use:

1 import lsst.pipe.base as pipeBase
2 class FooTask(pipeBase.Task):
3  ...
4 
5  @pipeBase.timeMethod
6  def run(self, ...): # or any other instance method you want to time
7  ...
Parameters
functhe method to wrap
Warning
This decorator only works with instance methods of Task, or any class with these attributes:
  • metadata: an instance of lsst.daf.base.PropertyList (or other object with add(name, value) method)
  • log: an instance of lsst.pex.logging.Log

Definition at line 91 of file timer.py.

91 
92 def timeMethod(func):
93  """!Decorator to measure duration of a task method
94 
95  Writes various measures of time and possibly memory usage to the task's metadata;
96  all items are prefixed with the function name.
97 
98  To use:
99  \code
100  import lsst.pipe.base as pipeBase
101  class FooTask(pipeBase.Task):
102  ...
103 
104  @pipeBase.timeMethod
105  def run(self, ...): # or any other instance method you want to time
106  ...
107  \endcode
108 
109  @param func the method to wrap
110 
111  @warning This decorator only works with instance methods of Task, or any class with these attributes:
112  * metadata: an instance of lsst.daf.base.PropertyList (or other object with add(name, value) method)
113  * log: an instance of lsst.pex.logging.Log
114  """
115  @functools.wraps(func)
116  def wrapper(self, *args, **keyArgs):
117  logInfo(obj = self, prefix = func.__name__ + "Start")
118  try:
119  res = func(self, *args, **keyArgs)
120  finally:
121  logInfo(obj = self, prefix = func.__name__ + "End")
122  return res
123  return wrapper
def logInfo
Log timer information to obj.metadata and obj.log.
Definition: timer.py:53
def timeMethod
Decorator to measure duration of a task method.
Definition: timer.py:91

Variable Documentation

list lsst.pipe.base.timer.__all__ = ["logInfo", "timeMethod"]

Definition at line 32 of file timer.py.