I'm profiling a Python application with cProfile
, and I find its output to be quite verbose. I am using this code to create the profile and visualize it:
PYTHONPATH=. \
python3 \
-m cProfile \
-s cumtime \
-o output.cprof \
foo/__main__.py && \
gprof2dot \
-f pstats \
output.cprof | \
dot -Tpdf -o output.pdf
For example, here's part of a graph that I've created with gprof2dot
:
Obviously, these are methods I am not interested in. I know, however, the name of the function I want to inspect, let's call it foo:xxx:my_function
. How can I get the cProfile output filtered so that I can only look at this function and its callees?
I've seen this question, but it only talks about looking at the head
of the output.
Fortunately, gprof2dot
has an option to filter function names.
So, I can just use -z
with the name of the function on the node to see the descendants only:
python3 \
-m cProfile \
-s cumtime \
-o output.cprof \
foo/__main__.py && \
gprof2dot \
-f pstats \
-z "foo:xxx:my_function" \
output.cprof | \
dot -Tpdf -o output.pdf