def before(self): now_millis = int(time.time()) * 1000 five_minutes_ago_millis = now_millis - (5 * 60 * 1000) sample = Sample( stacks=[[Frame(MY_PROFILING_GROUP_NAME_FOR_INTEG_TESTS)]], attempted_sample_threads_count=1, seen_threads_count=1) self.profile = Profile(MY_PROFILING_GROUP_NAME_FOR_INTEG_TESTS, 1.0, 1.0, five_minutes_ago_millis) # FIXME: Remove adding the end time manually below after feature fully support self.profile.end = now_millis self.profile.add(sample) self.environment = { "should_profile": True, "profiling_group_name": MY_PROFILING_GROUP_NAME_FOR_INTEG_TESTS, "aws_session": boto3.session.Session(), "reporting_interval": timedelta(minutes=13), "sampling_interval": timedelta(seconds=1), "minimum_time_reporting": timedelta(minutes=6), "max_stack_depth": 2345, "cpu_limit_percentage": 29, "agent_metadata": AgentMetadata(fleet_info=DefaultFleetInfo()) } self.environment["codeguru_profiler_builder"] = CodeGuruClientBuilder( self.environment) self.agent_config = AgentConfiguration( should_profile=True, sampling_interval=self.environment["sampling_interval"], reporting_interval=self.environment["reporting_interval"], minimum_time_reporting=self.environment["minimum_time_reporting"], max_stack_depth=self.environment["max_stack_depth"], cpu_limit_percentage=self.environment["cpu_limit_percentage"])
def example_profile(): start_time = 1514764800000 end_time = 1514772000000 profile = Profile(profiling_group_name="TestProfilingGroupName", sampling_interval_seconds=1.0, host_weight=2, start=start_time, clock=lambda: 1514772000000) profile.add( Sample( stacks=[[Frame("bottom"), Frame("middle"), Frame("top")], [Frame("bottom"), Frame("middle"), Frame("different_top")], [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
def test_it_raise_exception_for_invalid_start_time(self): with pytest.raises(ValueError): Profile(profiling_group_name="foo", sampling_interval_seconds=1.0, host_weight=2, start=0) with pytest.raises(ValueError): Profile(profiling_group_name="foo", sampling_interval_seconds=1.0, host_weight=2, start=-100) with pytest.raises(TypeError): Profile(profiling_group_name="foo", sampling_interval_seconds=1.0, host_weight=2, start=None)
def test_it_raise_exception_for_invalid_start_time(self): with pytest.raises(ValueError): Profile(profiling_group_name="foo", sampling_interval_seconds=1.0, host_weight=2, start=0, agent_debug_info=AgentDebugInfo(ErrorsMetadata)) with pytest.raises(ValueError): Profile(profiling_group_name="foo", sampling_interval_seconds=1.0, host_weight=2, start=-100, agent_debug_info=AgentDebugInfo(ErrorsMetadata)) with pytest.raises(TypeError): Profile(profiling_group_name="foo", sampling_interval_seconds=1.0, host_weight=2, start=None, agent_debug_info=AgentDebugInfo(ErrorsMetadata))
def before(self): self.test_start_time = 1603884061556 self.mock_clock = Mock() self.mock_clock.return_value = self.test_start_time / 1000 self.subject = Profile(profiling_group_name="foo", sampling_interval_seconds=1.0, host_weight=2, start=self.test_start_time, clock=self.mock_clock) def turn_clock(seconds): self.mock_clock.return_value += seconds self.turn_clock = turn_clock
class TestLiveBackendReporting: @before def before(self): now_millis = int(time.time()) * 1000 five_minutes_ago_millis = now_millis - (5 * 60 * 1000) sample = Sample( stacks=[[Frame(MY_PROFILING_GROUP_NAME_FOR_INTEG_TESTS)]], attempted_sample_threads_count=1, seen_threads_count=1) errors_metadata = ErrorsMetadata() self.profile = Profile(MY_PROFILING_GROUP_NAME_FOR_INTEG_TESTS, 1.0, 1.0, five_minutes_ago_millis, AgentDebugInfo(errors_metadata)) # FIXME: Remove adding the end time manually below after feature fully support self.profile.end = now_millis self.profile.add(sample) self.environment = { "should_profile": True, "profiling_group_name": MY_PROFILING_GROUP_NAME_FOR_INTEG_TESTS, "aws_session": boto3.session.Session(), "reporting_interval": timedelta(minutes=13), "sampling_interval": timedelta(seconds=1), "minimum_time_reporting": timedelta(minutes=6), "max_stack_depth": 2345, "cpu_limit_percentage": 29, "agent_metadata": AgentMetadata(fleet_info=DefaultFleetInfo()), "errors_metadata": errors_metadata } self.environment["codeguru_profiler_builder"] = CodeGuruClientBuilder( self.environment) self.agent_config = AgentConfiguration( should_profile=True, sampling_interval=self.environment["sampling_interval"], reporting_interval=self.environment["reporting_interval"], minimum_time_reporting=self.environment["minimum_time_reporting"], max_stack_depth=self.environment["max_stack_depth"], cpu_limit_percentage=self.environment["cpu_limit_percentage"]) def test_beta_endpoint_call_report_and_refresh_and_do_not_override_user_overrides_agent_configuration( self): self.environment["agent_config_merger"] = AgentConfigurationMerger( user_overrides=self.agent_config) sdk_reporter = SdkReporter(self.environment) sdk_reporter.setup() self.assert_initial_values() assert sdk_reporter.report(self.profile) is True sdk_reporter.refresh_configuration() self.assert_initial_values() def test_beta_endpoint_call_report_and_refresh_and_overrides_default_agent_configuration( self): self.environment["agent_config_merger"] = AgentConfigurationMerger( default=self.agent_config) sdk_reporter = SdkReporter(self.environment) sdk_reporter.setup() self.assert_initial_values() assert sdk_reporter.report(self.profile) is True sdk_reporter.refresh_configuration() assert AgentConfiguration.get().should_profile is True assert AgentConfiguration.get().sampling_interval == timedelta( seconds=1) assert AgentConfiguration.get().reporting_interval == timedelta( minutes=5) assert AgentConfiguration.get().minimum_time_reporting == timedelta( seconds=60) assert AgentConfiguration.get().max_stack_depth == 1000 assert AgentConfiguration.get().cpu_limit_percentage == 10 def test_beta_endpoint_call_report_and_refresh_and_do_not_override_one_setting_of_default_agent_configuration( self): self.environment["agent_config_merger"] = AgentConfigurationMerger( default=self.agent_config, user_overrides=AgentConfiguration(sampling_interval=timedelta( seconds=2))) sdk_reporter = SdkReporter(self.environment) sdk_reporter.setup() assert AgentConfiguration.get().should_profile is True assert AgentConfiguration.get().sampling_interval == timedelta( seconds=2) assert AgentConfiguration.get().reporting_interval == timedelta( minutes=13) assert AgentConfiguration.get().minimum_time_reporting == timedelta( minutes=6) assert AgentConfiguration.get().max_stack_depth == 2345 assert AgentConfiguration.get().cpu_limit_percentage == 29 assert sdk_reporter.report(self.profile) is True sdk_reporter.refresh_configuration() assert AgentConfiguration.get().should_profile is True assert AgentConfiguration.get().sampling_interval == timedelta( seconds=2) assert AgentConfiguration.get().reporting_interval == timedelta( minutes=5) assert AgentConfiguration.get().minimum_time_reporting == timedelta( seconds=60) assert AgentConfiguration.get().max_stack_depth == 1000 assert AgentConfiguration.get().cpu_limit_percentage == 10 @staticmethod def assert_initial_values(): assert AgentConfiguration.get().should_profile is True assert AgentConfiguration.get().sampling_interval == timedelta( seconds=1) assert AgentConfiguration.get().reporting_interval == timedelta( minutes=13) assert AgentConfiguration.get().minimum_time_reporting == timedelta( minutes=6) assert AgentConfiguration.get().max_stack_depth == 2345 assert AgentConfiguration.get().cpu_limit_percentage == 29
from unittest.mock import MagicMock from botocore.stub import Stubber, ANY from codeguru_profiler_agent.reporter.agent_configuration import AgentConfigurationMerger from codeguru_profiler_agent.agent_metadata.agent_metadata import AgentMetadata, DefaultFleetInfo from codeguru_profiler_agent.agent_metadata.aws_lambda import LAMBDA_EXECUTION_ENV, \ LAMBDA_MEMORY_SIZE_ENV, LAMBDA_TASK_ROOT, LAMBDA_RUNTIME_DIR from codeguru_profiler_agent.reporter.agent_configuration import AgentConfiguration from codeguru_profiler_agent.sdk_reporter.sdk_reporter import SdkReporter from codeguru_profiler_agent.sdk_reporter.profile_encoder import ProfileEncoder from codeguru_profiler_agent.model.profile import Profile from codeguru_profiler_agent.codeguru_client_builder import CodeGuruClientBuilder profiling_group_name = "test-ProfilingGroup-name" autocreated_test_lambda_profiling_group_name = "aws-lambda-testLambdaName" profile = Profile(profiling_group_name, 1.0, 0.5, current_milli_time()) class TestSdkReporter: def before(self): codeguru_client_builder = CodeGuruClientBuilder( environment={"aws_session": boto3.session.Session()}) self.client_stubber = Stubber(codeguru_client_builder.codeguru_client) self.clear_lambda_specific_environment_variables_for_test_run() profile_encoder = MagicMock(name="profile_encoder", spec=ProfileEncoder) profile_encoder.encode.side_effect = lambda **args: args[ "output_stream"].write(b"test-profile-encoder-output")
from botocore.stub import Stubber, ANY from codeguru_profiler_agent.reporter.agent_configuration import AgentConfigurationMerger from codeguru_profiler_agent.agent_metadata.agent_metadata import AgentMetadata, DefaultFleetInfo from codeguru_profiler_agent.agent_metadata.aws_lambda import LAMBDA_EXECUTION_ENV, \ LAMBDA_MEMORY_SIZE_ENV, LAMBDA_TASK_ROOT, LAMBDA_RUNTIME_DIR from codeguru_profiler_agent.reporter.agent_configuration import AgentConfiguration from codeguru_profiler_agent.sdk_reporter.sdk_reporter import SdkReporter from codeguru_profiler_agent.sdk_reporter.profile_encoder import ProfileEncoder from codeguru_profiler_agent.model.profile import Profile from codeguru_profiler_agent.codeguru_client_builder import CodeGuruClientBuilder profiling_group_name = "test-ProfilingGroup-name" autocreated_test_lambda_profiling_group_name = "aws-lambda-testLambdaName" errors_metadata = ErrorsMetadata() profile = Profile(profiling_group_name, 1.0, 0.5, current_milli_time(), AgentDebugInfo(errors_metadata)) expected_response_create_pg = { 'profilingGroup': { 'agentOrchestrationConfig': { 'profilingEnabled': True }, 'arn': 'string', 'computePlatform': 'AWSLambda', 'createdAt': datetime(2015, 1, 1), 'name': 'string', 'profilingStatus': { 'latestAgentOrchestratedAt': datetime(2015, 1, 1), 'latestAgentProfileReportedAt': datetime(2015, 1, 1), 'latestAggregatedProfile': { 'period': 'PT5M', 'start': datetime(2015, 1, 1)