class UtilizationTimer(Timer): def __init__(self, histogram=HistogramExponentiallyDecaying): super(UtilizationTimer, self).__init__(histogram) self.duration_meter = Meter() def clear(self): super(UtilizationTimer, self).clear() self.duration_meter.clear() def update(self, duration): super(UtilizationTimer, self).update(duration) if duration >= 0: self.duration_meter.mark(duration) @property def one_minute_utilization(self): return self.duration_meter.one_minute_rate @property def five_minute_utilization(self): return self.duration_meter.five_minute_rate @property def fifteen_minute_utilization(self): return self.duration_meter.fifteen_minute_rate @property def mean_utilization(self): return self.duration_meter.mean_rate def stop(self): super(UtilizationTimer, self).stop() self.duration_meter.stop()
class UtilizationTimer(Timer): """ A specialized timer that calculates the percentage of wall-clock time that was spent :: utimer = Metrology.utilization_timer('responses') with utimer: do_something() """ def __init__(self, histogram=HistogramExponentiallyDecaying): super(UtilizationTimer, self).__init__(histogram) self.duration_meter = Meter() def clear(self): super(UtilizationTimer, self).clear() self.duration_meter.clear() def update(self, duration): super(UtilizationTimer, self).update(duration) if duration >= 0: self.duration_meter.mark(duration) @property def one_minute_utilization(self): """Returns the one-minute average utilization as a percentage.""" return self.duration_meter.one_minute_rate @property def five_minute_utilization(self): """Returns the five-minute average utilization as a percentage.""" return self.duration_meter.five_minute_rate @property def fifteen_minute_utilization(self): """Returns the fifteen-minute average utilization as a percentage.""" return self.duration_meter.fifteen_minute_rate @property def mean_utilization(self): """ Returns the mean (average) utilization as a percentage since the process started. """ return self.duration_meter.mean_rate def stop(self): super(UtilizationTimer, self).stop() self.duration_meter.stop()
class Timer(object): """ A timer measures both the rate that a particular piece of code is called and the distribution of its duration :: timer = Metrology.timer('responses') with timer: do_something() """ def __init__(self, histogram=HistogramExponentiallyDecaying): self.meter = Meter() self.histogram = histogram() def __call__(self, *args, **kwargs): if args and hasattr(args[0], '__call__'): _orig_func = args[0] def _decorator(*args, **kwargs): with self: _orig_func(*args, **kwargs) return _decorator def clear(self): self.meter.clear() self.histogram.clear() def update(self, duration): """Records the duration of an operation.""" if duration >= 0: self.meter.mark() self.histogram.update(duration) @property def snapshot(self): return self.histogram.snapshot def __enter__(self): self.interval = Interval.now() return self def __exit__(self, type, value, callback): duration = self.interval.stop() self.update(duration) @property def total_time(self): """Returns the total time spent.""" return self.histogram.total @property def count(self): """Returns the number of measurements that have been made.""" return self.histogram.count @property def one_minute_rate(self): """Returns the one-minute average rate.""" return self.meter.one_minute_rate @property def five_minute_rate(self): """Returns the five-minute average rate.""" return self.meter.five_minute_rate @property def fifteen_minute_rate(self): """Returns the fifteen-minute average rate.""" return self.meter.fifteen_minute_rate @property def mean_rate(self): """Returns the mean rate of the events since the start of the process.""" return self.meter.mean_rate @property def min(self): """Returns the minimum amount of time spent in the operation.""" return self.histogram.min @property def max(self): """Returns the maximum amount of time spent in the operation.""" return self.histogram.max @property def mean(self): """Returns the mean time spent in the operation.""" return self.histogram.mean @property def stddev(self): """Returns the standard deviation of the mean spent in the operation.""" return self.histogram.stddev def stop(self): self.meter.stop()
def __init__(self, histogram=HistogramExponentiallyDecaying): self.meter = Meter() self.histogram = histogram()
def __init__(self, histogram=HistogramExponentiallyDecaying): super(UtilizationTimer, self).__init__(histogram) self.duration_meter = Meter()
def setUp(self): self.meter = Meter()
class MeterTest(TestCase): def setUp(self): self.meter = Meter() def test_meter(self): self.meter.mark() self.assertEqual(1, self.meter.count) def test_blank_meter(self): self.assertEqual(0, self.meter.count) self.assertEqual(0.0, self.meter.mean_rate) def test_meter_value(self): self.meter.mark(3) self.assertEqual(3, self.meter.count) def test_one_minute_rate(self): self.meter.mark(1000) self.meter.tick() self.assertEqual(200, self.meter.one_minute_rate) def test_meter_threaded(self): def mark(): for i in range(100): self.meter.mark() for thread in [Thread(target=mark) for i in range(10)]: thread.start() thread.join() self.assertEqual(1000, self.meter.count) def test_meter_decorator(self): expected_return_value = 'meter' @self.meter def _test_decorator(): return expected_return_value for i in range(500): self.assertEqual(expected_return_value, _test_decorator()) self.assertEqual(500, self.meter.count) def test_meter_context_manager(self): for i in range(275): with self.meter: pass self.assertEqual(275, self.meter.count) def tearDown(self): self.meter.stop()
class Timer(object): """ A timer measures both the rate that a particular piece of code is called and the distribution of its duration :: timer = Metrology.timer('responses') with timer: do_something() """ def __init__(self, histogram=HistogramExponentiallyDecaying): self.meter = Meter() self.histogram = histogram() def __call__(self, *args, **kwargs): if args and hasattr(args[0], '__call__'): _orig_func = args[0] def _decorator(*args, **kwargs): with self: return _orig_func(*args, **kwargs) return _decorator def clear(self): self.meter.clear() self.histogram.clear() def update(self, duration): """Records the duration of an operation.""" if duration >= 0: self.meter.mark() self.histogram.update(duration) @property def snapshot(self): return self.histogram.snapshot def __enter__(self): self.interval = Interval.now() return self def __exit__(self, type, value, callback): duration = self.interval.stop() self.update(duration) @property def total_time(self): """Returns the total time spent.""" return self.histogram.total @property def count(self): """Returns the number of measurements that have been made.""" return self.histogram.count @property def one_minute_rate(self): """Returns the one-minute average rate.""" return self.meter.one_minute_rate @property def five_minute_rate(self): """Returns the five-minute average rate.""" return self.meter.five_minute_rate @property def fifteen_minute_rate(self): """Returns the fifteen-minute average rate.""" return self.meter.fifteen_minute_rate @property def mean_rate(self): """ Returns the mean rate of the events since the start of the process. """ return self.meter.mean_rate @property def min(self): """Returns the minimum amount of time spent in the operation.""" return self.histogram.min @property def max(self): """Returns the maximum amount of time spent in the operation.""" return self.histogram.max @property def mean(self): """Returns the mean time spent in the operation.""" return self.histogram.mean @property def stddev(self): """ Returns the standard deviation of the mean spent in the operation. """ return self.histogram.stddev def stop(self): self.meter.stop()
class MeterTest(TestCase): def setUp(self): self.meter = Meter() def test_meter(self): self.meter.mark() self.assertEqual(1, self.meter.count) def test_blank_meter(self): self.assertEqual(0, self.meter.count) self.assertEqual(0.0, self.meter.mean_rate) def test_meter_value(self): self.meter.mark(3) self.assertEqual(3, self.meter.count) def test_m1_rate(self): self.meter.mark(1000) self.meter.tick() self.assertEqual(200, self.meter.m1_rate) def test_meter_threaded(self): def mark(): for i in range(100): self.meter.mark() for thread in [Thread(target=mark) for i in range(10)]: thread.start() thread.join() self.assertEqual(1000, self.meter.count) def tearDown(self): self.meter.stop()
class Timer(object): def __init__(self, histogram=HistogramExponentiallyDecaying): self.meter = Meter() self.histogram = histogram() def clear(self): self.meter.clear() self.histogram.clear() def update(self, duration): if duration >= 0: self.meter.mark() self.histogram.update(duration) @property def snapshot(self): return self.histogram.snapshot def __enter__(self): self.start_time = time.time() return self def __exit__(self, type, value, callback): self.update(time.time() - self.start_time) @property def count(self): return self.histogram.count @property def one_minute_rate(self): return self.meter.one_minute_rate @property def five_minute_rate(self): return self.meter.five_minute_rate @property def fifteen_minute_rate(self): return self.meter.fifteen_minute_rate @property def mean_rate(self): return self.meter.mean_rate @property def min(self): return self.histogram.min @property def max(self): return self.histogram.max @property def mean(self): return self.histogram.mean @property def stddev(self): return self.histogram.stddev def stop(self): self.meter.stop()