Пример #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)
Пример #2
0
 def test_notracer(self):
     tracer = VizTracer()
     handler = VizLoggingHandler()
     logging.basicConfig(handlers=[handler])
     tracer.start()
     logging.warning("lol")
     tracer.stop()
Пример #3
0
 def test_save_invalid_format(self):
     tracer = VizTracer()
     tracer.start()
     _ = len([1, 2])
     tracer.stop()
     with self.assertRaises(Exception):
         tracer.save("test.invalid")
Пример #4
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)
Пример #5
0
 def test_addinstant(self):
     tracer = VizTracer()
     tracer.start()
     tracer.add_instant('instant - "karma": True')
     tracer.stop()
     tracer.parse()
     self.assertEventNumber(tracer.data, 1)
Пример #6
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)
Пример #7
0
 def test_addinstant(self):
     tracer = VizTracer()
     tracer.start()
     tracer.add_instant("instant", {"karma": True})
     tracer.stop()
     entries = tracer.parse()
     self.assertEqual(entries, 1)
Пример #8
0
 def test_tracer_entries(self):
     tracer = VizTracer(tracer_entries=10)
     tracer.start()
     fib(10)
     tracer.stop()
     tracer.parse()
     self.assertEventNumber(tracer.data, 10)
Пример #9
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)
Пример #10
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)
Пример #11
0
 def test_tracer_entries(self):
     tracer = VizTracer(tracer_entries=10)
     tracer.start()
     fib(10)
     tracer.stop()
     entries = tracer.parse()
     self.assertEqual(entries, 10)
Пример #12
0
    def do_one_function(self, func):
        # the original speed
        with Timer() as t:
            func()
            origin = t.get_time()

        # With viztracer + python tracer
        tracer = VizTracer("python", verbose=0)
        tracer.start()
        with Timer() as t:
            func()
            instrumented = t.get_time()
        tracer.stop()
        with Timer() as t:
            entries1 = tracer.parse()
            instrumented_parse = t.get_time()
        with Timer() as t:
            tracer.generate_json()
            instrumented_json = t.get_time()
        tracer.clear()

        # With viztracer + c tracer
        tracer = VizTracer("c", verbose=0, ignore_c_function=True)
        tracer.start()
        with Timer() as t:
            func()
            instrumented_c = t.get_time()
        tracer.stop()
        with Timer() as t:
            entries2 = tracer.parse()
            instrumented_c_parse = t.get_time()
        with Timer() as t:
            tracer.generate_json()
            instrumented_c_json = t.get_time()
        tracer.clear()

        # With cProfiler
        pr = cProfile.Profile()
        pr.enable()
        with Timer() as t:
            func()
            cprofile = t.get_time()
        pr.disable()

        def time_str(name, origin, instrumented):
            return "{:.9f}({:.2f})[{}] ".format(instrumented,
                                                instrumented / origin, name)

        print("{:10}({}, {}):".format(func.__name__, entries1, entries2))
        print(time_str("origin", origin, origin))
        print(
            time_str("py", origin, instrumented) +
            time_str("parse", origin, instrumented_parse) +
            time_str("json", origin, instrumented_json))
        print(
            time_str("c", origin, instrumented_c) +
            time_str("parse", origin, instrumented_c_parse) +
            time_str("json", origin, instrumented_c_json))
        print(time_str("cProfile", origin, cprofile))
