def test_rolling(): time = MockedTime() percentile = RollingPercentile(time, 60000, 12, 1000, True) percentile.add_value(1000) percentile.add_value(1000) percentile.add_value(1000) percentile.add_value(2000) assert percentile.buckets.size == 1 # No bucket turnover yet so percentile not yet generated assert percentile.percentile(50) == 0 time.increment(6000) # Still only 1 bucket until we touch it again assert percentile.buckets.size == 1 # A bucket has been created so we have a new percentile assert percentile.percentile(50) == 1000 # Now 2 buckets since getting a percentile causes bucket retrieval assert percentile.buckets.size == 2 percentile.add_value(1000) percentile.add_value(500) assert percentile.buckets.size == 2 percentile.add_value(200) percentile.add_value(200) percentile.add_value(1600) percentile.add_value(200) percentile.add_value(1600) percentile.add_value(1600) # We haven't progressed to a new bucket so the percentile should be the # same and ignore the most recent bucket assert percentile.percentile(50) == 1000 # Increment to another bucket so we include all of the above in the # PercentileSnapshot time.increment(6000) # The rolling version should have the same data as creating a snapshot # like this snapshot = PercentileSnapshot(1000, 1000, 1000, 2000, 1000, 500, 200, 200, 1600, 200, 1600, 1600) assert snapshot.percentile(0.15) == percentile.percentile(0.15) assert snapshot.percentile(0.50) == percentile.percentile(0.50) assert snapshot.percentile(0.90) == percentile.percentile(0.90) assert snapshot.percentile(0.995) == percentile.percentile(0.995) # mean = 1000+1000+1000+2000+1000+500+200+200+1600+200+1600+1600/12 assert snapshot.mean() == 991
def test_percentile_algorithm_extremes(): ''' Unsorted so it is expected to sort it for us. ''' snapshot = PercentileSnapshot(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 800, 768, 657, 700, 867) print('0.01', snapshot.percentile(0.01)) print('10th', snapshot.percentile(10)) print('Median', snapshot.percentile(50)) print('75th', snapshot.percentile(75)) print('90th', snapshot.percentile(90)) print('99th', snapshot.percentile(99)) print('99.5th', snapshot.percentile(99.5)) print('99.99', snapshot.percentile(99.99)) assert snapshot.percentile(50) == 2 assert snapshot.percentile(10) == 2 assert snapshot.percentile(75) == 2 if snapshot.percentile(95) < 600: msg = 'We expect 90th to be over 600 to show the extremes but got: {}' pytest.fail(msg.format(snapshot.percentile(95))) if snapshot.percentile(99) < 600: msg = 'We expect 99th to be over 600 to show the extremes but got: {}' pytest.fail(msg.format(snapshot.percentile(99)))
def test_percentile_algorithm_media4(): ''' Unsorted so it is expected to sort it for us. ''' snapshot = PercentileSnapshot(300, 75, 125, 500, 100, 160, 180, 200, 210, 50, 170) assert snapshot.percentile(50) == 175
def test_percentile_algorithm_media3(): snapshot = PercentileSnapshot(50, 75, 100, 125, 160, 170, 180, 200, 210, 300, 500) assert snapshot.percentile(50) == 175
def test_percentile_algorithm_media2(): snapshot = PercentileSnapshot(100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 500) assert snapshot.percentile(50) == 100
def test_percentile_algorithm_media1(): snapshot = PercentileSnapshot(100, 100, 100, 100, 200, 200, 200, 300, 300, 300, 300) assert snapshot.percentile(50) == 200
def percentile_for_values(*values): return PercentileSnapshot(*values)