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
class TestModulePathExtractorWithCurrentPath: @before def before(self): self.current_path = str(Path().absolute()) self.subject = ProfileEncoder( gzip=False, environment=environment).ModulePathExtractor(sys_path=[]) def test_it_removes_current_path_and_slash_and_dot(self): file_path = self.current_path + '/./polls/views.py' if platform.system() == "Windows": import os # This ignores the first D:. # This test just asserts the current behaviour, though the "/./" removal is not set for Windows. expected = self.current_path.replace(os.sep, ".")[3:] + ".polls.views" else: expected = "polls.views" assert self.subject.get_module_path(file_path) == expected def test_it_removes_slash_and_dot(self): file_path = '/./polls/views.py' assert self.subject.get_module_path(file_path) == "polls.views" def test_it_does_nothing_when_file_path_has_no_current_path(self): file_path = '/polls/views.py' assert self.subject.get_module_path(file_path) == "polls.views"
def test_it_removes_longest_root_path_matched_from_sys_path(self): subject = ProfileEncoder( gzip=False, environment=environment).ModulePathExtractor(sys_path=[ "/tmp/TestPythonAgent/site-package/", "/tmp/TestPythonAgent/site-package/threading/", "\\tmp\\TestPythonAgent\\site-package\\", "\\tmp\\TestPythonAgent\\site-package\\threading\\" ]) assert subject.get_module_path("/tmp/TestPythonAgent/site-package/threading/DummyPackage/dummy") == \ "DummyPackage.dummy"
class TestModulePathExtractor: @before def before(self): self.subject = ProfileEncoder( gzip=False, environment=environment).ModulePathExtractor(sys_path=[ "/tmp/TestPythonAgent/site-package/", "\\tmp\\TestPythonAgent\\site-package\\" ]) def test_it_removes_root_path(self): assert self.subject \ .get_module_path("/tmp/TestPythonAgent/site-package/DummyPackage/dummy") == \ "DummyPackage.dummy" def test_it_returns_same_path_if_no_match_from_sys_paths(self): assert self.subject \ .get_module_path("this/is/clearly/not/in/sys/path/dummy.py") == \ "this.is.clearly.not.in.sys.path.dummy" def test_it_removes_longest_root_path_matched_from_sys_path(self): subject = ProfileEncoder( gzip=False, environment=environment).ModulePathExtractor(sys_path=[ "/tmp/TestPythonAgent/site-package/", "/tmp/TestPythonAgent/site-package/threading/", "\\tmp\\TestPythonAgent\\site-package\\", "\\tmp\\TestPythonAgent\\site-package\\threading\\" ]) assert subject.get_module_path("/tmp/TestPythonAgent/site-package/threading/DummyPackage/dummy") == \ "DummyPackage.dummy" def test_it_caches_result(self): self.dummy_module_extract = MagicMock("dummy_module_extractor") self.subject = ProfileEncoder( gzip=False, environment=environment).ModulePathExtractor( sys_path=["/tmp/TestPythonAgent/site-package/"], extractor_fun=self.dummy_module_extract) some_path = "/tmp/TestPythonAgent/site-package/DummyPackage/dummy.py" self.subject.get_module_path(some_path) self.subject.get_module_path(some_path) self.dummy_module_extract.assert_called_once_with( "/tmp/TestPythonAgent/site-package/DummyPackage/dummy.py", ["/tmp/TestPythonAgent/site-package/"]) def test_debug_pretty_encode_it_returns_a_json_representation_for_a_profile( self): result = ProfileEncoder.debug_pretty_encode(profile=example_profile(), environment=environment) assert (json.loads(result)["start"] == 1514764800000)
def test_it_caches_result(self): self.dummy_module_extract = MagicMock("dummy_module_extractor") self.subject = ProfileEncoder( gzip=False, environment=environment).ModulePathExtractor( sys_path=["/tmp/TestPythonAgent/site-package/"], extractor_fun=self.dummy_module_extract) some_path = "/tmp/TestPythonAgent/site-package/DummyPackage/dummy.py" self.subject.get_module_path(some_path) self.subject.get_module_path(some_path) self.dummy_module_extract.assert_called_once_with( "/tmp/TestPythonAgent/site-package/DummyPackage/dummy.py", ["/tmp/TestPythonAgent/site-package/"])
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) self._decoded_json_result = json.loads( self.output_stream.getvalue().decode("utf-8")) return self._decoded_json_result
def __init__(self, environment=dict()): """ :param environment: dependency container dictionary for the current profiler :param file_prefix: (required inside environment) path + file prefix to use for profile reports """ self._file_prefix = environment["file_prefix"] self._profile_encoder = \ environment.get("profile_encoder") or ProfileEncoder(gzip=False, environment=environment)
def test_it_gzips_the_result_before_writing_to_the_stream(self): ProfileEncoder(gzip=True, environment=environment).encode( profile=self.profile, output_stream=self.output_stream) self.output_stream.seek(0) uncompressed_result = gzip.GzipFile(fileobj=self.output_stream, mode="rb").read() assert (len(uncompressed_result) > 0)
class TestModulePathExtractorWithCurrentPath: @before def before(self): self.current_path = str(Path().absolute()) self.subject = ProfileEncoder( gzip=False, environment=environment).ModulePathExtractor(sys_path=[]) def test_it_removes_current_path(self): file_path = self.current_path + '/polls/views.py' assert self.subject.get_module_path(file_path) == "polls.views" def test_it_removes_current_path_and_slash_and_dot(self): file_path = self.current_path + '/./polls/views.py' assert self.subject.get_module_path(file_path) == "polls.views" def test_it_does_nothing_when_file_path_has_no_current_path(self): file_path = '/polls/views.py' assert self.subject.get_module_path(file_path) == "polls.views"
def __init__(self, environment): """ :param environment: dependency container dictionary for the current profiler. :param profiling_group_name: (required inside environment) name of the profiling group. :param codeguru_profiler_client: (required inside environment) sdk client for CodeGuru Profiler calls. """ self.profiling_group_name = environment["profiling_group_name"] self.codeguru_client_builder = environment["codeguru_profiler_builder"] # TODO decide if we need to compress with gzip or not. self.profile_encoder = \ environment.get("profile_encoder") or ProfileEncoder(environment=environment, gzip=False) self.timer = environment.get("timer") self.metadata = environment["agent_metadata"] self.agent_config_merger = environment["agent_config_merger"]
def test_debug_pretty_encode_it_returns_a_json_representation_for_a_profile( self): result = ProfileEncoder.debug_pretty_encode(profile=example_profile(), environment=environment) assert (json.loads(result)["start"] == 1514764800000)
def before(self): self.subject = ProfileEncoder( gzip=False, environment=environment).ModulePathExtractor(sys_path=[ "/tmp/TestPythonAgent/site-package/", "\\tmp\\TestPythonAgent\\site-package\\" ])
def before(self): self.current_path = str(Path().absolute()) self.subject = ProfileEncoder( gzip=False, environment=environment).ModulePathExtractor(sys_path=[])