Skip to main content

A debugging and profiling tool that can trace and visualize python code execution

Project description

VizTracer

build pypi support-version license commit

VizTracer is a low-overhead deterministic debugging/profiling 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.)

example_img

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
  • Customize 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

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.

At the moment, perfetto does not support locally stand alone HTML file generation and it has some bugs, so I'm not able to switch completely to it. The good news is that once you load the perfetto page, you can use it even when you are offline.

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:

  • max stack depth
  • include files
  • exclude files
  • ignore c function

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:

  • Instant Event
  • Counter Event
  • Object Event

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.

example_img

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.

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

viztracer-0.3.4.tar.gz (662.6 kB view details)

Uploaded Source

Built Distributions

viztracer-0.3.4-cp38-cp38-manylinux2010_x86_64.whl (696.9 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

viztracer-0.3.4-cp38-cp38-manylinux1_x86_64.whl (696.9 kB view details)

Uploaded CPython 3.8

viztracer-0.3.4-cp38-cp38-macosx_10_14_x86_64.whl (667.6 kB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

viztracer-0.3.4-cp37-cp37m-manylinux2010_x86_64.whl (693.9 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

viztracer-0.3.4-cp37-cp37m-manylinux1_x86_64.whl (693.9 kB view details)

Uploaded CPython 3.7m

viztracer-0.3.4-cp37-cp37m-macosx_10_14_x86_64.whl (667.5 kB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

viztracer-0.3.4-cp36-cp36m-manylinux2010_x86_64.whl (692.9 kB view details)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

viztracer-0.3.4-cp36-cp36m-manylinux1_x86_64.whl (692.9 kB view details)

Uploaded CPython 3.6m

viztracer-0.3.4-cp36-cp36m-macosx_10_14_x86_64.whl (667.5 kB view details)

Uploaded CPython 3.6m macOS 10.14+ x86-64

File details

Details for the file viztracer-0.3.4.tar.gz.

File metadata

  • Download URL: viztracer-0.3.4.tar.gz
  • Upload date:
  • Size: 662.6 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

Hashes for viztracer-0.3.4.tar.gz
Algorithm Hash digest
SHA256 dbb8fbbdb1384ff087a18e4c0fbe0ef0146efcd0e5898e24cc29fa8159a56adb
MD5 1d572698709e17839727080c9f55ae3a
BLAKE2b-256 067ce87176616739a48c56f1569e07c6381e07936351acf1bb5472ebec475ff7

See more details on using hashes here.

File details

Details for the file viztracer-0.3.4-cp38-cp38-manylinux2010_x86_64.whl.

File metadata

  • Download URL: viztracer-0.3.4-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 696.9 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

Hashes for viztracer-0.3.4-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 111777a76439d783862afb9136c6be5944c7e0e630a2163372755aaf3abb909a
MD5 cd6f373044c6d33e9637974579d28751
BLAKE2b-256 bd53d3fd63319c05e4311739a5a7dfca3739c0914d5e6da1ce0d4bbee4d9e299

See more details on using hashes here.

File details

Details for the file viztracer-0.3.4-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: viztracer-0.3.4-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 696.9 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

Hashes for viztracer-0.3.4-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 037ff479c4822d384557137fb744c18d2cd1f2ec37d7faa36aa7b21144c810bf
MD5 4ef9180c8e45aefbbbd425ed7f1f6978
BLAKE2b-256 dc055a2b9b497ab5bc75800878cc45b0181aec1e7622fcda0afcd40e1dbbae74

See more details on using hashes here.

File details

Details for the file viztracer-0.3.4-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: viztracer-0.3.4-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 667.6 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

Hashes for viztracer-0.3.4-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ea6a1b0305b964f8c4410493ad23104f440260810775ecdf11c3dc39b3ccfc69
MD5 d640d9ad288ecec6cba75b450a7b722d
BLAKE2b-256 f7e9903a272a6b8d3c55b464ad74174e7909c09d2f250f29c9140ae8f7fd0d39

See more details on using hashes here.

File details

Details for the file viztracer-0.3.4-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: viztracer-0.3.4-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 693.9 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

Hashes for viztracer-0.3.4-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 cd8e870cc9df60c15d0dcb55dfc57839be8a0460aa61be26700982fdef3b01da
MD5 a6853447e66041cf2cb58b58b5d59e86
BLAKE2b-256 847a8d82eeb1151ba2a906ea1f2e57948edb255fd2eacf163f9c7f068e61cb3b

See more details on using hashes here.

File details

Details for the file viztracer-0.3.4-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: viztracer-0.3.4-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 693.9 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

Hashes for viztracer-0.3.4-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 77fef06d90d74efcc314357284024fcc4fbaea8bc0a75e988d2f4d198e2eff51
MD5 9579717799c8d6ed6e8ebbbe47628857
BLAKE2b-256 6be7725525c62645bded3654753b33747606cf3cc1241d7fda5781bfaab3fa1b

See more details on using hashes here.

File details

Details for the file viztracer-0.3.4-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: viztracer-0.3.4-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 667.5 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

Hashes for viztracer-0.3.4-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2bda045dea21fa484e3873e7eb94083b954a93fb987e16a7c126158db05c0ee3
MD5 8571f2c460cd297d1a1144e4eda2fe9d
BLAKE2b-256 8f364c1899fb62b3d502102f22de683ce7a9e3e38c736e5df32e2d0b57157dd2

See more details on using hashes here.

File details

Details for the file viztracer-0.3.4-cp36-cp36m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: viztracer-0.3.4-cp36-cp36m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 692.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

Hashes for viztracer-0.3.4-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 b9713dc1f4616a601e21b872f48f37227ad30f59b6b46d5889df3be5bbc45ddf
MD5 9dcdf8827e928284ceac28b517118118
BLAKE2b-256 75d5521b2ba382c4e54518e530fe0feeb2a696649e38e749dc8245b3af33b5a5

See more details on using hashes here.

File details

Details for the file viztracer-0.3.4-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: viztracer-0.3.4-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 692.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

Hashes for viztracer-0.3.4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f5c107d6200c2ab9edb56f922cfdf34caffdf35f80e23e91068bb69db6ca4533
MD5 8d7854076d36871924c39980e4021207
BLAKE2b-256 37c43c5ad75333e80ad0d30d79adb1542183299d59460ed6ba026a4f1bcf15a2

See more details on using hashes here.

File details

Details for the file viztracer-0.3.4-cp36-cp36m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: viztracer-0.3.4-cp36-cp36m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 667.5 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

Hashes for viztracer-0.3.4-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 0713ece593243b6a26cd297d5084bb3248281962d3cdc358cc04e111c8b95d20
MD5 946cfefb6f2504dd61c7a0ded73c812f
BLAKE2b-256 c8418fa67bb3468ca34a7458a48204213e8bbfae248be80d7906373bd12ae3f0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page