Last active
December 1, 2021 19:28
-
-
Save koffie/db525739c125bccb778e9415a9a0870e to your computer and use it in GitHub Desktop.
A pytest fixture together with a makefile that allow you generate nice profiling graphs from tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile, os, pathlib | |
from pytest import fixture | |
PROFILING_DIR = pathlib.Path("/tmp") # wherever you want the resulting files to show up | |
@fixture() | |
def profile(request): | |
""" | |
Use this fixture in a test to profile the test. The test results end up as | |
pstats files in: | |
PROFILING_DIR/<testmodule_name>/<testcase_name>.pstats | |
The corresponding make file contains a command to automatically generate | |
nice graphs out of all these pstats files. | |
""" | |
function = request.function | |
test_names = [key for key in request.keywords if key.startswith(function.__name__)] | |
test_name = test_names[0] if test_names else function.__name__ | |
with cProfile.Profile() as pr: | |
yield test_name | |
test_dir = PROFILING_DIR.joinpath(function.__module__) | |
try: | |
os.mkdir(test_dir) | |
except FileExistsError: | |
pass | |
pr.dump_stats(test_dir.joinpath(f"{test_name}.pstats")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requires gprof2dot and dot to be installed | |
PROFILING_DIR=/tmp | |
PSTATS = $(wildcard ${PROFILING_DIR}/**/*.pstats) | |
PNG = $(PSTATS:.pstats=.png) | |
.PHONY: profile | |
profile: $(PNG) | |
$(PROFILING_DIR)/%.png: $(PROFILING_DIR)/%.pstats | |
gprof2dot -f pstats $< | dot -Tpng -o $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment