See also

Given a python program main.py:

  1. python -m cProfile -o main.py.profile main.py
    • If you need to pass args to main.py, do so after its appearance in this command
  2. gprof2dot -f pstats main.py.profile -o main.py.dot
  3. dot -Tpng main.py.dot

Open main.py.png and viola!

Call-graph profiling in unit tests

Using pytest-profiling, you can automate this process as part of your (pytest-driven) unit tests:

  1. Install the plugin either as part of the normal dependency process or directly python -m pip install pytest-profiling
  2. Run unit tests with the --profile-svg argument to enable the plugin: python -m pytest --profile-svg ...

Your unit tests should output a file named prof/combined.svg. This is the same heat-graph diagram.