def test_exit_routine(self): tracer = VizTracer(tracer_entries=10) tracer.start() fib(5) tracer.exit_routine() self.assertTrue(os.path.exists("result.html")) os.remove("result.html")
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)
def test_notracer(self): tracer = VizTracer() handler = VizLoggingHandler() logging.basicConfig(handlers=[handler]) tracer.start() logging.warning("lol") tracer.stop()
def test_tracer_entries(self): tracer = VizTracer(tracer_entries=10) tracer.start() fib(10) tracer.stop() entries = tracer.parse() self.assertEqual(entries, 10)
def test_addinstant(self): tracer = VizTracer() tracer.start() tracer.add_instant('instant - "karma": True') tracer.stop() tracer.parse() self.assertEventNumber(tracer.data, 1)
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)
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")
def test_addinstant(self): tracer = VizTracer() tracer.start() tracer.add_instant("instant", {"karma": True}) tracer.stop() entries = tracer.parse() self.assertEqual(entries, 1)
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)
def test_tracer_entries(self): tracer = VizTracer(tracer_entries=10) tracer.start() fib(10) tracer.stop() tracer.parse() self.assertEventNumber(tracer.data, 10)
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)
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)
def test_save_invalid_format(self): tracer = VizTracer() tracer.start() _ = len([1, 2]) tracer.stop() with self.assertRaises(Exception): tracer.save("test.invalid")
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))
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))
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)
def test_notracer(self): tracer = VizTracer() handler = VizLoggingHandler() logging.basicConfig(handlers=[handler]) tracer.start() with self.assertRaises(Exception): logging.warning("lol") tracer.stop()
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)
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")
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)
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")
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)
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)
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)
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"])
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)
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)
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"])
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()
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)