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 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 test_terminate(self): pl = MyPlugin() with VizTracer(plugins=[pl]): _ = [] pl = MyPlugin(terminate_well=False) with self.assertRaises(VizPluginError): with VizTracer(plugins=[pl]): _ = []
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 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()
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 do_one_function(self, func): tracer = VizTracer(verbose=0) tracer.start() with Timer() as t: func() baseline = t.get_time() tracer.stop() tracer.cleanup() tracer.include_files = ["/"] tracer.start() with Timer() as t: func() include_files = t.get_time() tracer.stop() tracer.cleanup() tracer.include_files = [] tracer.max_stack_depth = 200 tracer.start() with Timer() as t: func() max_stack_depth = t.get_time() tracer.stop() tracer.cleanup() print("Filter performance:") print("Baseline: {:.9f}(1)".format(baseline)) print("Include: {:.9f}({:.2f})".format( include_files, include_files / baseline)) print("Max stack depth: {:.9f}({:.2f})".format( max_stack_depth, max_stack_depth / baseline))
def test_notracer(self): tracer = VizTracer() handler = VizLoggingHandler() logging.basicConfig(handlers=[handler]) tracer.start() logging.warning("lol") tracer.stop()
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() entries = tracer.parse() self.assertEqual(entries, 0)
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() entries = tracer.parse() self.assertEqual(entries, 1)
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_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_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_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_save_invalid_format(self): tracer = VizTracer() tracer.start() _ = len([1, 2]) tracer.stop() with self.assertRaises(Exception): tracer.save("test.invalid")
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())
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): 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_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")
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() 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() with tracer.log_event("event"): a = [] a.append(1) tracer.stop() tracer.parse() self.assertEventNumber(tracer.data, 2)
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_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__")
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_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)