Ejemplo n.º 1
0
    def test_basic(self):
        def fib(n):
            if n == 1 or n == 0:
                return 1
            return fib(n - 1) + fib(n - 2)

        t = VizTracer(verbose=0)
        for i in range(5, 10):
            t.start()
            fib(i)
            t.stop()
            t.parse()
            t.fork_save(output_file=str(i) + ".json")
        time.sleep(0.6)

        expected = {5: 15, 6: 25, 7: 41, 8: 67, 9: 109}
        pid = None
        for i in range(5, 10):
            path = str(i) + ".json"
            self.assertTrue(os.path.exists(path))
            with open(path) as f:
                data = json.load(f)
            os.remove(path)
            self.assertEqual(len(data["traceEvents"]), expected[i])
            if pid is None:
                pid = data["traceEvents"][0]["pid"]
            else:
                self.assertEqual(data["traceEvents"][0]["pid"], pid)
Ejemplo n.º 2
0
 def test_addinstant(self):
     tracer = VizTracer()
     tracer.start()
     tracer.add_instant('instant - "karma": True')
     tracer.stop()
     tracer.parse()
     self.assertEventNumber(tracer.data, 1)
Ejemplo n.º 3
0
 def test_invalid_scope(self):
     tracer = VizTracer()
     tracer.start()
     tracer.add_instant('instant - "karma": True', scope="invalid")
     tracer.stop()
     tracer.parse()
     self.assertEventNumber(tracer.data, 0)
Ejemplo n.º 4
0
    def add_signal_handlers(self):
        try:
            if self._should_profiler_be_enabled(
            ) and self.signal_helper.can_usr_signals_be_used():
                from viztracer import VizTracer
                tracer = VizTracer(
                    output_file=constants.CODE_PROFILER_TRACE_NAME,
                    ignore_c_function=True,
                    plugins=[
                        'vizplugins.cpu_usage', 'vizplugins.memory_usage'
                    ],
                    max_stack_depth=20)
                self.logger.info(
                    "Attempting to install the default code profiler.")
                tracer.install()
                self.logger.info("Successfully installed code profiler.")
                self._set_signal_handler_not_initialized_env = False
                self.is_profiler_enabled = True
            else:
                self._disable_code_profiler()

        except Exception as e:
            self.logger.exception(e)
            self._disable_code_profiler()

        finally:
            self.shut_down()
Ejemplo n.º 5
0
 def test_install(self):
     tracer = VizTracer(output_file="remote.json")
     tracer.install()
     os.kill(os.getpid(), signal.SIGUSR1)
     time.sleep(0.1)
     os.kill(os.getpid(), signal.SIGUSR2)
     self.assertTrue(os.path.exists("remote.json"))
     os.remove("remote.json")
Ejemplo n.º 6
0
 def test_save(self):
     tracer = VizTracer(tracer_entries=10)
     tracer.start()
     fib(5)
     tracer.stop()
     tracer.parse()
     tracer.save("./tmp/result.html")
     self.assertTrue(os.path.exists("./tmp/result.html"))
     shutil.rmtree("./tmp")
Ejemplo n.º 7
0
    def test_terminate(self):
        pl = MyPlugin()
        with VizTracer(plugins=[pl]):
            _ = []

        pl = MyPlugin(terminate_well=False)
        with self.assertRaises(VizPluginError):
            with VizTracer(plugins=[pl]):
                _ = []
Ejemplo n.º 8
0
 def test_save_flamegraph(self):
     tracer = VizTracer(tracer_entries=10)
     tracer.start()
     fib(5)
     tracer.stop()
     tracer.parse()
     tracer.save_flamegraph()
     self.assertTrue(os.path.exists("result_flamegraph.html"))
     os.remove("result_flamegraph.html")
