Skip to content

Commit

Permalink
feat($pytest): integrate pyinstrument to profile test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Aug 30, 2022
1 parent 144b371 commit 513d456
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,7 @@ dmypy.json
# Pyre type checker
.pyre/

# pyinstrument
.profiles/

# End of https://www.gitignore.io/api/python
7 changes: 2 additions & 5 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,22 @@ loguru = "==0.6.0"
# pyhocon is a HOCON parser for Python. Additionally we provide a tool (pyhocon) to convert any HOCON content into json,
# yaml and properties format. https://pypi.org/project/pyhocon/
pyhocon = "==0.3.59"

# Data manipulation dependencies
# NumPy is a Python library used for working with arrays. https://pypi.org/project/numpy/
numpy = "==1.23.2"
# pandas is a Python package that provides fast, flexible, and expressive data structures designed to make working
# with "relational" or "labeled" data both easy and intuitive. https://pypi.org/project/pandas/
pandas = "==1.4.3"

# Python enhancement dependencies
# Arrow is a Python library that offers a sensible and human-friendly approach to creating, manipulating,
# formatting and converting dates, times and timestamps. https://pypi.org/project/arrow/
arrow = "==1.2.2"

# ORM dependencies
# Peewee is a simple and small ORM. https://pypi.org/project/peewee/
peewee = "==3.15.1"

# Template engine dependencies
# Jinja is a fast, expressive, extensible templating engine. https://pypi.org/project/Jinja2/
jinja2 = "==3.1.2"

# Tool dependencies
# Faker is a Python package that generates fake data for you. https://pypi.org/project/Faker/
faker = "==14.1.0"
Expand All @@ -61,3 +56,5 @@ pytest = "==7.1.2"
pytest-mock = "==3.8.2"
# This plugin produces coverage reports. https://pypi.org/project/pytest-cov/
pytest-cov = "==3.0.0"
# Call stack profiler for Python. Shows you why your code is slow!
pyinstrument = "==4.3.0"
91 changes: 89 additions & 2 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pathlib import Path

import pytest
from loguru import logger
from pyinstrument import Profiler

TESTS_ROOT = Path.cwd()


@pytest.fixture(autouse=True)
def auto_profile(request):
"""
Generate a HTML file for each test node in your test suite inside the .profiles directory.
https://pyinstrument.readthedocs.io/en/latest/guide.html#profile-pytest-tests
"""

# noinspection PyPep8Naming
PROFILE_ROOT = TESTS_ROOT / ".profiles"
logger.info("Starting to profile Pytest unit tests...")
# Turn profiling on
profiler = Profiler()
profiler.start()

yield # Run test

profiler.stop()
PROFILE_ROOT.mkdir(exist_ok=True)
results_file = PROFILE_ROOT / f"{request.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}]")

0 comments on commit 513d456

Please sign in to comment.