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_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 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_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(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_terminate(self): pl = MyPlugin() with VizTracer(plugins=[pl]): _ = [] pl = MyPlugin(terminate_well=False) with self.assertRaises(VizPluginError): with VizTracer(plugins=[pl]): _ = []
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): 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_addinstant(self): tracer = VizTracer() tracer.start() tracer.add_instant("instant", {"karma": True}) tracer.stop() entries = tracer.parse() self.assertEqual(entries, 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 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
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_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()
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_tracer_entries(self): tracer = VizTracer(tracer_entries=10) tracer.start() fib(10) tracer.stop() tracer.parse() self.assertEventNumber(tracer.data, 10)
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_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_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_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)
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_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)
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)
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_tracer_entries(self): tracer = VizTracer(tracer_entries=10) tracer.start() fib(10) tracer.stop() entries = tracer.parse() self.assertEqual(entries, 10)
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_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_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_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__")