<s> import pytest <EOL> from . utils import MockedTime <EOL> from . sample_data import sample_data_holder_1 , sample_data_holder_2 <EOL> from hystrix . rolling_percentile import RollingPercentile , PercentileSnapshot <EOL> def test_rolling ( ) : <EOL> time = MockedTime ( ) <EOL> percentile = RollingPercentile ( time , <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:1000> , True ) <EOL> percentile . add_value ( <NUM_LIT:1000> ) <EOL> percentile . add_value ( <NUM_LIT:1000> ) <EOL> percentile . add_value ( <NUM_LIT:1000> ) <EOL> percentile . add_value ( <NUM_LIT> ) <EOL> assert percentile . buckets . size == <NUM_LIT:1> <EOL> assert percentile . percentile ( <NUM_LIT:50> ) == <NUM_LIT:0> <EOL> time . increment ( <NUM_LIT> ) <EOL> assert percentile . buckets . size == <NUM_LIT:1> <EOL> assert percentile . percentile ( <NUM_LIT:50> ) == <NUM_LIT:1000> <EOL> assert percentile . buckets . size == <NUM_LIT:2> <EOL> percentile . add_value ( <NUM_LIT:1000> ) <EOL> percentile . add_value ( <NUM_LIT> ) <EOL> assert percentile . buckets . size == <NUM_LIT:2> <EOL> percentile . add_value ( <NUM_LIT:200> ) <EOL> percentile . add_value ( <NUM_LIT:200> ) <EOL> percentile . add_value ( <NUM_LIT> ) <EOL> percentile . add_value ( <NUM_LIT:200> ) <EOL> percentile . add_value ( <NUM_LIT> ) <EOL> percentile . add_value ( <NUM_LIT> ) <EOL> assert percentile . percentile ( <NUM_LIT:50> ) == <NUM_LIT:1000> <EOL> time . increment ( <NUM_LIT> ) <EOL> snapshot = PercentileSnapshot ( <NUM_LIT:1000> , <NUM_LIT:1000> , <NUM_LIT:1000> , <NUM_LIT> , <NUM_LIT:1000> , <NUM_LIT> , <EOL> <NUM_LIT:200> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> ) <EOL> assert snapshot . percentile ( <NUM_LIT> ) == percentile . percentile ( <NUM_LIT> ) <EOL> assert snapshot . percentile ( <NUM_LIT> ) == percentile . percentile ( <NUM_LIT> ) <EOL> assert snapshot . percentile ( <NUM_LIT> ) == percentile . percentile ( <NUM_LIT> ) <EOL> assert snapshot . percentile ( <NUM_LIT> ) == percentile . percentile ( <NUM_LIT> ) <EOL> assert snapshot . mean ( ) == <NUM_LIT> <EOL> def test_value_is_zero_after_rolling_window_passes_and_no_traffic ( ) : <EOL> time = MockedTime ( ) <EOL> percentile = RollingPercentile ( time , <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:1000> , True ) <EOL> percentile . add_value ( <NUM_LIT:1000> ) <EOL> percentile . add_value ( <NUM_LIT:1000> ) <EOL> percentile . add_value ( <NUM_LIT:1000> ) <EOL> percentile . add_value ( <NUM_LIT> ) <EOL> percentile . add_value ( <NUM_LIT> ) <EOL> assert percentile . buckets . size == <NUM_LIT:1> <EOL> assert percentile . percentile ( <NUM_LIT:50> ) == <NUM_LIT:0> <EOL> time . increment ( <NUM_LIT> ) <EOL> assert percentile . buckets . size == <NUM_LIT:1> <EOL> assert percentile . percentile ( <NUM_LIT:50> ) == <NUM_LIT> <EOL> time . increment ( <NUM_LIT> ) <EOL> assert percentile . percentile ( <NUM_LIT:50> ) == <NUM_LIT:0> <EOL> def test_sample_data_over_time_1 ( ) : <EOL> time = MockedTime ( ) <EOL> percentile = RollingPercentile ( time , <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:1000> , True ) <EOL> previous_time = <NUM_LIT:0> <EOL> for time_millis , latency in sample_data_holder_1 : <EOL> time . increment ( time_millis - previous_time ) <EOL> previous_time = time_millis <EOL> percentile . add_value ( latency ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT:50> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT:50> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT:50> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT:50> ) ) <EOL> for _ in range ( <NUM_LIT:10> ) : <EOL> percentile50 = percentile . percentile ( <NUM_LIT:50> ) <EOL> if percentile50 > <NUM_LIT:5> : <EOL> pytest . fail ( '<STR_LIT>' . format ( percentile50 ) ) <EOL> percentile995 = percentile . percentile ( <NUM_LIT> ) <EOL> if percentile995 < <NUM_LIT:20> : <EOL> msg = '<STR_LIT>' <EOL> pytest . fail ( msg . format ( percentile995 ) ) <EOL> def test_sample_data_over_time_2 ( ) : <EOL> time = MockedTime ( ) <EOL> percentile = RollingPercentile ( time , <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:1000> , True ) <EOL> previous_time = <NUM_LIT:0> <EOL> for time_millis , latency in sample_data_holder_2 : <EOL> time . increment ( time_millis - previous_time ) <EOL> previous_time = time_millis <EOL> percentile . add_value ( latency ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT:50> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , percentile . percentile ( <NUM_LIT> ) ) <EOL> percentile50 = percentile . percentile ( <NUM_LIT:50> ) <EOL> if percentile50 > <NUM_LIT> or percentile50 < <NUM_LIT:50> : <EOL> pytest . fail ( '<STR_LIT>' . format ( percentile50 ) ) <EOL> percentile99 = percentile . percentile ( <NUM_LIT> ) <EOL> if percentile99 < <NUM_LIT> : <EOL> msg = '<STR_LIT>' <EOL> pytest . fail ( msg . format ( percentile99 ) ) <EOL> def test_percentile_algorithm_media1 ( ) : <EOL> snapshot = PercentileSnapshot ( <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:200> , <NUM_LIT:200> , <EOL> <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> assert snapshot . percentile ( <NUM_LIT:50> ) == <NUM_LIT:200> <EOL> def test_percentile_algorithm_media2 ( ) : <EOL> snapshot = PercentileSnapshot ( <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:100> , <EOL> <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT> ) <EOL> assert snapshot . percentile ( <NUM_LIT:50> ) == <NUM_LIT:100> <EOL> def test_percentile_algorithm_media3 ( ) : <EOL> snapshot = PercentileSnapshot ( <NUM_LIT:50> , <NUM_LIT> , <NUM_LIT:100> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> assert snapshot . percentile ( <NUM_LIT:50> ) == <NUM_LIT> <EOL> def test_percentile_algorithm_media4 ( ) : <EOL> '''<STR_LIT>''' <EOL> snapshot = PercentileSnapshot ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:100> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT:50> , <NUM_LIT> ) <EOL> assert snapshot . percentile ( <NUM_LIT:50> ) == <NUM_LIT> <EOL> def test_percentile_algorithm_extremes ( ) : <EOL> '''<STR_LIT>''' <EOL> snapshot = PercentileSnapshot ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <EOL> <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <EOL> <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> print ( '<STR_LIT>' , snapshot . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , snapshot . percentile ( <NUM_LIT:10> ) ) <EOL> print ( '<STR_LIT>' , snapshot . percentile ( <NUM_LIT:50> ) ) <EOL> print ( '<STR_LIT>' , snapshot . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , snapshot . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , snapshot . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , snapshot . percentile ( <NUM_LIT> ) ) <EOL> print ( '<STR_LIT>' , snapshot . percentile ( <NUM_LIT> ) ) <EOL> assert snapshot . percentile ( <NUM_LIT:50> ) == <NUM_LIT:2> <EOL> assert snapshot . percentile ( <NUM_LIT:10> ) == <NUM_LIT:2> <EOL> assert snapshot . percentile ( <NUM_LIT> ) == <NUM_LIT:2> <EOL> if snapshot . percentile ( <NUM_LIT> ) < <NUM_LIT> : <EOL> msg = '<STR_LIT>' <EOL> pytest . fail ( msg . format ( snapshot . percentile ( <NUM_LIT> ) ) ) <EOL> if snapshot . percentile ( <NUM_LIT> ) < <NUM_LIT> : <EOL> msg = '<STR_LIT>' <EOL> pytest . fail ( msg . format ( snapshot . percentile ( <NUM_LIT> ) ) ) <EOL> def percentile_for_values ( * values ) : <EOL> return PercentileSnapshot ( * values ) <EOL> def test_percentile_algorithm_high_percentile ( ) : <EOL> snapshot = percentile_for_values ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> assert snapshot . percentile ( <NUM_LIT:50> ) == <NUM_LIT:2> <EOL> assert snapshot . percentile ( <NUM_LIT> ) == <NUM_LIT:3> <EOL> def test_percentile_algorithm_low_percentile ( ) : <EOL> snapshot = percentile_for_values ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> assert snapshot . percentile ( <NUM_LIT> ) == <NUM_LIT:1> <EOL> assert snapshot . percentile ( <NUM_LIT> ) == <NUM_LIT:2> <EOL> def test_percentile_algorithm_percentiles ( ) : <EOL> snapshot = percentile_for_values ( <NUM_LIT:10> , <NUM_LIT:30> , <NUM_LIT:20> , <NUM_LIT> ) <EOL> assert snapshot . percentile ( <NUM_LIT:30> ) == <NUM_LIT> <EOL> assert snapshot . percentile ( <NUM_LIT> ) == <NUM_LIT:20> <EOL> assert snapshot . percentile ( <NUM_LIT> ) == <NUM_LIT> <EOL> assert snapshot . percentile ( <NUM_LIT:50> ) == <NUM_LIT:30> <EOL> assert snapshot . percentile ( - <NUM_LIT:1> ) == <NUM_LIT:10> <EOL> assert snapshot . percentile ( <NUM_LIT> ) == <NUM_LIT> <EOL> def test_percentile_algorithm_NIST_example ( ) : <EOL> snapshot = percentile_for_values ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> ) <EOL> assert snapshot . percentile ( <NUM_LIT> ) == <NUM_LIT> <EOL> assert snapshot . percentile ( <NUM_LIT:100> ) == <NUM_LIT> <EOL> def test_does_nothing_when_disabled ( ) : <EOL> time = MockedTime ( ) <EOL> percentile = RollingPercentile ( time , <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:1000> , False ) <EOL> previous_time = <NUM_LIT:0> <EOL> for time_millis , latency in sample_data_holder_2 : <EOL> time . increment ( time_millis - previous_time ) <EOL> previous_time = time_millis <EOL> percentile . add_value ( latency ) <EOL> assert percentile . percentile ( <NUM_LIT:50> ) == - <NUM_LIT:1> <EOL> assert percentile . percentile ( <NUM_LIT> ) == - <NUM_LIT:1> <EOL> assert percentile . mean ( ) == - <NUM_LIT:1> </s>
