@@ -28,10 +28,10 @@ def set_agent_config(sampling_interval_seconds=1, cpu_limit_percentage=DEFAULT_C
2828
2929
3030def assert_config_sampling_interval_used (process_duration_check , profile ):
31- assert process_duration_check .is_cpu_usage_limit_reached (profile )
31+ assert process_duration_check .is_sampling_cpu_usage_limit_reached (profile )
3232
3333 set_agent_config (sampling_interval_seconds = 42 , cpu_limit_percentage = 80 )
34- assert not process_duration_check .is_cpu_usage_limit_reached (profile )
34+ assert not process_duration_check .is_sampling_cpu_usage_limit_reached (profile )
3535
3636
3737class TestProfilerDisabler :
@@ -59,28 +59,57 @@ def test_it_sets_all_parameters(self):
5959 assert AgentConfiguration .get ().cpu_limit_percentage == DEFAULT_CPU_LIMIT_PERCENTAGE
6060
6161
62- class TestWhenAnyFails (TestProfilerDisabler ):
63- @before
64- def before (self ):
65- super ().before ()
66- self .profiler = Mock ()
67- self .disabler .killswitch = Mock ()
68- self .disabler .cpu_usage_check = Mock ()
69- self .disabler ._is_memory_limit_reached = Mock (return_value = False )
70- self .disabler .killswitch .is_killswitch_on = Mock (return_value = False )
71- self .disabler .killswitch .is_process_duration_limit_reached = Mock (return_value = False )
62+ class TestShouldStopSampling :
63+ class TestWhenAnyFails (TestProfilerDisabler ):
64+ @before
65+ def before (self ):
66+ super ().before ()
67+ self .disabler .killswitch = Mock ()
68+ self .disabler .cpu_usage_check = Mock ()
69+ self .disabler .cpu_usage_check .is_sampling_cpu_usage_limit_reached = Mock (return_value = False )
70+ self .disabler ._is_memory_limit_reached = Mock (return_value = False )
71+ self .disabler .killswitch .is_killswitch_on = Mock (return_value = False )
72+ self .disabler .killswitch .is_process_duration_limit_reached = Mock (return_value = False )
73+ assert not self .disabler .should_stop_sampling ()
74+
75+ def test_it_stops_profiling_if_killswitch_is_on (self ):
76+ self .disabler .killswitch .is_killswitch_on = Mock (return_value = True )
77+ assert self .disabler .should_stop_sampling ()
78+
79+ def test_it_stops_profiling_if_memory_limit_is_reached (self ):
80+ self .disabler ._is_memory_limit_reached = Mock (return_value = True )
81+ assert self .disabler .should_stop_sampling ()
82+
83+ def test_it_stops_profiling_if_process_duration_is_reached (self ):
84+ self .disabler .cpu_usage_check .is_sampling_cpu_usage_limit_reached = Mock (return_value = True )
85+ assert self .disabler .should_stop_sampling ()
86+
87+
88+ class TestShouldStopProfiling :
89+ class TestWhenAnyFails (TestProfilerDisabler ):
90+ @before
91+ def before (self ):
92+ super ().before ()
93+ self .profiler = Mock ()
94+ self .disabler .killswitch = Mock ()
95+ self .disabler .cpu_usage_check = Mock ()
96+ self .disabler .cpu_usage_check .is_overall_cpu_usage_limit_reached = Mock (return_value = False )
97+ self .disabler ._is_memory_limit_reached = Mock (return_value = False )
98+ self .disabler .killswitch .is_killswitch_on = Mock (return_value = False )
99+ self .disabler .killswitch .is_process_duration_limit_reached = Mock (return_value = False )
100+ assert not self .disabler .should_stop_profiling ()
72101
73- def test_it_stops_profiling_if_killswitch_is_on (self ):
74- self .disabler .killswitch .is_killswitch_on = Mock (return_value = True )
75- assert self .disabler .should_stop_profiling (self . profiler )
102+ def test_it_stops_profiling_if_killswitch_is_on (self ):
103+ self .disabler .killswitch .is_killswitch_on = Mock (return_value = True )
104+ assert self .disabler .should_stop_profiling ()
76105
77- def test_it_stops_profiling_if_memory_limit_is_reached (self ):
78- self .disabler ._is_memory_limit_reached = Mock (return_value = True )
79- assert self .disabler .should_stop_profiling (self . profiler )
106+ def test_it_stops_profiling_if_memory_limit_is_reached (self ):
107+ self .disabler ._is_memory_limit_reached = Mock (return_value = True )
108+ assert self .disabler .should_stop_profiling ()
80109
81- def test_it_stops_profiling_if_process_duration_is_reached (self ):
82- self .disabler .cpu_usage_check .is_cpu_usage_limit_reached = Mock (return_value = True )
83- assert self .disabler .should_stop_profiling (self . profiler )
110+ def test_it_stops_profiling_if_process_duration_is_reached (self ):
111+ self .disabler .cpu_usage_check .is_overall_cpu_usage_limit_reached = Mock (return_value = True )
112+ assert self .disabler .should_stop_profiling ()
84113
85114
86115class TestKillSwitch :
@@ -145,17 +174,17 @@ def test_it_returns_false_after_a_minute(self):
145174 assert not self .killswitch .is_killswitch_on ()
146175
147176
148- class TestCpuUsageCheck :
177+ class TestSamplingCpuUsageCheck :
149178 def before (self ):
150179 self .timer = Timer ()
151180 self .profile = Mock (spec = Profile )
152181 for i in range (20 ):
153- self .timer .record ('runProfiler ' , 0.5 )
182+ self .timer .record ('sampleAndAggregate ' , 0.5 )
154183 set_agent_config (sampling_interval_seconds = 1 , cpu_limit_percentage = 10 )
155184 self .process_duration_check = CpuUsageCheck (self .timer )
156185
157186
158- class TestGetAverageSamplingIntervalSeconds (TestCpuUsageCheck ):
187+ class TestGetAverageSamplingIntervalSeconds (TestSamplingCpuUsageCheck ):
159188 @before
160189 def before (self ):
161190 super ().before ()
@@ -176,7 +205,7 @@ def test_when_profiler_sample_count_less_than_min_samples_in_profile_it_returns_
176205 assert CpuUsageCheck ._get_average_sampling_interval_seconds (self .profile ) == 23
177206
178207
179- class TestIsCpuUsageLimitReached ( TestCpuUsageCheck ):
208+ class TestIsSamplingCpuUsageLimitReached ( TestSamplingCpuUsageCheck ):
180209 @before
181210 def before (self ):
182211 super ().before ()
@@ -187,43 +216,70 @@ def before(self):
187216 yield
188217
189218 def test_it_calls_get_average_sampling_interval_with_profile (self ):
190- self .process_duration_check .is_cpu_usage_limit_reached (self .profile )
219+ self .process_duration_check .is_sampling_cpu_usage_limit_reached (self .profile )
191220 self .get_average_sampling_interval_mock .assert_called_once_with (self .profile )
192221
193222 def test_when_average_duration_exceeds_limit_it_returns_true (self ):
194223 # timer: (0.5/4) * 100= 12.5%
195- assert self .process_duration_check .is_cpu_usage_limit_reached ()
224+ assert self .process_duration_check .is_sampling_cpu_usage_limit_reached ()
196225
197- def test_when_average_duragtion_is_below_limit_it_returns_false (self ):
226+ def test_when_average_duration_is_below_limit_it_returns_false (self ):
198227 # timer: (0.5/4) * 100= 12.5%
199228 set_agent_config (cpu_limit_percentage = 13 )
200- assert not self .process_duration_check .is_cpu_usage_limit_reached ()
229+ assert not self .process_duration_check .is_sampling_cpu_usage_limit_reached ()
201230
202231 def test_when_profile_is_none_it_calls_get_average_sampling_interval_without_profile (self ):
203- self .process_duration_check .is_cpu_usage_limit_reached ()
232+ self .process_duration_check .is_sampling_cpu_usage_limit_reached ()
204233 self .get_average_sampling_interval_mock .assert_called_once_with (None )
205234
206235
207- class TestWhenTimerDoesNotHaveTheKey (TestCpuUsageCheck ):
236+ class TestIsOverallCpuUsageLimitReached ():
237+ @before
238+ def before (self ):
239+ self .timer = Timer ()
240+ self .profile = Mock (spec = Profile )
241+ for i in range (20 ):
242+ self .timer .record ('runProfiler' , 0.5 )
243+ set_agent_config (cpu_limit_percentage = 9 )
244+ self .process_duration_check = CpuUsageCheck (self .timer )
245+ self .profile .get_active_millis_since_start = Mock (return_value = 100 * 1000 )
246+
247+ def test_when_average_duration_exceeds_limit_it_returns_true (self ):
248+ # timer: (0.5*20/100) * 100= 10%
249+ assert self .process_duration_check .is_overall_cpu_usage_limit_reached (self .profile )
250+
251+ def test_when_average_duration_is_below_limit_it_returns_false (self ):
252+ # timer: (0.5*20/100) * 100= 10%
253+ set_agent_config (cpu_limit_percentage = 11 )
254+ assert not self .process_duration_check .is_overall_cpu_usage_limit_reached (self .profile )
255+
256+ def test_when_profile_is_none_it_returns_false (self ):
257+ assert not self .process_duration_check .is_overall_cpu_usage_limit_reached ()
258+
259+
260+ class TestWhenTimerDoesNotHaveTheKey (TestSamplingCpuUsageCheck ):
208261 @before
209262 def before (self ):
210263 super ().before ()
211264
212265 def test_it_returns_false (self ):
213266 self .process_duration_check .timer = Timer ()
214- assert not self .process_duration_check .is_cpu_usage_limit_reached ()
267+ assert not self .process_duration_check .is_sampling_cpu_usage_limit_reached ()
215268
216269
217- class TestWhenTimerDoesNotHaveEnoughMeasures (TestCpuUsageCheck ):
270+ class TestWhenTimerDoesNotHaveEnoughMeasures (TestSamplingCpuUsageCheck ):
218271 @before
219272 def before (self ):
220273 super ().before ()
221-
222- def test_it_returns_false (self ):
223274 self .timer .reset ()
224275 for i in range (4 ):
225- self .timer .record ('runProfiler' , 0.5 )
226- assert not self .process_duration_check .is_cpu_usage_limit_reached ()
276+ self .timer .record ('sampleAndAggregate' , 0.5 )
277+
278+ def test_sampling_cpu_usage_limit_reached_returns_false (self ):
279+ assert not self .process_duration_check .is_sampling_cpu_usage_limit_reached ()
280+
281+ def test_overall_cpu_usage_limit_reached_returns_false (self ):
282+ assert not self .process_duration_check .is_overall_cpu_usage_limit_reached ()
227283
228284
229285class TestMemoryLimitCheck :
0 commit comments