PYPROF2CALLTREE(1)

πŸ“¦
version 1.4.5 Β· 2 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble

πŸ’– PYPROF2CALLTREE πŸ’–

okay bestie, welcome to the glow-up of Pyprof2calltree (that's the title, that's the name, that's the whole vibe) β€” it's a General Commands Manual girly and she lives in section 1. Let's get into it ✨


🌸 NAME 🌸

Pyprof2calltree β€” visualize Python cProfile data in kcachegrind πŸ¦‹


✨ DESCRIPTION ✨

So like, this is a script that helps you visualize profiling data that you collected with the cProfile Python module, using the kcachegrind graphical calltree analyser. That's the whole point of her existence and she's serving it πŸ’…

This is a rebranding of the venerable http://www.gnome.org/~johan/lsprofcalltree.py script by David Allouche et Al. She's giving it a whole new era because:

  • she wants to be easier to distribute (like, e.g. through PyPI, no big deal πŸŽ€)
  • she wants to behave more like the scripts of the debian kcachegrind-converters package

And the final goal? Making her part of the official upstream kdesdk package. Main character energy, honestly πŸ‘‘


πŸ’… OPTIONS πŸ’…

Upon installation you should have a pyprof2calltree script in your path, and she will literally tell you all about herself:

$ pyprof2calltree --help
usage: pyprof2calltree [-h] [-o output_file_path] [-i input_file_path] [-k]
                       [-r scriptfile [args ...]]

optional arguments:
  -h, --help            show this help message and exit
  -o output_file_path, --outfile output_file_path
                        Save calltree stats to <outfile>
  -i input_file_path, --infile input_file_path
                        Read Python stats from <infile>
  -k, --kcachegrind     Run the kcachegrind tool on the converted data
  -r scriptfile [args ...], --run-script scriptfile [args ...]
                        Name of the Python script to run to collect profiling
                        data

and here's the breakdown, one by one, no skips, we don't do that here 🫢

  • -h, --help β€” show this help message and exit. the classic, the icon, the "wait what does this do again" button 🌈
  • -o output_file_path, --outfile output_file_path β€” save calltree stats to <outfile> πŸ’Ύ
  • -i input_file_path, --infile input_file_path β€” read Python stats from <infile> πŸ“₯
  • -k, --kcachegrind β€” run the kcachegrind tool on the converted data πŸš€
  • -r scriptfile [args ...], --run-script scriptfile [args ...] β€” name of the Python script to run to collect profiling data. yes she will run it for you, and yes you can pass args ... πŸ“

🐍 PYTHON SHELL USAGE 🐍

ngl, pyprof2calltree is also best used from an interactive Python shell, like the default shell. for instance, let's profile XML parsing, because why not 😌

>>> from xml.etree import ElementTree
>>> from cProfile import Profile
>>> xml_content = '<a>\n' + '\t<b/><c><d>text</d></c>\n' * 100 + '</a>'
>>> profiler = Profile()
>>> profiler.runctx(
...     "ElementTree.fromstring(xml_content)",
...     locals(), globals())

>>> from pyprof2calltree import convert, visualize
>>> visualize(profiler.getstats())                            # run kcachegrind
>>> convert(profiler.getstats(), 'profiling_results.kgrind')  # save for later

or with the ipython:

In [1]: %doctest_mode
Exception reporting mode: Plain
Doctest mode is: ON

>>> from xml.etree import ElementTree
>>> xml_content = '<a>\n' + '\t<b/><c><d>text</d></c>\n' * 100 + '</a>'
>>> %prun -D out.stats ElementTree.fromstring(xml_content)

*** Profile stats marshalled to file 'out.stats'

>>> from pyprof2calltree import convert, visualize
>>> visualize('out.stats')
>>> convert('out.stats', 'out.kgrind')

>>> results = %prun -r ElementTree.fromstring(xml_content)
>>> visualize(results)
ok that's the whole page bestie, go profile something and make it pretty πŸ’œ

and that's a wrap!! this whole thing is from the General Commands Manual, she's PYPROF2CALLTREE(1) and she ate, no crumbs, periodt πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά

πŸ“¦ where this page comes from