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
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)
class Gauge(AbstractGauge): ttl = 15 * 60 def __init__(self, meterId, clock=SystemClock()): self.meterId = meterId self._clock = clock self._last_update = AtomicNumber(float('nan')) self._value = AtomicNumber(float('nan')) def get(self): return self._value.get() def set(self, value): self._last_update.set(self._clock.wall_time()) self._value.set(value) def _has_expired(self): return (self._clock.wall_time() - self._last_update.get()) > self.ttl def _measure(self): id = self.meterId.with_default_stat('gauge') if self._has_expired(): v = self._value.get_and_set(float('nan')) else: v = self._value.get() return {id: v}
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)}
class Gauge: def __init__(self, meterId): self.meterId = meterId self._value = AtomicNumber(float('nan')) def get(self): return self._value.get() def set(self, value): self._value.set(value) def _measure(self): id = self.meterId.with_stat('gauge') v = self._value.get_and_set(float('nan')) return {id: v}
def test_max(self): v = AtomicNumber(42) self.assertEqual(42, v.max(2)) self.assertEqual(46, v.max(46))
def test_add_and_get(self): v = AtomicNumber(42) self.assertEqual(44, v.add_and_get(2)) self.assertEqual(44, v.get())
def test_increment_and_get(self): v = AtomicNumber(42) self.assertEqual(43, v.increment_and_get()) self.assertEqual(43, v.get())
def test_get_and_set(self): v = AtomicNumber(42) self.assertEqual(42, v.get_and_set(44)) self.assertEqual(44, v.get())
def __init__(self, meterId): self.meterId = meterId self._count = AtomicNumber(0)
def __init__(self, meterId, clock=SystemClock()): self.meterId = meterId self._clock = clock self._last_update = AtomicNumber(float('nan')) self._value = AtomicNumber(float('nan'))
def __init__(self, meterId): self.meterId = meterId self._count = AtomicNumber(0) self._totalAmount = AtomicNumber(0) self._totalOfSquares = AtomicNumber(0) self._max = AtomicNumber(0)
def test_str(self): v = AtomicNumber(42) self.assertEqual("AtomicLong(42)", str(v))
def __init__(self, meterId): self.meterId = meterId self._value = AtomicNumber(float('nan'))
def test_init(self): v = AtomicNumber(42) self.assertEqual(42, v.get()) v.set(44) self.assertEqual(44, v.get())
def test_get_and_add(self): v = AtomicNumber(42) self.assertEqual(42, v.get_and_add(2))