mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-11-25 23:16:18 +01:00
The single test job ran all tests one after the other on one runner,
including the Sphinx build and the Docker checks (11-19 min). It is now
split so that every test still runs exactly once:
- docs-test, docker-test: the Sphinx build (new marker "docs") and the
Docker / Home Assistant add-on checks ("docker") in jobs of their own.
- test-server: the tests that start EOS or EOSdash on the fixed ports.
The collection hook marks every test that uses the xprocess fixture
with "eos_server" and puts them into one xdist group, so
"pytest -n auto --dist loadgroup" also works locally. A test that
binds the ports without a fixture carries the marker itself.
- test-shard: all other tests in four shards of about equal runtime
(scripts/shard_test_files.py, runtimes in scripts/test_durations.json),
each with pytest-xdist.
- test-count: fails if these parts do not add up to the tests of one
plain run.
- test: keeps the name of the required check. It prints the combined
coverage report and fails if any of the jobs above failed.
COVERAGE_CORE=sysmon measures line coverage through sys.monitoring:
the report is the same, the test time drops by about a third. The
combined report misses 1-3 statements more than a single run, in lines
that only run when a test finds state left by an earlier test.
The Sphinx build spent 74 % of its time in one search of
PythonDomain.find_obj (sphinx-doc/sphinx#11282). The local extension
docs/_ext/pyxrefindex.py answers it from an index of name endings with
the same matches in the same order; a build has a byte-identical
objects.inv. It copies the logic of Sphinx 9.0.4 and is only active for
that version; TestPyXrefIndex compares it with the installed Sphinx and
fails after a Sphinx update until the version is raised. The
documentation test builds with -j (up to four jobs); its pages can
differ from a serial build in the target of ambiguous cross-references.
Read the Docs builds without -j. Sphinx build on GitHub runners:
427-480 s -> 180-233 s.
make test-ci runs the commands of these jobs one after the other.
On GitHub runners the tests are done after about 2 minutes; the
required check ends with docs-test after about 4-5 minutes.
Additional changes
Add pytest-xdist to the development dependencies
List the 25 slowest tests in each test job (--durations=25)
Test version() directly; it was covered only through the Sphinx test
Add a "Keep Tests Fast" tip, the server marker rule, the extension and
test-ci to the development guide and the testing skill
Refs #1553
Co-authored-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com>
Assisted-by: Claude Code (claude-opus-5-5)
Assisted-by: Claude Code (claude-fable-5-2)
155 lines
5.6 KiB
Python
155 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Split the pytest modules into shards of about equal runtime.
|
|
|
|
CI runs the tests that do not start a server in several jobs ("shards"), see
|
|
``.github/workflows/pytest.yml``. This script prints the test modules of one shard::
|
|
|
|
python scripts/shard_test_files.py 4 0
|
|
|
|
The modules are distributed by their measured runtime, heaviest first, each to the shard
|
|
with the smallest load so far. The runtimes are stored in ``scripts/test_durations.json``
|
|
(seconds per module). A module without an entry counts one second. Every module ends up
|
|
in exactly one shard whatever the runtimes say, so stale runtimes cost balance only.
|
|
|
|
Update the runtimes from the junit reports of the shards of a CI run. Pass the reports of
|
|
all shards: the file is rewritten from them, and a module that is in none of them counts
|
|
one second again::
|
|
|
|
python scripts/shard_test_files.py --update junit-shard-*.xml
|
|
"""
|
|
|
|
import argparse
|
|
import json
|
|
import re
|
|
import sys
|
|
import xml.etree.ElementTree as ET
|
|
from pathlib import Path
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
DURATIONS_FILE = Path(__file__).resolve().with_name("test_durations.json")
|
|
DEFAULT_DURATION = 1.0
|
|
|
|
_JUNIT_MODULE = re.compile(r"^tests\.(test_[a-z0-9]+)")
|
|
|
|
|
|
def list_test_modules(repo_root: Path = REPO_ROOT) -> list[str]:
|
|
"""List the pytest modules directly in ``tests/``.
|
|
|
|
Subdirectories are not searched; there are none with test modules. The ``test-count``
|
|
job of ``pytest.yml`` fails if pytest collects a test that no shard runs.
|
|
|
|
Args:
|
|
repo_root: The root directory of the repository.
|
|
|
|
Returns:
|
|
The module paths relative to the repository root, sorted, with forward slashes.
|
|
"""
|
|
return sorted(
|
|
path.relative_to(repo_root).as_posix() for path in (repo_root / "tests").glob("test_*.py")
|
|
)
|
|
|
|
|
|
def load_durations(path: Path = DURATIONS_FILE) -> dict[str, float]:
|
|
"""Read the stored runtimes.
|
|
|
|
Args:
|
|
path: The JSON file with the seconds per module.
|
|
|
|
Returns:
|
|
The runtimes by module path; empty if the file does not exist.
|
|
"""
|
|
if not path.exists():
|
|
return {}
|
|
with path.open(encoding="utf-8") as fd:
|
|
return {str(name): float(seconds) for name, seconds in json.load(fd).items()}
|
|
|
|
|
|
def split(modules: list[str], durations: dict[str, float], shards: int) -> list[list[str]]:
|
|
"""Distribute the modules to shards of about equal runtime.
|
|
|
|
Args:
|
|
modules: The module paths to distribute.
|
|
durations: The runtime in seconds by module path.
|
|
shards: The number of shards.
|
|
|
|
Returns:
|
|
One list of module paths per shard. Every module is in exactly one of them.
|
|
|
|
Raises:
|
|
ValueError: If the number of shards is not positive.
|
|
"""
|
|
if shards < 1:
|
|
raise ValueError(f"The number of shards must be positive, got {shards}.")
|
|
load = [0.0] * shards
|
|
result: list[list[str]] = [[] for _ in range(shards)]
|
|
# Heaviest first; the module name breaks ties, so the result is deterministic.
|
|
for module in sorted(modules, key=lambda name: (-durations.get(name, DEFAULT_DURATION), name)):
|
|
lightest = min(range(shards), key=lambda index: (load[index], index))
|
|
result[lightest].append(module)
|
|
load[lightest] += durations.get(module, DEFAULT_DURATION)
|
|
return result
|
|
|
|
|
|
def durations_from_junit(reports: list[Path]) -> dict[str, float]:
|
|
"""Sum the test times of junit reports by pytest module.
|
|
|
|
Args:
|
|
reports: The junit XML files written by ``pytest --junitxml``.
|
|
|
|
Returns:
|
|
The runtime in seconds by module path, rounded to two decimals.
|
|
"""
|
|
seconds: dict[str, float] = {}
|
|
for report in reports:
|
|
for case in ET.parse(report).getroot().iter("testcase"):
|
|
match = _JUNIT_MODULE.match(case.get("classname", ""))
|
|
if match:
|
|
module = f"tests/{match.group(1)}.py"
|
|
seconds[module] = seconds.get(module, 0.0) + float(case.get("time") or 0.0)
|
|
return {module: round(value, 2) for module, value in sorted(seconds.items())}
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
"""Print the modules of one shard, or update the stored runtimes.
|
|
|
|
Args:
|
|
argv: The command line arguments; ``sys.argv[1:]`` if omitted.
|
|
|
|
Returns:
|
|
The process exit code.
|
|
"""
|
|
parser = argparse.ArgumentParser(
|
|
description="Split the pytest modules into shards of about equal runtime."
|
|
)
|
|
parser.add_argument("shards", nargs="?", type=int, help="number of shards")
|
|
parser.add_argument("index", nargs="?", type=int, help="shard to print, counted from 0")
|
|
parser.add_argument(
|
|
"--update",
|
|
nargs="+",
|
|
type=Path,
|
|
metavar="JUNIT_XML",
|
|
help="rewrite the stored runtimes from these junit reports",
|
|
)
|
|
args = parser.parse_args(argv)
|
|
|
|
if args.update:
|
|
durations = durations_from_junit(args.update)
|
|
if not durations:
|
|
parser.error("the junit reports contain no test of a tests/test_*.py module")
|
|
with DURATIONS_FILE.open("w", encoding="utf-8", newline="\n") as fd:
|
|
json.dump(durations, fd, indent=1)
|
|
fd.write("\n")
|
|
print(f"Wrote the runtimes of {len(durations)} modules to {DURATIONS_FILE}.")
|
|
return 0
|
|
|
|
if args.shards is None or args.index is None:
|
|
parser.error("give the number of shards and the index of the shard to print")
|
|
if not 0 <= args.index < args.shards:
|
|
parser.error(f"the shard index must be between 0 and {args.shards - 1}")
|
|
print(" ".join(split(list_test_modules(), load_durations(), args.shards)[args.index]))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|