Пример #13
0
    def do_one_function(self, func):
        # the original speed
        with Timer() as t:
            func()
            origin = t.get_time()

        # With viztracer + c tracer + vdb
        tracer = VizTracer(verbose=0, vdb=True)
        tracer.start()
        with Timer() as t:
            func()
            instrumented_c_vdb = t.get_time()
        tracer.stop()
        with Timer() as t:
            tracer.parse()
            instrumented_c_vdb_parse = t.get_time()
        with Timer() as t:
            tracer.save(output_file="tmp.json")
            instrumented_c_vdb_json = t.get_time()
        os.remove("tmp.json")
        tracer.clear()

        # With viztracer + c tracer
        tracer = VizTracer(verbose=0)
        tracer.start()
        with Timer() as t:
            func()
            instrumented_c = t.get_time()
        tracer.stop()
        with Timer() as t:
            tracer.parse()
            instrumented_c_parse = t.get_time()
        with Timer() as t:
            tracer.generate_json(allow_binary=True)
            instrumented_c_json = t.get_time()
        tracer.clear()

        # With cProfiler
        pr = cProfile.Profile()
        pr.enable()
        with Timer() as t:
            func()
            cprofile = t.get_time()
        pr.disable()

        def time_str(name, origin, instrumented):
            return "{:.9f}({:.2f})[{}] ".format(instrumented,
                                                instrumented / origin, name)

        print(time_str("origin", origin, origin))
        print(
            time_str("c+vdb", origin, instrumented_c_vdb) +
            time_str("parse", origin, instrumented_c_vdb_parse) +
            time_str("json", origin, instrumented_c_vdb_json))
        print(
            time_str("c", origin, instrumented_c) +
            time_str("parse", origin, instrumented_c_parse) +
            time_str("json", origin, instrumented_c_json))
        print(time_str("cProfile", origin, cprofile))
Пример #14
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)
Пример #15
0
 def test_notracer(self):
     tracer = VizTracer()
     handler = VizLoggingHandler()
     logging.basicConfig(handlers=[handler])
     tracer.start()
     with self.assertRaises(Exception):
         logging.warning("lol")
     tracer.stop()
Пример #16
0
 def test_basic(self):
     tracer = VizTracer()
     tracer.start()
     a = VizObject(tracer, "my variable")
     a.hello = 1
     a.hello = 2
     tracer.stop()
     entries = tracer.parse()
     self.assertEqual(entries, 3)
Пример #17
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")
Пример #18
0
 def test_basic(self):
     tracer = VizTracer()
     tracer.start()
     counter = VizCounter(tracer, "name")
     counter.a = 1
     counter.b = 2
     tracer.stop()
     entries = tracer.parse()
     self.assertEqual(entries, 2)
Пример #19
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")
Пример #20
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)
Пример #21
0
 def test_log_print(self):
     tracer = VizTracer(log_print=True)
     tracer.start()
     print("hello")
     print("hello")
     print("hello")
     print("hello")
     tracer.stop()
     entries = tracer.parse()
     self.assertEqual(entries, 4)
Пример #22
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)
Пример #23
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)
Пример #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"])
Пример #25
0
 def test_double_parse(self):
     tracer = VizTracer()
     tracer.start()
     fib(10)
     tracer.stop()
     tracer.parse()
     result1 = tracer.save()
     tracer.parse()
     result2 = tracer.save()
     self.assertEqual(result1, result2)
Пример #26
0
 def test_exclude(self):
     tracer = VizTracer()
     tracer.start()
     a = VizObject(tracer, "my variable", exclude_attributes=["b", "c"])
     a.hello = 1
     a.b = 2
     a.c = 3
     a.lol = 4
     tracer.stop()
     entries = tracer.parse()
     self.assertEqual(entries, 3)
Пример #27
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"])
Пример #28
0
 def test_basic(self):
     tracer = VizTracer()
     counter = tracer.register_counter("counter")
     tracer.start()
     counter.update("a", 3)
     counter.update({"a": 4})
     tracer.update_counter("counter", "a", 5)
     tracer.update_counter("counter", {"a": 6})
     tracer.stop()
     entries = tracer.parse()
     self.assertEqual(entries, 4)
Пример #29
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()
Пример #30
0
 def test_handler(self):
     tracer = VizTracer()
     handler = VizLoggingHandler()
     handler.setTracer(tracer)
     logging.basicConfig(handlers=[handler])
     tracer.start()
     logging.warning("lol")
     tracer.stop()
     entries = tracer.parse()
     self.assertGreater(entries, 10)
     logging.getLogger().removeHandler(handler)