def test_update_max_1(): time = MockedTime() counter = RollingNumber(time, 200, 10) # We start out with 0 buckets in the queue assert counter.buckets.size == 0 # Increment counter.update_rolling_max(RollingNumberEvent.THREAD_MAX_ACTIVE, 10) # We should have 1 assert counter.buckets.size == 1 # The count should be 10 assert counter.buckets.last().max_updater(RollingNumberEvent.THREAD_MAX_ACTIVE).max() == 10 assert counter.rolling_max(RollingNumberEvent.THREAD_MAX_ACTIVE) == 10 # Sleep to get to a new bucket time.increment(counter.buckets_size_in_milliseconds() * 3) # Increment again is latest bucket counter.update_rolling_max(RollingNumberEvent.THREAD_MAX_ACTIVE, 20) # We should have 4 assert counter.buckets.size == 4 # The max assert counter.buckets.last().max_updater(RollingNumberEvent.THREAD_MAX_ACTIVE).max() == 20 # Count per buckets values = counter.values(RollingNumberEvent.THREAD_MAX_ACTIVE) assert values[0] == 20 # Latest bucket assert values[1] == 0 assert values[2] == 0 assert values[3] == 10 # Oldest bucket
def test_rolling(): _time = MockedTime() counter = RollingNumber(20, 2, _time=_time) event = RollingNumberEvent.THREAD_MAX_ACTIVE assert counter.cumulative_sum(event) == 0 # Iterate over 20 buckets on a queue sized for 2 for i in range(20): counter.current_bucket() _time.increment(counter.buckets_size_in_milliseconds()) assert len(counter.values(event)) == 2 counter.value_of_latest_bucket(event)
def test_rolling(): time = MockedTime() counter = RollingNumber(time, 20, 2) event = RollingNumberEvent.THREAD_MAX_ACTIVE assert counter.cumulative_sum(event) == 0 # Iterate over 20 buckets on a queue sized for 2 for i in range(20): counter.current_bucket() time.increment(counter.buckets_size_in_milliseconds()) assert len(counter.values(event)) == 2 counter.value_of_latest_bucket(event)
def test_cumulative_counter_after_rolling(): _time = MockedTime() counter = RollingNumber(20, 2, _time=_time) event = RollingNumberEvent.SUCCESS assert counter.cumulative_sum(event) == 0 # Iterate over 20 buckets on a queue sized for 2 for i in range(20): counter.increment(event) _time.increment(counter.buckets_size_in_milliseconds()) assert len(counter.values(event)) == 2 counter.value_of_latest_bucket(event) # Cumulative count should be 20 (for the number of loops above) regardless # of buckets rolling assert counter.cumulative_sum(event) == 20
def test_cumulative_counter_after_rolling(): time = MockedTime() counter = RollingNumber(time, 20, 2) event = RollingNumberEvent.SUCCESS assert counter.cumulative_sum(event) == 0 # Iterate over 20 buckets on a queue sized for 2 for i in range(20): counter.increment(event) time.increment(counter.buckets_size_in_milliseconds()) assert len(counter.values(event)) == 2 counter.value_of_latest_bucket(event) # Cumulative count should be 20 (for the number of loops above) regardless # of buckets rolling assert counter.cumulative_sum(event) == 20
def test_cumulative_counter_after_rolling_and_reset(): _time = MockedTime() counter = RollingNumber(20, 2, _time=_time) event = RollingNumberEvent.SUCCESS assert counter.cumulative_sum(event) == 0 # Iterate over 20 buckets on a queue sized for 2 for i in range(20): counter.increment(event) _time.increment(counter.buckets_size_in_milliseconds()) assert len(counter.values(event)) == 2 counter.value_of_latest_bucket(event) # simulate a reset occurring every once in a while # so we ensure the absolute sum is handling it okay if i == 5 or i == 15: counter.reset() # Cumulative count should be 20 (for the number of loops above) regardless # of buckets rolling assert counter.cumulative_sum(event) == 20
def test_cumulative_counter_after_rolling_and_reset(): time = MockedTime() counter = RollingNumber(time, 20, 2) event = RollingNumberEvent.SUCCESS assert counter.cumulative_sum(event) == 0 # Iterate over 20 buckets on a queue sized for 2 for i in range(20): counter.increment(event) time.increment(counter.buckets_size_in_milliseconds()) assert len(counter.values(event)) == 2 counter.value_of_latest_bucket(event) # simulate a reset occurring every once in a while # so we ensure the absolute sum is handling it okay if i == 5 or i == 15: counter.reset() # Cumulative count should be 20 (for the number of loops above) regardless # of buckets rolling assert counter.cumulative_sum(event) == 20