Skip to main content

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

Project description

VizTracer

build pypi license commit

VizTracer is a deterministic debugging/profiling tool that can trace and visualize python code. The major data VizTracer displays is FEE(function entry/exit), or equivalently, the call stack.

Unlike traditional flame graph, which is normally generated by sampling profiler, VizTracer can display every function executed and the corresponding entry/exit time from the beginning of the program to the end, which is helpful for programmers to catch sporatic performance issues.

With VizTracer, the programmer can intuitively understand what their code is doing and how long each function takes.

You can take a look at the demo result of an example program running recursive merge and quick sort algorithm.

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

Requirements

VizTracer requires python 3.6+. No other package is needed. For now, VizTracer only supports CPython + Linux/MacOS.

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 it through command line. Assume you have a python script to profile and the normal way to run it is:

python3 my_script.py

You can simply use VizTracer as

python3 -m viztracer my_script.py

which will generate a result.html file in the directory you run this command. Open it in browser and there's your result.

If your script needs arguments like

python3 my_script.py arg1 arg2

Just feed it as it is to VizTracer

python3 -m viztracer my_script.py arg1 arg2

You can also specify the tracer to be used in command line by passing --tracer argument. c tracer is the default value, you can use python tracer instead

python3 -m viztracer --tracer c my_script.py
python3 -m viztracer --tracer python my_script.py

You can specify the output file using -o or --output_file argument. The default output file is result.html. Two types of files are supported, html and json.

python3 -m viztracer -o other_name.html my_script.py
python3 -m viztracer -o other_name.json 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.

First of all, you need to import VizTracer class from the package, and make an object of it.

from viztracer import VizTracer

tracer = VizTracer()

If your code is executable by exec function, you can simply call tracer.run()

tracer.run("import random;random.randrange(10)")

This will as well generate a result.html file in your current directory. You can pass other file path to the function if you do not like the name result.html

tracer.run("import random; random.randrange(10)", output_file = "better_name.html")

When you need a more delicate profiler, you can manually enable/disable the profile using start() and stop() function.

tracer.start()
# Something happens here
tracer.stop()
tracer.save() # also takes output_file as an optional argument

With this method, you can only record the part that you are interested in

# Some code that I don't care
tracer.start()
# Some code I do care
tracer.stop()
# Some code that I want to skip
tracer.start()
# Important code again
tracer.stop()
tracer.save()

Or, you can do it with with statement

with VizTracer(output_file="optional.html") as tracer:
    # Something happens here

It is higly recommended that start() and stop() function should be in the same frame(same level on call stack). Problem might happen if the condition is not met

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.

At the moment, perfetto did not support locally stand alone HTML file generation, 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.

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 viztrace to filter out the data you don't need.

The filter mechanism only works in C tracer, and it 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.

Currently we support two kinds of filters:

max_stack_depth

max_stack_depth is a straight forward way to filter your data. It limits the stack depth viztracer will trace, which cuts out deep call stacks, including some nasty recursive calls.

You can specify max_stack_depth in command line:

python3 -m viztracer --max_stack_depth 10 my_script.py

Or you can pass it as an argument to the VizTracer object:

from viztracer import VizTracer

tracer = VizTracer(max_stack_depth=10)

include_files and exclude_files

There are cases when you are only interested in functions in certain files. You can use include_files and exclude_files feature to filter out data you are not insterested in.

When you are using include_files, only the files and directories you specify are recorded. Similarly, when you are using exclude_files, files and directories you specify will not be recorded.

IMPORTANT: include_files and exclude_files can't be both spcified. You can only use one of them.

If a function is not recorded based on include_files or exclude_files rules, none of its descendent functions will be recorded, even if they match the rules

You can specify include_files and exclude_files in command line, but they can take more than one argument, which will make the following command ambiguous:

# Ambiguous command which should NOT be used
python3 -m viztracer --include_files ./src my_script.py

Instead, when you are using --include_files or --exclude_files, --run should be passed for the command that you actually want to execute:

# --run is used to solve ambiguity
python3 -m viztracer --include_files ./src --run my_script.py

However, if you have some other commands that can separate them and solve ambiguity, that works as well:

