Previous: Metric Watchers, Up: Metrics


8.12.4 Metric Helpers

countMethod(name)
A function decorator that counts how many times the function is called.
          from buildbot.process.metrics import countMethod
          
          @countMethod('foo_called')
          def foo():
              return "foo!"

Timer(name)
Timer objects can be used to make timing events easier. When Timer.stop() is called, a MetricTimeEvent is logged with the elapsed time since timer.start() was called.
          from buildbot.process.metrics import Timer
          
          def foo():
              t = Timer('time_foo')
              t.start()
              try:
                  for i in range(1000):
                      calc(i)
                  return "foo!"
              finally:
                  t.stop()

Timer objects also provide a pair of decorators, startTimer/stopTimer to decorate other functions.

          from buildbot.process.metrics import Timer
          
          t = Timer('time_thing')
          
          @t.startTimer
          def foo():
              return "foo!"
          
          @t.stopTimer
          def bar():
              return "bar!"
          
          foo()
          bar()

timeMethod(name)
A function decorator that measures how long a function takes to execute. Note that many functions in buildbot return deferreds, so may return before all the work they set up has completed. Using an explicit Timer is better in this case.
          from buildbot.process.metrics import timeMethod
          
          @timeMethod('time_foo')
          def foo():
              for i in range(1000):
                  calc(i)
              return "foo!"