cli: debug: finalize profiler reports on errors

Even if a command failed, it might still be useful to look at the profiler report.
This commit is contained in:
Ruslan Kuprieiev
2023-12-13 20:34:30 +02:00
parent a02c5a7405
commit 4392af6d95
+12 -6
View File
@@ -23,9 +23,10 @@ def viztracer_profile(
tracer = viztracer.VizTracer(max_stack_depth=depth, log_async=log_async)
tracer.start()
try:
yield
finally:
tracer.stop()
tracer.save(path() if callable(path) else path)
@@ -45,7 +46,9 @@ def yappi_profile(
yappi.set_clock_type("wall" if wall_clock else "cpu")
yappi.start()
try:
yield
finally:
yappi.stop()
threads = yappi.get_thread_stats()
@@ -85,12 +88,14 @@ def instrument(html_output=False):
profiler = Profiler()
profiler.start()
try:
yield
finally:
profiler.stop()
if html_output:
profiler.open_in_browser()
return
else:
print(profiler.output_text(unicode=True, color=True)) # noqa: T201
@@ -102,13 +107,14 @@ def profile(dump_path: Optional[str] = None):
prof = cProfile.Profile()
prof.enable()
try:
yield
finally:
prof.disable()
if not dump_path:
prof.print_stats(sort="cumtime")
return
if dump_path:
prof.dump_stats(dump_path)
else:
prof.print_stats(sort="cumtime")
@contextmanager