# This will work too
python3 -m viztracer --include_files ./src --max_stack_depth 5 my_script.py

You can also pass a list as an argument to VizTracer:

from viztracer import VizTracer

tracer = VizTracer(include_files=["./src", "./test/test1.py"])

Choose Tracer

The default tracer for current version is c tracer, which introduce a relatively small overhead(worst case 2-3x) but only works for CPython on Linux. However, if there's other reason that you would prefer a pure-python tracer, you can use python tracer using tracer argument when you initialize VizTracer object.

tracer = VizTracer(tracer="python")

python tracer will be deprecated because of the performance issue in the future

Cleanup of c Tracer

The interface for c trace is almost exactly the same as python tracer, except for the fact that c tracer does not support command line run now. However, to achieve lower overhead, some optimization is applied to c tracer so it will withhold the memory it allocates for future use to reduce the time it calls malloc(). If you want the c trace to free all the memory it allocates while collecting trace, use

tracer.cleanup()

Add Custom Event

VizTracer supports custom event added while the program is running. This works like a print debug, but you can know when this print happens while looking at trace data.

When your code is running with VizTracer on, you can use

tracer.add_instant(name, args, scope)

to add an event that will be shown in the report. In trace viewer, you would be able to see what args is.

name should be a string for this event. args should be a json serializable object(dict, list, string, number). scope is an optional argument, default is "g" for global. You can use p for process or t for thread. This affects how long the event shows in the final report.

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

Performance

Overhead is a big consideration when people choose profilers. VizTracer now has a similar overhead as native cProfiler. It works slightly worse in the worst case(Pure FEE) and better in easier case because even though it collects some extra information than cProfiler, the structure is lighter.

Admittedly, VizTracer is only focusing on FEE now, so cProfiler also gets other information that VizTracer does not acquire.

An example run for test_performance with Python 3.8 / Ubuntu 18.04.4 on Github VM

fib       (10336, 10336): 0.000852800 vs 0.013735200(16.11)[py] vs 0.001585900(1.86)[c] vs 0.001628400(1.91)[cProfile]
hanoi     (8192, 8192): 0.000621400 vs 0.012924899(20.80)[py] vs 0.001801800(2.90)[c] vs 0.001292900(2.08)[cProfile]
qsort     (10586, 10676): 0.003457500 vs 0.042572898(12.31)[py] vs 0.005594100(1.62)[c] vs 0.007573200(2.19)[cProfile]
slow_fib  (1508, 1508): 0.033606299 vs 0.038840998(1.16)[py] vs 0.033270399(0.99)[c] vs 0.032577599(0.97)[cProfile]

Limitations

VizTracer uses sys.setprofile() for its profiler capabilities, so it will conflict with other profiling tools which also use this function. Be aware of it when using VizTracer.

Bugs/Requirements

