예제 #1
0
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
예제 #2
0
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
예제 #3
0
 def test_increment_and_get(self):
     v = AtomicNumber(42)
     self.assertEqual(43, v.increment_and_get())
     self.assertEqual(43, v.get())