pyprof2calltree

repository·master·Indexed 41 days ago

https://github.com/pwaller/pyprof2calltree

A tool and API for converting Python cProfile profiling data into a format compatible with the KCacheGrind graphical calltree analyser.

Tokens
595
Snippets
4
Records
4
Agent score
1%

What's inside pyprof2calltree

  1. Install pyprof2calltree

    master

    On Debian-based systems (Debian ≥ 11 or Ubuntu ≥ 20.04), you can install pyprof2calltree and its dependency kcachegrind using apt.

    sudo apt install kcachegrind pyprof2calltree
  2. Use pyprof2calltree with IPython

    master

    In an IPython environment, you can use magic commands to profile code and then use the pyprof2calltree API to view the results. You can pass either the raw stats object returned by %prun or a path to a saved stats file.

    # 1. Profile and visualize from the returned object
    results = %prun -r ElementTree.fromstring(xml_content)
    visualize(results)
    
    # 2. Profile to a file and then convert/visualize that file
    %prun -D out.stats ElementTree.fromstring(xml_content)
    visualize('out.stats')
    convert('out.stats', 'out.kgrind')
  3. Use pyprof2calltree in Python Shell

    master

    You can use the pyprof2calltree API directly in a Python script or interactive shell to visualize cProfile results. The library provides visualize() to open the data in KCacheGrind and convert() to save the results to a file.

    from pyprof2calltree import convert, visualize
    
    # If you have a profiler object from cProfile
    visualize(profiler.getstats())
    
    # To save the results to a file for later use
    convert(profiler.getstats(), 'profiling_results.kgrind')
  4. Use pyprof2calltree via Command Line

    master

    The pyprof2calltree CLI tool allows you to convert Python cProfile data into a format compatible with KCacheGrind. You can run a script directly to collect profiling data or provide an existing stats file.

    # Basic help command
    pyprof2calltree --help
    
    # Example: Run a script and immediately open the results in kcachegrind
    pyprof2calltree -r my_script.py [args]