Skip to content

Commit

Permalink
chore: Add a profile task
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Oct 9, 2021
1 parent d7cac7a commit 7bb2213
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -14,3 +14,5 @@ pdm.lock
.pdm.toml
__pypackages__/
.venv/
profile.pstats
profile.svg
4 changes: 3 additions & 1 deletion Makefile
Expand Up @@ -8,6 +8,7 @@ check_code_quality_args = files
docs_serve_args = host port
release_args = version
test_args = match
profile_args = async browser

BASIC_DUTIES = \
changelog \
Expand All @@ -18,7 +19,8 @@ BASIC_DUTIES = \
docs-regen \
docs-serve \
format \
release
release \
profile

QUALITY_DUTIES = \
check \
Expand Down
38 changes: 35 additions & 3 deletions duties.py
Expand Up @@ -290,13 +290,45 @@ def test(ctx, match: str = ""):
ctx: The context instance (passed automatically).
match: A pytest expression to filter selected tests.
"""
nofail = sys.version_info >= (3, 11, 0)
py_version = f"{sys.version_info.major}{sys.version_info.minor}"
os.environ["COVERAGE_FILE"] = f".coverage.{py_version}"
ctx.run(
["pytest", "-c", "config/pytest.ini", "-n", "auto", "-k", match, "tests"],
title="Running tests",
pty=PTY,
nofail=nofail,
quiet=nofail,
)


@duty
def profile(ctx, browser: bool = False, **opts):
"""
Run the test suite.
Arguments:
ctx: The context instance (passed automatically).
browser: Whether to open the SVG file in the browser at the end.
**opts: Additional options: async.
"""
async_loader = opts.pop("async", False)
griffe_opts = ["-A"] if async_loader else []
packages = ctx.run(
"find ~/.cache/pdm/packages -maxdepth 4 -type f -name __init__.py -exec dirname {} +", # noqa: P103
title="Finding packages",
).split("\n")
ctx.run(
[
sys.executable,
"-mcProfile",
"-oprofile.pstats",
"-m",
"griffe",
"-o/dev/null",
*griffe_opts,
*packages,
],
title=f"Profiling in {'async' if async_loader else 'sync'} mode on {len(packages)} packages",
pty=False,
)
ctx.run("gprof2dot profile.pstats | dot -Tsvg -o profile.svg", title="Converting to SVG")
if browser:
os.system("/usr/bin/firefox profile.svg 2>/dev/null &") # noqa: S605

0 comments on commit 7bb2213

Please sign in to comment.