@@ -28,10 +28,10 @@ def set_agent_config(sampling_interval_seconds=1, cpu_limit_percentage=DEFAULT_C
28
28
29
29
30
30
def 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 )
32
32
33
33
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 )
35
35
36
36
37
37
class TestProfilerDisabler :
@@ -59,28 +59,57 @@ def test_it_sets_all_parameters(self):
59
59
assert AgentConfiguration .get ().cpu_limit_percentage == DEFAULT_CPU_LIMIT_PERCENTAGE
60
60
61
61
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 ()
72
101
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 ()
76
105
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 ()
80
109
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 ()
84
113
85
114
86
115
class TestKillSwitch :
@@ -145,17 +174,17 @@ def test_it_returns_false_after_a_minute(self):
145
174
assert not self .killswitch .is_killswitch_on ()
146
175
147
176
148
- class TestCpuUsageCheck :
177
+ class TestSamplingCpuUsageCheck :
149
178
def before (self ):
150
179
self .timer = Timer ()
151
180
self .profile = Mock (spec = Profile )
152
181
for i in range (20 ):
153
- self .timer .record ('runProfiler ' , 0.5 )
182
+ self .timer .record ('sampleAndAggregate ' , 0.5 )
154
183
set_agent_config (sampling_interval_seconds = 1 , cpu_limit_percentage = 10 )
155
184
self .process_duration_check = CpuUsageCheck (self .timer )
156
185
157
186
158
- class TestGetAverageSamplingIntervalSeconds (TestCpuUsageCheck ):
187
+ class TestGetAverageSamplingIntervalSeconds (TestSamplingCpuUsageCheck ):
159
188
@before
160
189
def before (self ):
161
190
super ().before ()
@@ -176,7 +205,7 @@ def test_when_profiler_sample_count_less_than_min_samples_in_profile_it_returns_
176
205
assert CpuUsageCheck ._get_average_sampling_interval_seconds (self .profile ) == 23
177
206
178
207
179
- class TestIsCpuUsageLimitReached ( TestCpuUsageCheck ):
208
+ class TestIsSamplingCpuUsageLimitReached ( TestSamplingCpuUsageCheck ):
180
209
@before
181
210
def before (self ):
182
211
super ().before ()
@@ -187,43 +216,70 @@ def before(self):
187
216
yield
188
217
189
218
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 )
191
220
self .get_average_sampling_interval_mock .assert_called_once_with (self .profile )
192
221
193
222
def test_when_average_duration_exceeds_limit_it_returns_true (self ):
194
223
# 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 ()
196
225
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 ):
198
227
# timer: (0.5/4) * 100= 12.5%
199
228
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 ()
201
230
202
231
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 ()
204
233
self .get_average_sampling_interval_mock .assert_called_once_with (None )
205
234
206
235
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 ):
208
261
@before
209
262
def before (self ):
210
263
super ().before ()
211
264
212
265
def test_it_returns_false (self ):
213
266
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 ()
215
268
216
269
217
- class TestWhenTimerDoesNotHaveEnoughMeasures (TestCpuUsageCheck ):
270
+ class TestWhenTimerDoesNotHaveEnoughMeasures (TestSamplingCpuUsageCheck ):
218
271
@before
219
272
def before (self ):
220
273
super ().before ()
221
-
222
- def test_it_returns_false (self ):
223
274
self .timer .reset ()
224
275
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 ()
227
283
228
284
229
285
class TestMemoryLimitCheck :
0 commit comments