Please send bug reports and feature requirements 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.1.3.tar.gz (656.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

viztracer-0.1.3-cp38-cp38-manylinux2010_x86_64.whl (682.6 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

viztracer-0.1.3-cp38-cp38-manylinux1_x86_64.whl (682.6 kB view details)

Uploaded CPython 3.8

viztracer-0.1.3-cp38-cp38-macosx_10_14_x86_64.whl (659.1 kB view details)

Uploaded CPython 3.8macOS 10.14+ x86-64

viztracer-0.1.3-cp37-cp37m-manylinux2010_x86_64.whl (681.3 kB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

viztracer-0.1.3-cp37-cp37m-manylinux1_x86_64.whl (681.3 kB view details)

Uploaded CPython 3.7m

viztracer-0.1.3-cp37-cp37m-macosx_10_14_x86_64.whl (659.1 kB view details)

Uploaded CPython 3.7mmacOS 10.14+ x86-64

viztracer-0.1.3-cp36-cp36m-manylinux2010_x86_64.whl (680.3 kB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.12+ x86-64

viztracer-0.1.3-cp36-cp36m-manylinux1_x86_64.whl (680.3 kB view details)

Uploaded CPython 3.6m

viztracer-0.1.3-cp36-cp36m-macosx_10_14_x86_64.whl (659.1 kB view details)

Uploaded CPython 3.6mmacOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: viztracer-0.1.3.tar.gz
  • Upload date:
  • Size: 656.8 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.1.3.tar.gz
Algorithm Hash digest
SHA256 8c41fe6f75cdb4e3dfb39cd2133bd95dc077baaf98bf4f1b47a0054d602c1493
MD5 a7f4d6f512d3e7b8cf523579577e379f
BLAKE2b-256 749796ee60119ecf61fdc15088ed83e4deaf30c5908f59348e667325c9bbaa75

See more details on using hashes here.

File details

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

File metadata

  • Download URL: viztracer-0.1.3-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 682.6 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.1.3-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 aa3c7cceccc15bad16f34288f38ac8483823f0e37b02101c27b76478cd30c9cd
MD5 9a7d037a85d45c6a7e0b83bed485dc52
BLAKE2b-256 a68930b622943589ae52445cedb66a2bd3fcc0a6cc0e04b65f32b11989ec376b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: viztracer-0.1.3-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 682.6 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.1.3-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9f7098dd752db785050a78ed30ee8ee24312fd3f4d90a6b70e6f512e4582cc02
MD5 09bf8531d13f540873dcd6c4ae888071
BLAKE2b-256 cf56af18f499b7b647b60980b5cfc2d92cabe7dcbefee9e1975c348a5d96d8c0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: viztracer-0.1.3-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 659.1 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.1.3-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 85c0e878e0b50a2b58ce61f731df283a06f75e725a2c954cdd7b59fefa508264
MD5 251f0f19ff75b062733bca2d2be5a656
BLAKE2b-256 d2b0741612bf0f09b2cc388e82de65553738feef7beb960ae47a0c99b10c9d96

See more details on using hashes here.

File details

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

File metadata

  • Download URL: viztracer-0.1.3-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 681.3 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.1.3-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 d235212d007f1ca60020d4112431fa25936536bab949e3fbb6aeef554f701b9a
MD5 84e69e375156a417eb31d934b3fc3100
BLAKE2b-256 c11974a33d60181591170105cb7563c8cbd39fed1313c4971302e1b42843361e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: viztracer-0.1.3-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 681.3 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.1.3-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3e8ec0f358a79168c1b314515812cbf185dde1201267799fb0370455d5a83bd4
MD5 55dabd6a41bd76fe16c8a4e6b60022d2
BLAKE2b-256 ff5368b52b4e453a67d133a15a8826f84fe1f1cb1a256fedf2df9a84c3624848

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for viztracer-0.1.3-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 49afb7aa21d6a1c8d5d79c0f3ea812d6cc9c5a8b41358a7984f947372c6c3cb9
MD5 e178a9a24f93b6047d82298520b70eeb
BLAKE2b-256 e8b0a91c67a84894e764b3b286665dbaabe53a81a1aca10ad9385452eac211eb

See more details on using hashes here.

File details

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

File metadata

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

Hashes for viztracer-0.1.3-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 b39666d33cfedac600657818a9db10b802da680d9fbf8ec3efb43b1515ab19e2
MD5 bc6d9104f83273721ca11604ee41be9f
BLAKE2b-256 0b22c91a68c257ab0b3b4284734a1fdcb8cc06eab3864bc9160204b0bad89f3a

See more details on using hashes here.

File details

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

File metadata

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

Hashes for viztracer-0.1.3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c5a347a540b57be8cfedfe4e25bc8a3135052611424bfc089d4203bfee121dd6
MD5 4e17504eaee3a257e4a5f5d88300619f
BLAKE2b-256 4af50a94d43f2281c9ba52935be7722f4b3f66d4803821a38b04c7bd2c3d7cf0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for viztracer-0.1.3-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 24843a52b401728a7ee82dd5bbb4ea4ae3eb3eb3963e964b95026e5d428cf893
MD5 52014e7aeeb430049c2b3024a06143c4
BLAKE2b-256 2ca7c8104952e4d19aa0f8bb6f6b7eb7d9fbb34675bca2e3b7520d6dcc41b218

See more details on using hashes here.

Supported by

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