Ejemplo n.º 9
0
    def test_basic(self):
        def fib(n):
            if n == 1 or n == 0:
                return 1
            return fib(n - 1) + fib(n - 2)

        t = VizTracer(verbose=0)
        processes = {}
        for i in range(5, 10):
            t.start()
            fib(i)
            t.stop()
            t.parse()
            processes[i] = t.fork_save(output_file=str(i) + ".json")

        expected = {5: 15, 6: 25, 7: 41, 8: 67, 9: 109}
        pid = None
        for i in range(5, 10):
            path = str(i) + ".json"
            processes[i].join()
            self.assertFileExists(path, timeout=10)
            with open(path) as f:
                data = json.load(f)
            os.remove(path)
            self.assertEventNumber(data, expected[i])
            if pid is None:
                pid = data["traceEvents"][0]["pid"]
            else:
                self.assertEqual(data["traceEvents"][0]["pid"], pid)
Ejemplo n.º 10
0
 def test_addinstant(self):
     tracer = VizTracer()
     tracer.start()
     tracer.add_instant("instant", {"karma": True})
     tracer.stop()
     entries = tracer.parse()
     self.assertEqual(entries, 1)
Ejemplo n.º 11
0
 def test_invalid_scope(self):
     tracer = VizTracer()
     tracer.start()
     tracer.add_instant("instant", {"karma": True}, scope="invalid")
     tracer.stop()
     entries = tracer.parse()
     self.assertEqual(entries, 0)
Ejemplo n.º 12
0
 def wrapper(*args, **kwargs):
     tracer = VizTracer(**viztracer_kwargs)
     tracer.start()
     ret = func(*args, **kwargs)
     tracer.stop()
     if not os.path.exists(output_dir):
         os.mkdir(output_dir)
     file_name = os.path.join(output_dir, "result_{}_{}.json".format(func.__name__, int(100000 * time.time())))
     tracer.fork_save(file_name)
     tracer.cleanup()
     return ret
Ejemplo n.º 13
0
 def test_basic(self):
     tracer = VizTracer()
     tracer.start()
     with tracer.log_event("event"):
         a = []
         a.append(1)
     tracer.stop()
     tracer.parse()
     self.assertEventNumber(tracer.data, 2)
Ejemplo n.º 14
0
    def test_ignore_function_without_global_tracer(self):
        @ignore_function
        def f():
            return

        if "__viz_tracer__" in builtins.__dict__:
            builtins.__dict__.pop("__viz_tracer__")

        tracer = VizTracer(register_global=False)

        tracer.start()
        with self.assertRaises(NameError):
            f()
Ejemplo n.º 15
0
 def test_buffer_wrap(self):
     tracer = VizTracer(tracer_entries=10)
     tracer.start()
     a = VizObject(tracer, "my variable")
     for i in range(15):
         a.hello = i
     tracer.stop()
     entries = tracer.parse()
     tracer.save()
     self.assertEqual(entries, 10)
Ejemplo n.º 16
0
 def test_tracer_entries(self):
     tracer = VizTracer(tracer_entries=10)
     tracer.start()
     fib(10)
     tracer.stop()
     tracer.parse()
     self.assertEventNumber(tracer.data, 10)
Ejemplo n.º 17
0
 def test_save_invalid_format(self):
     tracer = VizTracer()
     tracer.start()
     _ = len([1, 2])
     tracer.stop()
     with self.assertRaises(Exception):
         tracer.save("test.invalid")
Ejemplo n.º 18
0
 def test_add_invalid_variable(self):
     tracer = VizTracer()
     tracer.start()
     with self.assertRaises(Exception):
         tracer.add_variable("a", 1, event="invalid")
     with self.assertRaises(Exception):
         tracer.add_variable("a", "str", event="counter")
Ejemplo n.º 19
0
 def test_basic(self):
     pl = MyPlugin()
     tracer = VizTracer(plugins=[pl])
     tracer.start()
     tracer.stop()
     tracer.save()
     self.assertEqual(pl.event_counter, 4)
     self.assertEqual(pl.handler_triggered, True)
Ejemplo n.º 20
0
 def test_inherit(self):
     tracer = VizTracer()
     tracer.start()
     a = Hello(tracer, "name")
     a.b = 1
     a.c = 2
     a.d = 3
     a.log()
     tracer.stop()
     entries = tracer.parse()
     tracer.save()
     self.assertEqual(entries, 2)
Ejemplo n.º 21
0
 def test_version(self):
     pl = MyPluginFuture()
     s = io.StringIO()
     with redirect_stdout(s):
         with VizTracer(plugins=[pl]):
             _ = []
     self.assertIn("support version is higher", s.getvalue())
