def __init__(self, *args, **kwargs): """Initialize attributes. :param host: Optional. The hostname where redis is running. :param port: Optional. The port where redis is running. """ super().__init__() # The Redis connection object. self._redis = None self._redis_params = kwargs.get('redis_params', {}) # Running coordination. self._should_stop = False self._runner_coro = None # The verbosity level to pass around to Resolwe utilities # such as data_purge. self._verbosity = kwargs.get('verbosity', 1) # Statistics about how much time each event needed for handling. self.service_time = stats.NumberSeriesShape() # Statistics about the number of events handled per time interval. self.load_avg = stats.SimpleLoadAvg([60, 5 * 60, 15 * 60]) # Timestamp of last critical load error and level, for throttling. self.last_load_log = -math.inf self.last_load_level = 0
def test_load_avg_multi(self): avg = stats.SimpleLoadAvg([60, 5 * 60, 15 * 60]) for i in range(1, 15 * 60): avg.add(1, timestamp=i) self.assertAlmostEqual(avg.intervals['1m'].value, 1, places=1) self.assertAlmostEqual(avg.intervals['5m'].value, 0.9, places=1) self.assertAlmostEqual(avg.intervals['15m'].value, 0.6, places=1)
def test_load_avg_decay(self): avg = stats.SimpleLoadAvg([60]) for i in range(0, 5 * 60): avg.add(2, timestamp=i) self.assertAlmostEqual(avg.intervals['1m'].value, 2, places=1) for i in range(5 * 60, 10 * 60): avg.add(5, timestamp=i) self.assertAlmostEqual(avg.intervals['1m'].value, 5, places=1) for i in range(10 * 60, 15 * 60): avg.add(0, timestamp=i) self.assertAlmostEqual(avg.intervals['1m'].value, 0, places=1)
def test_load_avg_basic(self): avg = stats.SimpleLoadAvg([60]) self.assertAlmostEqual(avg.intervals[60].value, 0.0) self.assertAlmostEqual(avg.intervals['1m'].value, 0.0) avg.add(1, timestamp=0.0) self.assertAlmostEqual(avg.intervals[60].value, 0.0) avg.add(1, timestamp=0.2) with self.assertRaises(ValueError): avg.add(1, timestamp=0.1) avg.add(1, timestamp=0.4) avg.add(1, timestamp=0.6) avg.add(1, timestamp=0.8) avg.add(1, timestamp=1.0) avg.add(1, timestamp=5) self.assertAlmostEqual(avg.intervals[60].value, 0.1, places=1)