pythonprofilingcprofilepstats

Programmatically accessing cProfile data


I need to capture some python profiling data and generate a report.

While most of this component runs Python 3.5, to support a couple of legacy modules, we still have part of the system that runs 2.7.

In this instance, 3.5 invokes 2.7 via subprocess.Popen() and captures both stdout and stderr (which serve as data and log respectively).

I'm able to run cProfile inside the 2.7 process, write the resulting data to disk, and load it into pStats.

At this point, I can sort/filter and print to console. Unfortunately, as stdout/stderr are already in use, this isn't much help.

I'd also prefer a chance to format, inject source code snippets, etc.

So I need to interrogate the pStats class programmatically, build up a suitable report and then store it in our messaging system.

The documentation doesn't seem to indicate any way to extract data other that writing to console.

the .stats property looks promising but only contains an array of 3-tuples which omit lots of information.

How can I browse the data in pStats programmatically?

Specifically, I want to extract as a minimum:


Solution

  • Not strictly a complete answer, but you might want to check out the SnakeViz library (with which I am not affiliated). They do some visualisations of cProfile output, which presumably means they have a parser you might be able to hijack.