Ejemplo n.º 22
0
 def test_invalid_args(self):
     invalid_args = {
         "verbose": ["hello", 0.1],
         "pid_suffix": ["hello", 1, "True"],
         "max_stack_depth": ["0.3", "hello", 1.5],
         "include_files": ["./src"],
         "exclude_files": ["./src"],
         "ignore_c_function": ["hello", 1, "True"],
         "log_print": ["hello", 1, "True"],
         "log_return_value": ["hello", 1, "True"]
     }
     tracer = VizTracer()
     for args, vals in invalid_args.items():
         for val in vals:
             with self.assertRaises(ValueError):
                 tracer.__setattr__(args, val)
Ejemplo n.º 23
0
 def test_basic(self):
     tracer = VizTracer()
     tracer.start()
     a = Hello(tracer)
     a.set_viztracer_attributes(["a", "b"])
     a.change_val()
     a.change_val2()
     a = Hello(tracer)
     a.change_val()
     tracer.stop()
     entries = tracer.parse()
     tracer.save()
     self.assertEqual(entries, 8)
Ejemplo n.º 24
0
 def test_addfunctionarg(self):
     def f(tracer):
         tracer.add_functionarg("hello", "world")
     tracer = VizTracer()
     tracer.start()
     f(tracer)
     tracer.stop()
     tracer.parse()
     self.assertTrue("args" in tracer.data["traceEvents"][0] and
                     "hello" in tracer.data["traceEvents"][0]["args"])
Ejemplo n.º 25
0
 def test_log_print(self):
     tracer = VizTracer(log_print=True)
     tracer.start()
     print("hello")
     print("hello")
     print("hello")
     print("hello")
     tracer.stop()
     tracer.parse()
     self.assertEventNumber(tracer.data, 4)
Ejemplo n.º 26
0
 def test_tracer_entries(self):
     tracer = VizTracer(tracer_entries=10)
     tracer.start()
     fib(10)
     tracer.stop()
     entries = tracer.parse()
     self.assertEqual(entries, 10)
Ejemplo n.º 27
0
    def test_basic(self):
        tracer = VizTracer(max_stack_depth=4)
        tracer.start()

        thread1 = MyThread()
        thread2 = MyThread()
        thread3 = MyThread()
        thread4 = MyThread()

        thread1.start()
        thread2.start()
        thread3.start()
        thread4.start()

        threads = [thread1, thread2, thread3, thread4]

        for thread in threads:
            thread.join()

        tracer.stop()
        entries = tracer.parse()
        self.assertGreater(entries, 160)

        metadata = [e for e in tracer.data["traceEvents"] if e["ph"] == "M"]
        self.assertEqual(
            len([e for e in metadata if e["name"] == "process_name"]), 1)
        self.assertEqual(
            len([e for e in metadata if e["name"] == "thread_name"]), 5)
Ejemplo n.º 28
0
 def test_cleanup(self):
     tracer = VizTracer()
     tracer.start()
     _ = len([1, 2, 3])
     _ = sum([2, 3, 4])
     try:
         raise Exception("lol")
     except Exception:
         pass
     tracer.stop()
     tracer.cleanup()
Ejemplo n.º 29
0
    def test_addfunctionarg(self):
        def f(tracer):
            tracer.add_func_args("hello", "world")

        tracer = VizTracer()
        tracer.start()
        f(tracer)
        tracer.stop()
        tracer.parse()
        events = [e for e in tracer.data["traceEvents"] if e["ph"] != "M"]
        self.assertTrue("args" in events[0] and "hello" in events[0]["args"])
Ejemplo n.º 30
0
 def test_get_tracer(self):
     if "__viz_tracer__" in builtins.__dict__:
         builtins.__dict__.pop("__viz_tracer__")
     with self.assertRaises(NameError):
         _ = __viz_tracer__  # noqa: F821
     self.assertIs(get_tracer(), None)
     tracer = VizTracer()
     builtins.__dict__["__viz_tracer__"] = tracer
     self.assertIs(get_tracer(), tracer)
     builtins.__dict__.pop("__viz_tracer__")