class TestCPULimitReachedDuringProfiling: @before def before(self): self.timer = Timer() self.profiler = Profiler( profiling_group_name=DUMMY_TEST_PROFILING_GROUP_NAME, environment_override={ "timer": self.timer, "cpu_limit_percentage": 40, "sampling_interval": timedelta(seconds=0.01), 'agent_metadata': AgentMetadata(fleet_info=DefaultFleetInfo()) }, ) yield self.profiler.stop() def test_profiler_terminates(self): self.profiler.start() assert self.profiler.is_running() # With sampling_interval to be 0.01 seconds, having runProfiler as 0.5 seconds should breach # the cpu limit. We need to sample 20 times before we check the CPU limit for i in range(20): self.timer.record('sampleAndAggregate', 0.5) assert wait_for(lambda: not self.profiler.is_running(), timeout_seconds=5)
def test_metrics_get_reset(self): subject = Timer() subject.record("test-record", 10) subject.reset() assert (not "test-record" in subject.metrics)
class TestIsOverallCpuUsageLimitReached(): @before def before(self): self.timer = Timer() self.profile = Mock(spec=Profile) for i in range(20): self.timer.record('runProfiler', 0.5) set_agent_config(cpu_limit_percentage=9) self.process_duration_check = CpuUsageCheck(self.timer) self.profile.get_active_millis_since_start = Mock(return_value=100 * 1000) def test_when_average_duration_exceeds_limit_it_returns_true(self): # timer: (0.5*20/100) * 100= 10% assert self.process_duration_check.is_overall_cpu_usage_limit_reached( self.profile) def test_when_average_duration_is_below_limit_it_returns_false(self): # timer: (0.5*20/100) * 100= 10% set_agent_config(cpu_limit_percentage=11) assert not self.process_duration_check.is_overall_cpu_usage_limit_reached( self.profile) def test_when_profile_is_none_it_returns_false(self): assert not self.process_duration_check.is_overall_cpu_usage_limit_reached( )
class TestCpuUsageCheck: def before(self): self.timer = Timer() self.profile = Mock(spec=Profile) for i in range(20): self.timer.record('runProfiler', 0.5) set_agent_config(sampling_interval_seconds=1, cpu_limit_percentage=10) self.process_duration_check = CpuUsageCheck(self.timer)
def test_it_returns_json_with_generic_metrics(self): timer = Timer() timer.record("metric1", 12345000) timer.record("metric1", 12350000) subject = AgentDebugInfo(timer=timer) serialized_json = subject.serialize_to_json() assert serialized_json["genericMetrics"] == { "metric1_timings_max": 12350000, "metric1_timings_average": 12347500.0 }
class TestRecord: @before def before(self): self.subject = Timer() self.subject.record("test-record", 10) def test_new_record_comes_in(self): self.subject.record("new-record", 12) assert (self.subject.metrics["new-record"].total == 12) assert (self.subject.metrics["new-record"].counter == 1) def test_update_old_record(self): self.subject.record("test-record", 20) assert (self.subject.metrics["test-record"].total == 30) assert (self.subject.metrics["test-record"].counter == 2)