A debugging and profiling tool that can trace and visualize python code execution
Project description
VizTracer
VizTracer is a low-overhead deterministic debugging/profiling/logging tool that can trace and visualize your python code to help you intuitively understand your code better and figure out the time consuming part of your code.
You can take a look at the demo result of multiple example programs(sort algorithms, mcts, modulo algorithms, multithread tracing, etc.)
trace viewer is used to display the stand alone html data.
VizTracer also supports json output that complies with Chrome trace event format, which can be loaded using perfetto
VizTracer generates HTML report for flamegraph using d3-flamegraph
Highlights
- Lower overhead than cProfile, more accurate on actual time consumed
- Detailed function entry/exit information on timeline, not just summary of time used
- Super easy to use, no source code change for basic usage, no package dependency
- Optional function filter to ignore functions you are not interested
- Custom events to log and track data through time
- Stand alone HTML report with powerful front-end, or chrome-compatible json
- Works on Linux/MacOS/Windows
Install
The prefered way to install VizTracer is via pip
pip install viztracer
You can also download the source code and build it yourself.
Usage
There are a couple ways to use VizTracer
Command Line
The easiest way to use VizTracer is through command line. Assume you have a python script to profile and the normal way to run it is:
python3 my_script.py arg1 arg2
You can simply use VizTracer as
python3 -m viztracer my_script.py arg1 arg2
# OR
viztracer my_script arg1 arg2
which will generate a result.html
file in the directory you run this command. Open it in browser and there's your result.
You can also generate json
file or gz
file and load it with chrome://tracing/ or perfetto. gz
file is especially helpful when your trace file is large
python3 -m viztracer -o result.json my_script.py arg1 arg2
python3 -m viztracer -o result.json.gz my_script.py arg1 arg2
By default, VizTracer only generates trace file, either in HTML format or json. You can have VizTracer to generate a flamegraph as well by
python3 -m viztracer --save_flamegraph my_script.py
Inline
Sometimes the command line may not work as you expected, or you do not want to profile the whole script. You can manually start/stop the profiling in your script as well.
from viztracer import VizTracer
tracer = VizTracer()
tracer.start()
# Something happens here
tracer.stop()
tracer.save() # also takes output_file as an optional argument
Or, you can do it with with
statement
with VizTracer(output_file="optional.html") as tracer:
# Something happens here
Display Result
By default, VizTracer will generate a stand alone HTML file which you can simply open with Chrome(maybe Firefox?). The front-end uses trace-viewer to show all the data.
However, you can generate json file as well, which complies to the chrome trace event format. You can load the json file on perfetto, which will replace the deprecated trace viewer in the future. Or you can use chrome://tracing to load the file.
When you are dealing with big traces, a stand alone HTML file might be very large and hard to load. You should try to dump a compressed filename.json.gz
file and load it via chrome://tracing/ or perfetto
Trace Filter
Sometimes your code is really complicated or you need to run you program for a long time, which means the parsing time would be too long and the HTML/JSON file would be too large. There are ways in VizTracer to filter out the data you don't need.
The filter works at tracing time, not parsing time. That means, using filters will introduce some extra overhead while your tracing, but will save significant memory, parsing time and disk space.
VizTracer support:
Add Custom Event
VizTracer
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.
VizTracer has:
Multi Thread Support
VizTracer
supports python native threading
module without the need to do any modification to your code. Just start VizTracer
before you create threads and it will just work.
Multi Process Support
VizTracer can support multi process with some extra steps. The current structure of VizTracer keeps one single buffer for one process, which means the user will have to produce multiple results from multiple processes and combine them together.
Refer to multi process does for details
JSON alternative
VizTracer needs to dump the internal data to json format. It is recommended for the users to install orjson
, which is much faster than the builtin json
library. VizTracer will try to import orjson
and fall back to the builtin json
library if orjson
does not exist.
Performance
Overhead is a big consideration when people choose profilers. VizTracer has a better overhead performance than native cProfiler. In the worst case(Pure FEE) VizTracer is about the same as cProfile and in more practical cases VizTracer performs much better.
This is because VizTracer collects less information than cProfile, and optimized the hook function with a lot of efforts.
An example run for test_performance with Python 3.8 / Ubuntu 18.04.4 on Github VM
fib:
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]
Documentation
For full documentation, please see https://viztracer.readthedocs.io/en/stable
Bugs/Requests
Please send bug reports and feature requests through github issue tracker. VizTracer is currently under development now and it's open to any constructive suggestions.
License
Copyright Tian Gao, 2020.
Distributed under the terms of the Apache 2.0 license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file viztracer-0.5.0.tar.gz
.
File metadata
- Download URL: viztracer-0.5.0.tar.gz
- Upload date:
- Size: 665.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b97b596ea02249fa3c3eb2d0ec4f962d8c74a94b47ec2922bca96aefbc95677 |
|
MD5 | 9c170a71670371b50287ddc872fe86c4 |
|
BLAKE2b-256 | 3cade736f80fd7315ad41a0fba2e456d40eef02116105235254bb3086453559a |
File details
Details for the file viztracer-0.5.0-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: viztracer-0.5.0-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 672.8 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a54e871207a8d4f97921074e55c55dfe532da4b66895d1867d5c0ed8f52d33e6 |
|
MD5 | 4241b2a9c8266579dbb878c08975b518 |
|
BLAKE2b-256 | eb24e35215fa2f9cb68c824eb01a9e009bb4485ad302707eeb2cd98424eeb66e |
File details
Details for the file viztracer-0.5.0-cp38-cp38-manylinux2010_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.0-cp38-cp38-manylinux2010_x86_64.whl
- Upload date:
- Size: 701.7 kB
- Tags: CPython 3.8, manylinux: glibc 2.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6e268a7029ac9c2aafc7fdeb6aeabce0246ebecd91f99192722f2ed19038b45 |
|
MD5 | 262119f5e21efba83428904979fe06fc |
|
BLAKE2b-256 | d01c17a2082fc2f68df92b4fa8dd406a393f279ff813b5d6145b3ae182c2774d |
File details
Details for the file viztracer-0.5.0-cp38-cp38-manylinux1_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.0-cp38-cp38-manylinux1_x86_64.whl
- Upload date:
- Size: 701.7 kB
- Tags: CPython 3.8
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f4496c542e68404a14900c58a203700ca8a22081ae93c9245c7c173c08c95de |
|
MD5 | 594d1231587baea42ab3e0e987d13111 |
|
BLAKE2b-256 | d4071b3b596ea86bdd2f26b84d89f2563be283fe851983eabffaf5cbc98a1b1b |
File details
Details for the file viztracer-0.5.0-cp38-cp38-macosx_10_14_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.0-cp38-cp38-macosx_10_14_x86_64.whl
- Upload date:
- Size: 669.7 kB
- Tags: CPython 3.8, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2596f2269d96b64a80388c5083507a7c1622baf112497ef5f1e01e06518753f3 |
|
MD5 | a60c058d6bdbf4254a234c4ad059a151 |
|
BLAKE2b-256 | 6bbabac0125dc24826d251bf70ef85b38b9034aa268ea52669de4beab5246e8a |
File details
Details for the file viztracer-0.5.0-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: viztracer-0.5.0-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 672.8 kB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | da27e64439314cd3fe6be8e3ad9180b82526e83e5d718e73285891eef24f1010 |
|
MD5 | 0d5a6d9f1fd1d1a5a1a5407b5e51ed9f |
|
BLAKE2b-256 | fb9384719614e0da764a8d136dc8f850f8d16fd3417baafbaa5b7b2b825365b9 |
File details
Details for the file viztracer-0.5.0-cp37-cp37m-manylinux2010_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.0-cp37-cp37m-manylinux2010_x86_64.whl
- Upload date:
- Size: 697.8 kB
- Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 544b08d392a552920f5b0bded053a7679bdf8df8fc7df184935423f1a155ecd6 |
|
MD5 | 9360ff1bfbdb300008f9c1b6c43edf89 |
|
BLAKE2b-256 | 46f0926e787ab2e2fecc50d5d952ab816d78ac11753c26d2b259f7e277886128 |
File details
Details for the file viztracer-0.5.0-cp37-cp37m-manylinux1_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.0-cp37-cp37m-manylinux1_x86_64.whl
- Upload date:
- Size: 697.8 kB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0c730e6cb97902c486d3713b4fec0d7219cb2cc13c58573d807087e92fa4b7e |
|
MD5 | 751763f204752677f62b68c7d84c82f5 |
|
BLAKE2b-256 | 996588925e01ab6496f7b7394f9ad509bd1c2a787fbcc825808ac762954529ea |
File details
Details for the file viztracer-0.5.0-cp37-cp37m-macosx_10_14_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.0-cp37-cp37m-macosx_10_14_x86_64.whl
- Upload date:
- Size: 669.6 kB
- Tags: CPython 3.7m, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ee9e2ed499bad59f8f226cd649281ae971c6a4344531f9b7fa89b4c6497f6fd |
|
MD5 | cae3f460ac9c085f8b07db2fdeaebf8e |
|
BLAKE2b-256 | 15e71a50d8731a23726b905b392accf6d9f367cbcfd6049e84489a77c5e24a74 |
File details
Details for the file viztracer-0.5.0-cp36-cp36m-win_amd64.whl
.
File metadata
- Download URL: viztracer-0.5.0-cp36-cp36m-win_amd64.whl
- Upload date:
- Size: 672.8 kB
- Tags: CPython 3.6m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 628695bee500c74a03faf39a3dad0b439f1445fb32c62665ceec852524a7a869 |
|
MD5 | 6d84a769077a7d1e4125391fcc02e95d |
|
BLAKE2b-256 | 3a7ce07378851cb44389cb1473f9a226bf6308b8bc4c12a6fb0af8c1f229e40c |
File details
Details for the file viztracer-0.5.0-cp36-cp36m-manylinux2010_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.0-cp36-cp36m-manylinux2010_x86_64.whl
- Upload date:
- Size: 696.9 kB
- Tags: CPython 3.6m, manylinux: glibc 2.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1a836928eabcea49ca883683f299a0f8f7c3874f0e79beb878af38f69ffbcf4 |
|
MD5 | d813845c9cdd977ad375a46793262cde |
|
BLAKE2b-256 | 5ae655a89ee73d3a16e316373d19f7409bcc088ef100a1883de881e9aad67b69 |
File details
Details for the file viztracer-0.5.0-cp36-cp36m-manylinux1_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.0-cp36-cp36m-manylinux1_x86_64.whl
- Upload date:
- Size: 696.9 kB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9511d35b02492764fc941b3b396d06def3e7d9ad801a76b6e8b131b336a26b0e |
|
MD5 | 2ee4ee497ed1f19f3361ff02e233dd92 |
|
BLAKE2b-256 | ae95fa3c71fd07797cd140b9ab68b834b240f9f015c21952eafe367f5fb0f9b2 |
File details
Details for the file viztracer-0.5.0-cp36-cp36m-macosx_10_14_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.0-cp36-cp36m-macosx_10_14_x86_64.whl
- Upload date:
- Size: 669.6 kB
- Tags: CPython 3.6m, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b468ba5c16df04172bfa6e91a5e591c76e3e3638f700c1c0f6b381808b8683d |
|
MD5 | 0478a5f641cd19b3aa7300b63dd23ca3 |
|
BLAKE2b-256 | 72200d7660730083dddf49b27a8418c8f4336b8d8b82f87de1022d4a28c73370 |