Skip to content

Commit

Permalink
feat($profile): sort profile HTML by original Python package
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Sep 5, 2022
1 parent 75b8ec5 commit 5e7a651
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path

import pytest
from _pytest.nodes import Node
from loguru import logger
from pyinstrument import Profiler

Expand All @@ -12,9 +13,7 @@ def auto_profile(request):
"""
Generate a HTML file for each test node in your test suite inside the .profiles directory.
Links:
* https://pyinstrument.readthedocs.io/en/latest/guide.html#profile-pytest-tests
* https://docs.pytest.org/en/latest/how-to/fixtures.html?highlight=conftest#scope-sharing-fixtures-across-classes-modules-packages-or-session
https://pyinstrument.readthedocs.io/en/latest/guide.html#profile-pytest-tests
"""

# noinspection PyPep8Naming
Expand All @@ -27,8 +26,13 @@ def auto_profile(request):
yield # Run test

profiler.stop()
PROFILE_ROOT.mkdir(exist_ok=True)
results_file = PROFILE_ROOT / f"{request.node.name}.html"
node: Node = request.node
profile_html_path = PROFILE_ROOT / f"{node.path.parent.relative_to(TESTS_ROOT)}"
if not profile_html_path.exists():
# If parents is false (the default), a missing parent raises FileNotFoundError.
# If exist_ok is false (the default), FileExistsError is raised if the target directory already exists.
profile_html_path.mkdir(parents=True, exist_ok=True)
results_file = profile_html_path / f"{node.name}.html"
with open(results_file, "w", encoding="utf-8") as f_html:
f_html.write(profiler.output_html())
logger.info(f"Generated result: [{results_file}]")
logger.info(f"Generated profile result: [{results_file}]")

0 comments on commit 5e7a651

Please sign in to comment.