mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
ci: weight only real gloo marker usage when sharding
Detect the gloo marker by AST so tests that merely mention it in strings (policy fixtures) do not take a Gloo-sized weight and unbalance the shards. The duration-inventory policy test shares the same detector. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8681ef718b
commit
7f8a2d7fd0
3 files changed
+45
-10
No files matched your search
@@ -2,7 +2,8 @@
|
||||
"""Split selected files into two deterministic, duration-balanced Gloo shards.
|
||||
|
||||
Every selected file is assigned once, even without a duration or Gloo marker.
|
||||
Unlisted files containing ``pytest.mark.gloo`` use the table's default_seconds;
|
||||
Unlisted files whose code (not strings or comments) references
|
||||
``pytest.mark.gloo`` use the table's default_seconds;
|
||||
other unlisted files have zero weight. Marker detection affects balance only.
|
||||
Paths must be canonical relative files inside tests/ (symlink escapes rejected).
|
||||
Duplicate input paths are deduplicated. Empty input produces two empty files.
|
||||
@@ -11,6 +12,7 @@ Duplicate input paths are deduplicated. Empty input produces two empty files.
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import ast
|
||||
import json
|
||||
import math
|
||||
from pathlib import Path
|
||||
@@ -33,6 +35,19 @@ def load_durations(path: Path) -> tuple[float, dict[str, float]]:
|
||||
return default, files
|
||||
|
||||
|
||||
def has_gloo_marker(source: str) -> bool:
|
||||
"""Return whether Python source uses the ``pytest.mark.gloo`` marker expression."""
|
||||
|
||||
try:
|
||||
tree = ast.parse(source)
|
||||
except SyntaxError:
|
||||
return False
|
||||
return any(
|
||||
isinstance(node, ast.Attribute) and ast.unparse(node) == "pytest.mark.gloo"
|
||||
for node in ast.walk(tree)
|
||||
)
|
||||
|
||||
|
||||
def shard_tests(
|
||||
selected: list[str], default_seconds: float, durations: dict[str, float],
|
||||
*, root: Path,
|
||||
@@ -46,9 +61,9 @@ def shard_tests(
|
||||
raise ValueError(f"selected path must be a file inside tests/: {name!r}")
|
||||
weight = durations.get(name)
|
||||
if weight is None:
|
||||
weight = default_seconds if "pytest.mark.gloo" in (
|
||||
root / path
|
||||
).read_text(encoding="utf-8") else 0
|
||||
weight = default_seconds if has_gloo_marker(
|
||||
(root / path).read_text(encoding="utf-8")
|
||||
) else 0
|
||||
weighted.append((name, weight))
|
||||
shards: tuple[list[str], list[str]] = ([], [])
|
||||
loads = [0.0, 0.0]
|
||||
|
||||
@@ -240,16 +240,13 @@ def test_gloo_marker_is_registered_and_marks_real_cpu_worker_files():
|
||||
|
||||
|
||||
def test_gloo_durations_cover_exactly_the_marked_test_files():
|
||||
# AST detects real marker expressions, excluding quoted fixtures/comments.
|
||||
import ast
|
||||
# The sharder's AST detection excludes quoted fixtures and comments.
|
||||
from scripts.shard_pr_tests import has_gloo_marker
|
||||
|
||||
marked = {
|
||||
path.relative_to(ROOT).as_posix()
|
||||
for path in (ROOT / "tests").rglob("test_*.py")
|
||||
if any(
|
||||
isinstance(node, ast.Attribute) and ast.unparse(node) == "pytest.mark.gloo"
|
||||
for node in ast.walk(ast.parse(path.read_text(encoding="utf-8")))
|
||||
)
|
||||
if has_gloo_marker(path.read_text(encoding="utf-8"))
|
||||
}
|
||||
table = json.loads((ROOT / "ci/gloo-test-durations.json").read_text())
|
||||
assert set(table["files"]) == marked
|
||||
|
||||
@@ -51,6 +51,29 @@ def test_unlisted_marked_files_get_default_but_all_files_are_assigned_once(tmp_p
|
||||
assert sorted(shards[0] + shards[1]) == sorted(names)
|
||||
|
||||
|
||||
def test_marker_mentioned_only_in_strings_or_comments_has_no_weight(tmp_path):
|
||||
names = ["tests/test_policy.py", "tests/test_worker.py"]
|
||||
_files(tmp_path, names, marked=[names[1]])
|
||||
(tmp_path / names[0]).write_text(
|
||||
'# pytest.mark.gloo\nFIXTURE = "pytestmark = pytest.mark.gloo"\n', encoding="utf-8"
|
||||
)
|
||||
assert not shard_pr_tests.has_gloo_marker((tmp_path / names[0]).read_text())
|
||||
assert shard_pr_tests.has_gloo_marker((tmp_path / names[1]).read_text())
|
||||
assert shard_pr_tests.shard_tests(names, 80, {}, root=tmp_path) == (
|
||||
[names[1]], [names[0]],
|
||||
)
|
||||
|
||||
|
||||
def test_real_selection_keeps_policy_tests_out_of_gloo_balance():
|
||||
default, durations = shard_pr_tests.load_durations(ROOT / "ci/gloo-test-durations.json")
|
||||
names = [*durations, "tests/test_ci_policy.py", "tests/test_shard_pr_tests.py"]
|
||||
shards = shard_pr_tests.shard_tests(names, default, durations, root=ROOT)
|
||||
assert shards[0][0] == "tests/test_distributed_gloo.py"
|
||||
assert shards[1][:2] == [
|
||||
"tests/test_distributed_model_gloo.py", "tests/test_distributed_probe_gloo.py",
|
||||
]
|
||||
|
||||
|
||||
def test_equal_weights_use_path_order_and_equal_loads_choose_zero(tmp_path):
|
||||
names = [f"tests/test_{letter}.py" for letter in "dcba"]
|
||||
_files(tmp_path, names)
|
||||
|
||||
Reference in new issue
Block a user