Skip to content

Profiling

jsangmeister edited this page Apr 5, 2022 · 4 revisions

Creating the profiling file

Run pytest --profile (you can also use any other pytest option you like). This creates a prof directory with pstats files for each test along with a combined.prof file, which contains all results combined into one file.

If you do not want the whole overhead of pytest and the setup included in the data, you can also call the profiler programmatically, e.g. with:

import cProfile
cProfile.runctx("some_method()", globals(), locals(), filename="profile.out")

This will execute some_method in the current context and put the results into profile.out.

Analyzing the output

(Hint: you can copy the file to your host system with docker cp <containerId>:/file/path/within/container /host/path/target)

You can either analyze the files with the python pstats module (https://docs.python.org/3/library/profile.html#module-pstats), e.g. by running:

import pstats
p = pstats.Stats('profile')
p.strip_dirs()
p.sort_stats('cumtime')
p.print_stats(50)

To get a visual representation, gprof2dot (https://github.com/jrfonseca/gprof2dot) is the program of choice. See the github page for installation and requirements. You can then convert your pstats file into a call graph with the following command:

gprof2dot -f pstats <filename> | dot -Tpng -o output.png

This graph can then be used for further analysis.

Clone this wiki locally