class Timer: def __init__(self, meterId, clock=SystemClock()): self.meterId = meterId self._clock = clock self._count = AtomicNumber(0) self._totalTime = AtomicNumber(0) self._totalOfSquares = AtomicNumber(0) self._max = AtomicNumber(0) def record(self, amount): if amount >= 0: self._count.increment_and_get() self._totalTime.add_and_get(amount) self._totalOfSquares.add_and_get(amount * amount) self._max.max(amount) def stopwatch(self): return StopWatch(self) def count(self): return self._count.get() def total_time(self): return self._totalTime.get() def _measure(self): ms = {} for stat in ['count', 'totalTime', 'totalOfSquares', 'max']: v = getattr(self, "_{}".format(stat)).get_and_set(0) ms[self.meterId.with_stat(stat)] = v return ms
class DistributionSummary: def __init__(self, meterId): self.meterId = meterId self._count = AtomicNumber(0) self._totalAmount = AtomicNumber(0) self._totalOfSquares = AtomicNumber(0) self._max = AtomicNumber(0) def record(self, amount): if amount >= 0: self._count.increment_and_get() self._totalAmount.add_and_get(amount) self._totalOfSquares.add_and_get(amount * amount) self._max.max(amount) def count(self): return self._count.get() def total_amount(self): return self._totalAmount.get() def _measure(self): ms = {} for stat in ['count', 'totalAmount', 'totalOfSquares', 'max']: v = getattr(self, "_{}".format(stat)).get_and_set(0) ms[self.meterId.with_stat(stat)] = v return ms
class Counter(AbstractCounter): def __init__(self, meterId): self.meterId = meterId self._count = AtomicNumber(0) def increment(self, amount=1): if amount > 0: self._count.add_and_get(amount) def count(self): return self._count.get() def _measure(self): return {self.meterId.with_stat('count'): self._count.get_and_set(0)}
def test_add_and_get(self): v = AtomicNumber(42) self.assertEqual(44, v.add_and_get(2)) self.assertEqual(44, v.get())