Package: viztracer
VizTracerVizTracer is a low-overhead logging/debugging/profiling tool that can trace and visualize your python code execution.The front-end UI is powered byPerfetto.Use "AWSD" to zoom/navigate.
More help can be found in "Support - Controls".HighlightsDetailed function entry/exit information on timeline with source codeSuper easy to use, no source code change for most features, no package dependencySupports threading, multiprocessing, subprocess and asyncPowerful front-end, able to render GB-level trace smoothlyWorks on Linux/MacOS/WindowsInstallThe preferred way to install VizTracer is via pippipinstallviztracerBasic UsageCommand LineAssume you have a python script to run:python3my_script.pyarg1arg2You can simply use VizTracer byviztracermy_script.pyarg1arg2Aresult.jsonfile will be generated, which you can open withvizviewervizviewer will host an HTTP server onhttp://localhost:9001. You can also open your browser and use that address.If you do not want vizviewer to open the webbrowser automatically, you can usevizviewer--server_onlyresult.jsonIf you just need to bring up the trace report once, and do not want the persistent server, usevizviewer--onceresult.jsonvizviewerresult.json# You can display all the files in a directory and open them in browser toovizviewer./# For very large trace files, try external trace processorvizviewer--use_external_processorresult.jsonAVS Code Extensionis available to make your life even easier.Add--opento open the reports right after tracingviztracer--openmy_script.pyarg1arg2
viztracer-oresult.html--openmy_script.pyarg1arg2modules and console scripts(likeflask) are supported as wellviztracer-myour_moduleviztracerflaskrunInlineYou can also manually start/stop VizTracer in your script as well.fromviztracerimportVizTracertracer=VizTracer()tracer.start()# Something happens heretracer.stop()tracer.save()# also takes output_file as an optional argumentOr, you can do it withwithstatementwithVizTracer(output_file="optional.json")astracer:# Something happens hereJupyterIf you are using Jupyter, you can use viztracer cell magics.# You need to load the extension first%load_extviztracer%%viztracer# Your code afterAVizTracer Reportbutton will appear after the cell and you can click it to view the resultsAdvanced UsageTrace FilterVizTracer can filter out the data you don't want to reduce overhead and keep info of a longer time period before you dump the log.Min DurationMax Stack DepthInclude FilesExclude FilesIgnore C FunctionSparse LogExtra Logs without Code ChangeVizTracer can log extra information without changing your source codeAny Variable/Attribute with RegExFunction EntryVariables in Specified FunctionGarbage Collector OperationFunction Input ArgumentsFunction Return ValueAudit EventsRaised ExceptionsAdd Custom EventVizTracer supports inserting custom events while the program is running. This works like a print debug, but you can know when this print happens while looking at trace data.Instant EventVariable EventDuration EventMiscMulti Thread SupportVizTracer supports python nativethreadingmodule without the need to do any modification to your code. Just startVizTracerbefore you create threads and it will just work.For other multi-thread scenarios, you can useenable_thread_tracing()to notice VizTracer about the thread to trace it.Refer tomulti thread docsfor detailsMulti Process SupportVizTracer supportssubprocess,multiprocessing,os.fork(),concurrent.futures, andlokyout of the box.For more general multi-process cases, VizTracer can support with some extra steps.Refer tomulti process docsfor detailsAsync SupportVizTracer supportsasyncionatively, but could enhance the report by using--log_async.Refer toasync docsfor detailsFlamegraphVizTracer can show flamegraph of traced data.vizviewer--flamegraphresult.jsonRemote attachVizTracer supports remote attach to an arbitrary Python process to trace it, as long as viztracer is importableRefer toremote attach docsJSON alternativeVizTracer needs to dump the internal data to json format. It is recommended for the users to installorjson, which is much faster than the builtinjsonlibrary. VizTracer will try to importorjsonand fall back to the builtinjsonlibrary iforjsondoes not exist.PerformanceVizTracer will introduce 2x to 3x overhead in the worst case. The overhead is much better if there are less function calls or if filters are applied correctly.An example run for test_performance with Python 3.8 / Ubuntu 18.04.4 on Github VMfib:0.000678067(1.00)[origin]0.019880272(29.32)[py]0.011103901(16.38)[parse]0.021165599(31.21)[json]0.001344933(1.98)[c]0.008181911(12.07)[parse]0.015789866(23.29)[json]0.001472846(2.17)[cProfile]hanoi(6148,4100):0.000550255(1.00)[origin]0.016343521(29.70)[py]0.007299123(13.26)[parse]0.016779364(30.49)[json]0.001062505(1.93)[c]0.006416136(11.66)[parse]0.011463236(20.83)[json]0.001144914(2.08)[cProfile]qsort(8289,5377):0.002817679(1.00)[origin]0.052747431(18.72)[py]0.011339725(4.02)[parse]0.023644345(8.39)[json]0.004767673(1.69)[c]0.008735166(3.10)[parse]0.017173703(6.09)[json]0.007248019(2.57)[cProfile]slow_fib(1135,758):0.028759652(1.00)[origin]0.033994071(1.18)[py]0.001630461(0.06)[parse]0.003386635(0.12)[json]0.029481623(1.03)[c]0.001152415(0.04)[parse]0.002191417(0.08)[json]0.028289305(0.98)[cProfile]DocumentationFor full documentation, please seehttps://viztracer.readthedocs.io/en/stableBugs/RequestsPlease send bug reports and feature requests throughgithub issue tracker. VizTracer is currently under development now and it's open to any constructive suggestions.LicenseCopyright 2020-2023 Tian Gao.Distributed under the terms of theApache 2.0 license.