コード例 #1
0
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(
        )
 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)
コード例 #3
0
    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)
コード例 #4
0
 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_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
        }
コード例 #6
0
 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()
コード例 #7
0
 def _set_default_environment(profiling_group_name):
     return {
         'timer':
         Timer(),
         'profiler_thread_name':
         'codeguru-profiler-agent-' + str(uuid.uuid4()).replace('-', ''),
         'reporting_mode':
         'codeguru_service',
         'file_prefix':
         'profile-{}'.format(re.sub(r"\W", "", profiling_group_name)),
         'excluded_threads':
         set(),
         'should_profile':
         True,
         'sampling_interval':
         DEFAULT_SAMPLING_INTERVAL,
         'reporting_interval':
         DEFAULT_REPORTING_INTERVAL,
         'minimum_time_reporting':
         INITIAL_MINIMUM_REPORTING_INTERVAL,
         'max_stack_depth':
         DEFAULT_MAX_STACK_DEPTH,
         'cpu_limit_percentage':
         DEFAULT_CPU_LIMIT_PERCENTAGE,
         'memory_limit_bytes':
         DEFAULT_MEMORY_LIMIT_BYTES,
         'host_weight':
         1.0,
         'killswitch_filepath':
         KILLSWITCH_FILEPATH,
         'max_threads':
         100
     }
 def before(self):
     self.timer = Timer()
     self.env = {
         'timer': self.timer,
         'killswitch_filepath': 'path_to_my_kill_switch',
         'memory_limit_bytes': DEFAULT_MEMORY_LIMIT_BYTES
     }
     self.disabler = ProfilerDisabler(self.env)
コード例 #9
0
        def test_metrics_get_reset(self):
            subject = Timer()
            subject.record("test-record", 10)

            subject.reset()

            assert (not "test-record" in subject.metrics)
コード例 #10
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)
 def test_it_returns_false(self):
     self.process_duration_check.timer = Timer()
     assert not self.process_duration_check.is_cpu_usage_limit_reached()
コード例 #12
0
                    [Frame("bottom"), Frame("middle")]],
            attempted_sample_threads_count=10,
            seen_threads_count=15))
    profile.end = end_time
    profile.set_overhead_ms(timedelta(milliseconds=256))
    if platform.system() == "Windows":
        # In Windows, as time.process stays constant if no cpu time was used (https://bugs.python.org/issue37859), we
        # would need to manually override the cpu_time_seconds to ensure the test runs as expected
        profile.cpu_time_seconds = 0.123
    return profile


agent_metadata = AgentMetadata(fleet_info=AWSEC2Instance(
    host_name="testHostName", host_type="testHostType"))

environment = {"timer": Timer(), "agent_metadata": agent_metadata}


class TestSdkProfileEncoder:
    def before(self):
        self.profile = example_profile()
        self.output_stream = io.BytesIO()
        self.subject = \
            ProfileEncoder(gzip=False, environment=environment)
        self.decoded_json_result = self.decoded_json_result.__get__(self)
        self._decoded_json_result = None

    def decoded_json_result(self):
        if not self._decoded_json_result:
            self.subject.encode(profile=self.profile,
                                output_stream=self.output_stream)
コード例 #13
0
 def __init__(self):
     self.timer = Timer()
コード例 #14
0
 def before(self):
     self.subject = Timer()
     self.subject.record("test-record", 10)
            seen_threads_count=15))
    profile.end = end_time
    profile.set_overhead_ms(timedelta(milliseconds=256))
    if platform.system() == "Windows":
        # In Windows, as time.process stays constant if no cpu time was used (https://bugs.python.org/issue37859), we
        # would need to manually override the cpu_time_seconds to ensure the test runs as expected
        profile.cpu_time_seconds = 0.123
    return profile


agent_metadata = AgentMetadata(fleet_info=AWSEC2Instance(
    host_name="testHostName", host_type="testHostType"))
errors_metadata = ErrorsMetadata()

environment = {
    "timer": Timer(),
    "agent_metadata": agent_metadata,
    "errors_metadata": errors_metadata
}


class TestSdkProfileEncoder:
    def before(self):
        self.profile = example_profile()
        self.output_stream = io.BytesIO()
        self.subject = \
            ProfileEncoder(gzip=False, environment=environment)
        self.decoded_json_result = self.decoded_json_result.__get__(self)
        self._decoded_json_result = None

    def decoded_json_result(self):