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
- Keep latest entries in a circular buffer, dump anytime or auto save at exit
- 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
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
There are a lot of advanced usage available as well.
Display Result
By default, VizTracer will generate a stand alone HTML file which you can simply open with Chrome. 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.2.tar.gz
.
File metadata
- Download URL: viztracer-0.5.2.tar.gz
- Upload date:
- Size: 665.5 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 | f9eb224f7702c33ecb839323a8c655dff7e793054f97a499c91e9e70ca8bac61 |
|
MD5 | e0731e3dafabb292d37d35e1aa22b1e1 |
|
BLAKE2b-256 | 88dbcd9b698c4910d097e9066319a2697748bc3fee8eb85eec44eae787a35680 |
File details
Details for the file viztracer-0.5.2-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: viztracer-0.5.2-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 673.7 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 | 9ecc8ea6d40b2adbcdbf6f841ba34eafea97c28f8834535639f7d85f0e214dbb |
|
MD5 | 55fa9b1b271ec7e95b99b93ed3f70217 |
|
BLAKE2b-256 | d30d3a380d59158623e59c2f652206758cce25569e3450ec2a51be06dd1985f7 |
File details
Details for the file viztracer-0.5.2-cp38-cp38-manylinux2010_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.2-cp38-cp38-manylinux2010_x86_64.whl
- Upload date:
- Size: 709.0 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 | 091f1e74f90948ee30b0581e4593932f89281920297241443d8fda0930ae4810 |
|
MD5 | a8b9ab2cf930a5291e575643f90487df |
|
BLAKE2b-256 | 5097512b8ef28d91c0a2a3372a3b30219d7d816b68b240f41d676ecf148f5f06 |
File details
Details for the file viztracer-0.5.2-cp38-cp38-manylinux1_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.2-cp38-cp38-manylinux1_x86_64.whl
- Upload date:
- Size: 709.0 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 | 8fa6a551a8325119cf036291b8a9b7f48c0f7f5faec7584fb2f559f6cadb2b5d |
|
MD5 | 9ea18f4be42f70a3a32a4c7d8e4a7217 |
|
BLAKE2b-256 | b28ecd32cbd4f6827f2fcff0d041db18a23b27509b0e9d013ccb89f1ad581aae |
File details
Details for the file viztracer-0.5.2-cp38-cp38-macosx_10_14_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.2-cp38-cp38-macosx_10_14_x86_64.whl
- Upload date:
- Size: 670.4 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 | 1b2862a8071283529d46211f5a39947608c4d0ff0ab4cff6ddd5b190f2f05cce |
|
MD5 | 593918d1ce0c12fafba56868f3aa9e02 |
|
BLAKE2b-256 | b9a932049f360abdeb5cd4dc3c8913a2eb9c8909329b8a9441274d1be923e980 |
File details
Details for the file viztracer-0.5.2-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: viztracer-0.5.2-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 673.6 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 | 7501164cfc10476939da6744998842069b4d2419b4491158011f387587ee8f75 |
|
MD5 | 011ed5607bab62cd8589af189750d7f2 |
|
BLAKE2b-256 | 2146dcc02666abcd10a296f25e27b6cafe1666959110658891c7fa525c37bdc3 |
File details
Details for the file viztracer-0.5.2-cp37-cp37m-manylinux2010_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.2-cp37-cp37m-manylinux2010_x86_64.whl
- Upload date:
- Size: 702.2 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 | 6432e57d783f86e2d11fd583a2eb3fff145f37ab0acf101b189af2a525f6c217 |
|
MD5 | cd4e6f44ff6f34f02da64ec212bc4cd3 |
|
BLAKE2b-256 | 19266880fa524219bc4dac271b82e7964ce77097540356a8e32504a92a31c397 |
File details
Details for the file viztracer-0.5.2-cp37-cp37m-manylinux1_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.2-cp37-cp37m-manylinux1_x86_64.whl
- Upload date:
- Size: 702.2 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 | 1b2506112654cf455564bd1ed52621b298427789999aa2dce6c537a88979b578 |
|
MD5 | e9b623e148c195417ccac7d69b7bf654 |
|
BLAKE2b-256 | 31a93edf7045a99bc7c06e469baf6a89a2f995d22a2c9a879da0a84baaf7536f |
File details
Details for the file viztracer-0.5.2-cp37-cp37m-macosx_10_14_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.2-cp37-cp37m-macosx_10_14_x86_64.whl
- Upload date:
- Size: 670.3 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 | 854a202b42bdc454a528725fe56fc598e62a024acd1f6282bc596872d9253107 |
|
MD5 | 410632df57eeda3ef10832c87038a562 |
|
BLAKE2b-256 | e7ca4acb5638d8e156feda8f4f30991c243d0ce4146ebfb38065eac10c42cfb8 |
File details
Details for the file viztracer-0.5.2-cp36-cp36m-win_amd64.whl
.
File metadata
- Download URL: viztracer-0.5.2-cp36-cp36m-win_amd64.whl
- Upload date:
- Size: 673.6 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 | f46b7e24c115d6857f553a10c766be9dbafc9d2a3c1d5618b2b78f8ace703d3a |
|
MD5 | c5510cfda6c26b55f47ca8cb5498c650 |
|
BLAKE2b-256 | 3e6aaf9190043422b4c6d2dd5a7f2091b418764e9daa669904bceb340c14b79c |
File details
Details for the file viztracer-0.5.2-cp36-cp36m-manylinux2010_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.2-cp36-cp36m-manylinux2010_x86_64.whl
- Upload date:
- Size: 701.3 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 | 993c2def63812b120b7f43d54b70feb880b94f3a0367de398303c6943b7bfe6a |
|
MD5 | 8d1790dd507b39f3917bdb06c2c6cd0e |
|
BLAKE2b-256 | e7202ae1b9ff758ce4fd9f1fe1e6cc1e47b06643957aa95c4e2a53c2a21f4bae |
File details
Details for the file viztracer-0.5.2-cp36-cp36m-manylinux1_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.2-cp36-cp36m-manylinux1_x86_64.whl
- Upload date:
- Size: 701.3 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 | 87245074fa355e8667f329dec0d47f4365b9e87595c61e4748387d1a38d42904 |
|
MD5 | 695613a08077485b86d3be254370948a |
|
BLAKE2b-256 | 12111c48d567a117fe461d2ed46c84e0002ec4898aa2431dfdd8e050d662c818 |
File details
Details for the file viztracer-0.5.2-cp36-cp36m-macosx_10_14_x86_64.whl
.
File metadata
- Download URL: viztracer-0.5.2-cp36-cp36m-macosx_10_14_x86_64.whl
- Upload date:
- Size: 670.3 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 | ac85cdec6067506daf6bb39da064b0cbd8f034684e5adfc7e8813dc2f69d68a1 |
|
MD5 | 122ca1b966ac9bd208b311bb3b212537 |
|
BLAKE2b-256 | 2f7db3de3792364054f7c89b50e2cf45b7d780d7b15cd2618a19c86462609896 |