def test_inactivity_should_not_corrupt_sampling_state(): class Clock(object): def __init__(self, time): self._time = time def __call__(self): return self._time def add_millis(self, millis): self._time += 0.001 * millis def add_hours(self, hours): self._time += 60 * 60 * hours with mock.patch( 'uwsgi_metrics.reservoir.Reservoir.current_time_in_fractional_seconds', Clock(42.0)) as clock: reservoir = Reservoir('unit', size=10, alpha=0.015) # Add 1000 values at a rate of 10 values/second for i in xrange(1000): reservoir.update(1000 + i) clock.add_millis(100) assert reservoir.get_snapshot().size() == 10 assert_all_values_between(reservoir, 1000, 2000) # Wait for 15 hours and add another value. This should trigger a # rescale. Note that the number of samples will be reduced to 2 # because of the very small scaling factor that will make all # existing priorities equal to zero after rescale. clock.add_hours(15) reservoir.update(2000) reservoir.get_snapshot().size() == 2 assert_all_values_between(reservoir, 1000, 3000)
def test_inactivity_should_not_corrupt_sampling_state(): class Clock(object): def __init__(self, time): self._time = time def __call__(self): return self._time def add_millis(self, millis): self._time += 0.001 * millis def add_hours(self, hours): self._time += 60 * 60 * hours with mock.patch('uwsgi_metrics.reservoir.Reservoir.current_time_in_fractional_seconds', Clock(42.0)) as clock: reservoir = Reservoir('unit', size=10, alpha=0.015) # Add 1000 values at a rate of 10 values/second for i in xrange(1000): reservoir.update(1000 + i) clock.add_millis(100) assert reservoir.get_snapshot().size() == 10 assert_all_values_between(reservoir, 1000, 2000) # Wait for 15 hours and add another value. This should trigger a # rescale. Note that the number of samples will be reduced to 2 # because of the very small scaling factor that will make all # existing priorities equal to zero after rescale. clock.add_hours(15) reservoir.update(2000) reservoir.get_snapshot().size() == 2 assert_all_values_between(reservoir, 1000, 3000)
def test_insert_100_elemnts_into_a_heavily_baised_reservoir_of_1000(): reservoir = Reservoir('unit', size=1000, alpha=0.01) for i in xrange(100): reservoir.update(i) assert reservoir.get_snapshot().size() == 100 assert_all_values_between(reservoir, 0, 100)
def test_insert_10_elements_into_a_reservoir_of_100(): reservoir = Reservoir('unit', size=100, alpha=0.99) for i in range(10): reservoir.update(i) assert reservoir.get_snapshot().size() == 10 assert_all_values_between(reservoir, 0, 10)
class Histogram(object): """A metric which calculates the distribution of a value. Translated from Histogram.java """ def __init__(self): self.count = 0 self.reservoir = Reservoir() def update(self, value): self.count += 1 self.reservoir.update(value) def get_count(self): return self.count def get_snapshot(self): return self.reservoir.get_snapshot() def view(self): result = self.get_snapshot().view() result['count'] = self.get_count() return result