def clear(self): """ Resets the meter. """ self.start_time = time() self._count = 0 self._m1_rate = EWMA.one_minute_EWMA() self._m5_rate = EWMA.five_minute_EWMA() self._m15_rate = EWMA.fifteen_minute_EWMA()
def __init__(self, event_type=""): """ Creates a new L{Meter} instance. @type event_type: C{str} @param event_type: the plural name of the event the meter is measuring (e.g., I{"requests"}) """ self.event_type = event_type self.start_time = time() self._m1_rate = EWMA.one_minute_EWMA() self._m5_rate = EWMA.five_minute_EWMA() self._m15_rate = EWMA.fifteen_minute_EWMA() self._count = 0
def test_fifteen_minute_EWMA_one_minute_tick(self, time_mock): time_mock.return_value = 0.0 self.ewma = EWMA.fifteen_minute_EWMA() self.ewma.update(3) time_mock.return_value += 5 self.ewma.tick() for expected_rate in [0.6, 0.56130419, 0.52510399, 0.49123845, 0.45955700, 0.42991879, 0.40219203, 0.37625345, 0.35198773, 0.32928698]: self.assertAlmostEqual(self.ewma.get_rate(), expected_rate) time_mock.return_value += 60
def test_five_minute_EWMA_one_minute_tick(self, time_mock): time_mock.return_value = 0.0 self.ewma = EWMA.five_minute_EWMA() self.ewma.update(3) time_mock.return_value += 5 self.ewma.tick() for expected_rate in [0.6, 0.49123845, 0.40219203, 0.32928698, 0.26959738, 0.22072766, 0.18071653, 0.14795818, 0.12113791, 0.09917933]: self.assertAlmostEqual(self.ewma.get_rate(), expected_rate) time_mock.return_value += 60
def test_one_minute_EWMA_one_minute_tick(self, time_mock): time_mock.return_value = 0.0 self.ewma = EWMA.one_minute_EWMA() self.ewma.update(3) time_mock.return_value += 5 self.ewma.tick() for expected_rate in [0.6, 0.22072766, 0.08120117, 0.02987224, 0.01098938, 0.00404277, 0.00148725, 0.00054713, 0.00020128, 0.00007405]: self.assertAlmostEqual(self.ewma.get_rate(), expected_rate) time_mock.return_value += 60
def test_fifteen_minute_EWMA_one_minute_tick(self, time_mock): time_mock.return_value = 0.0 self.ewma = EWMA.fifteen_minute_EWMA() self.ewma.update(3) time_mock.return_value += 5 self.ewma.tick() for expected_rate in [ 0.6, 0.56130419, 0.52510399, 0.49123845, 0.45955700, 0.42991879, 0.40219203, 0.37625345, 0.35198773, 0.32928698 ]: self.assertAlmostEqual(self.ewma.get_rate(), expected_rate) time_mock.return_value += 60
def test_five_minute_EWMA_one_minute_tick(self, time_mock): time_mock.return_value = 0.0 self.ewma = EWMA.five_minute_EWMA() self.ewma.update(3) time_mock.return_value += 5 self.ewma.tick() for expected_rate in [ 0.6, 0.49123845, 0.40219203, 0.32928698, 0.26959738, 0.22072766, 0.18071653, 0.14795818, 0.12113791, 0.09917933 ]: self.assertAlmostEqual(self.ewma.get_rate(), expected_rate) time_mock.return_value += 60
def test_one_minute_EWMA_one_minute_tick(self, time_mock): time_mock.return_value = 0.0 self.ewma = EWMA.one_minute_EWMA() self.ewma.update(3) time_mock.return_value += 5 self.ewma.tick() for expected_rate in [ 0.6, 0.22072766, 0.08120117, 0.02987224, 0.01098938, 0.00404277, 0.00148725, 0.00054713, 0.00020128, 0.00007405 ]: self.assertAlmostEqual(self.ewma.get_rate(), expected_rate) time_mock.return_value += 60