You might be familiar with the chrome trace profiling tool if you are a front-end engineer and often work with Chrome. In fact, you can open chrome://tracing and record your browser’s behavior. The result will look like the image below:
It is a great tool for both performance analysis and debugging, because it shows every single thing happened in all the threads in chronological order. You can zoom out the get a general idea of the whole picture, or zoom in to check the details of a single function.
This is especially helpful when you try to profile or debug a multi-process or/and multi-thread program, when a traditional profiler/debugger can’t layout concurrency intuitively.
In fact, this UI, called trace-viewer, does not only live in Chrome. Android systrace is using this as well(and of course that’s a Google thing too).
Now, you can enjoy the powerful tracing visualization tool with Python, using an open-source library — VizTracer.
VizTracer generates similar data that complies to chrome trace event format so you can visualize that using the exact same tool. Actually, VizTracer can generate the standalone report directly, so the only thing you need to do is to open the file in the browser.
It’s super easy to use VizTracer. Install it through pip:
pip install viztracer
Then, instead of using python to run your program, use VizTracer
viztracer your_script.py
VizTracer will produce a result.html file with the log of your program. Something like:
VizTracer supports multi-thread and multi-process, Linux, MacOS and Windows. You can enjoy the power of Chrome trace viewer on Python.
In addition, VizTracer has many features to make your life even easier without any source code change.
For example, you can log
- Function arguments and return value
- Any variable or attribute matching certain RegEx
- Exceptions raised, caught or not
- Garbage collection
- All stuff
print()
function send to stdout
There are more features to explore once you are familiar with the tool. You can check it out at https://github.com/gaogaotiantian/viztracer