Ejemplo n.º 1
0
    def init_app(self, app: Flask):
        repo = RemoteRepository(self._client)
        self._sampler = sample(self._interval,
                               self._output_threshold,
                               output_repo=repo)

        app.before_request(self.before_request)
        app.after_request(self.after_request)
        app.teardown_request(self.teardown_request)
Ejemplo n.º 2
0
def execute_script(script_name: str, options: Any, output_path: str):
    sampler = sample(options.interval, 0, output_path)

    ctx = None
    try:
        print(f"Executing the given script {script_name}")
        with open(script_name, "rb") as f:
            code_object = compile(f.read(), script_name, "exec")
            # Start to sampling
            ctx = sampler.begin(script_name)
            globals_ctx = locals_ctx = {"__name__": "__main__"}
            exec(code_object, globals_ctx, locals_ctx)
    finally:
        if ctx:
            sampler.end(ctx)
        if timer_started():
            stop_timer()
        print(f"Wrote sampling result to {output_path}")
Ejemplo n.º 3
0
    def test_for_multi_threads(self):
        path_template = "/tmp/pysample_test_output/foo_%s.txt"

        def foo():
            time.sleep(0.11)
            time.sleep(0.31)

        def foo1():
            time.sleep(0.11)

        test_funcs = [
            foo,
            foo1,
            foo1,
            foo1,
            foo,
            foo1,
            foo,
            foo1,
            foo,
            foo,
            foo,
        ]

        with ThreadPoolExecutor(max_workers=5) as executor:
            for index, func in enumerate(test_funcs):
                path = path_template % index
                func_deco = sample(100, 0, path)(func)
                executor.submit(func_deco)

        for index, func in enumerate(test_funcs):
            if func == foo:
                count = 2
            else:
                count = 1
            path = path_template % index
            with open(path, 'r') as file:
                content = file.read()
                lines = content.splitlines()
                self.assertEqual(len(lines), count)

            self._clean_output_path(path)
Ejemplo n.º 4
0
    def test_for_multi_function(self):
        def foo():
            time.sleep(0.11)

        def foo1():
            time.sleep(0.11)
            time.sleep(0.11)

        test_args = [
            ("/tmp/pysample_test_output/foo.txt", foo, 1),
            ("/tmp/pysample_test_output/foo1.txt", foo1, 2),
        ]
        for arg_tuple in test_args:
            path, func, count = arg_tuple
            func_deco = sample(100, 0, path)(func)
            func_deco()
            with open(path, 'r') as file:
                content = file.read()
                lines = content.splitlines()
                self.assertEqual(len(lines), count)

            self._clean_output_path(path)