-
Notifications
You must be signed in to change notification settings - Fork 19
[CI] Add per-file timing report to Mac Metal test job #695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hughperkins
wants to merge
1
commit into
main
Choose a base branch
from
ci/metal-per-file-timing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+90
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Pytest plugin that reports wall-clock time spent per test file. | ||
|
|
||
| Activated by the environment variable QD_FILE_TIMING=1. Collects the "call" phase duration of each test item and | ||
| prints a sorted summary at the end of the session. | ||
|
|
||
| Works correctly with pytest-xdist: the controller process receives forwarded reports from all workers and aggregates | ||
| them here. | ||
|
|
||
| Set QD_FILE_TIMING_OUTPUT to a file path to also write the results as markdown (useful for GitHub Actions job | ||
| summaries). | ||
| """ | ||
|
|
||
| import os | ||
| from collections import defaultdict | ||
|
|
||
| _active = os.environ.get("QD_FILE_TIMING", "0") == "1" | ||
|
|
||
| _file_durations: dict[str, float] = defaultdict(float) | ||
| _file_test_counts: dict[str, int] = defaultdict(int) | ||
|
|
||
|
|
||
| def pytest_runtest_logreport(report): | ||
| if not _active: | ||
| return | ||
| if report.when == "call": | ||
| fspath = report.fspath | ||
| _file_durations[fspath] += report.duration | ||
| _file_test_counts[fspath] += 1 | ||
|
|
||
|
|
||
| def pytest_terminal_summary(terminalreporter, exitstatus, config): | ||
| if not _active: | ||
| return | ||
| if not _file_durations: | ||
| return | ||
|
|
||
| tw = terminalreporter._tw | ||
| tw.sep("=", "per-file timing summary") | ||
| tw.line(f"{'Duration (s)':>12} {'Tests':>6} File") | ||
| tw.sep("-") | ||
|
|
||
| sorted_files = sorted(_file_durations.items(), key=lambda x: -x[1]) | ||
|
|
||
| total = 0.0 | ||
| for fspath, duration in sorted_files: | ||
| count = _file_test_counts[fspath] | ||
| tw.line(f"{duration:12.2f} {count:6d} {fspath}") | ||
| total += duration | ||
|
|
||
| tw.sep("-") | ||
| total_tests = sum(_file_test_counts.values()) | ||
| tw.line(f"{total:12.2f} {total_tests:6d} TOTAL (sum of per-file call durations)") | ||
| tw.sep("=") | ||
|
|
||
| output_path = os.environ.get("QD_FILE_TIMING_OUTPUT") | ||
| if output_path: | ||
| _write_markdown(sorted_files, total, total_tests, output_path) | ||
|
|
||
|
|
||
| def _write_markdown(sorted_files, total, total_tests, path): | ||
| lines = [ | ||
| "### Per-file test timing", | ||
| "", | ||
| "| Duration (s) | Tests | File |", | ||
| "|---:|---:|:---|", | ||
| ] | ||
| for fspath, duration in sorted_files: | ||
| count = _file_test_counts[fspath] | ||
| basename = os.path.basename(fspath) | ||
| lines.append(f"| {duration:.2f} | {count} | `{basename}` |") | ||
| lines.append(f"| **{total:.2f}** | **{total_tests}** | **TOTAL** |") | ||
| lines.append("") | ||
|
|
||
| with open(path, "a") as f: | ||
| f.write("\n".join(lines)) |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the Mac job exports
QD_FILE_TIMING=1, this adds-p pytest_file_timingto everytests/run_tests.pyinvocation, but the new plugin lives intests/python/pytest_file_timing.pywhile the script is executed aspython tests/run_tests.py; at pytest plugin-import time only the repo root/testsscript directory are onsys.path, nottests/python. I verified the enabled path fails during pytest configuration withImportError: Error importing plugin "pytest_file_timing": No module named 'pytest_file_timing', so the new Mac test job will abort before collecting tests unless the plugin is moved/imported from an importable location or the path is added before calling pytest.Useful? React with 👍 / 👎.