VizTracer is a low-overhead logging, debugging, and profiling tool for Python that traces and visualizes code execution on a timeline. It supports multi-threading, multi-processing, asyncio, and PyTorch, and the front-end UI is powered by Perfetto. The current version is 1.1.1, and it has a regular release cadence with recent releases approximately every 1-2 months.
pip install viztracerVerified import paths — ran on the pinned version, not inferred.
This example demonstrates inline usage of VizTracer as a context manager to profile a Python function. After execution, a 'result.json' file is generated, which can be opened and visualized with the `vizviewer` command-line tool.
Upgrade to a supported Python version (currently 3.10+).
Use the native flamegraph functionality available in the Perfetto UI after loading the VizTracer report.
Review and update code to use the public `VizTracer` API, avoiding direct interaction with `_VizTracer`.
Adjust the circular buffer size (`buffer_size` parameter in `VizTracer` or `--buffer_size` via CLI) or use trace filters (`--include_files`, `--exclude_files`, `--max_stack_depth`, `--log_sparse`) to reduce the amount of data collected.
Whenever possible, use the command-line interface for profiling scripts with multiprocessing or subprocesses. Refer to the official documentation for specific concurrency tracing configurations.
Use `viztracer [viztracer_args] -- [script_args]` to explicitly separate arguments.
pip install viztracer
Ensure `viztracer` is installed (`pip install viztracer`) and your Python environment's script directory (e.g., `~/.local/bin` or your virtual environment's `bin/Scripts` folder) is included in your system's PATH.
Run `viztracer` on your code to generate a trace file (e.g., `viztracer your_script.py`), or specify the correct path to an existing trace file (e.g., `vizviewer my_trace.json`).
Ensure `VizTracer.start()` is called only once per `VizTracer` instance; if you need to trace multiple independent sections, create new `VizTracer` instances or use `VizTracer.pause()` and `VizTracer.resume()`.