Files
Pulse/scripts/release_control/run_go_package_slice.py
rcourtman 5e4d88f4e5 Balance backend tests across the seven full PR workers
Rest workers held the verdict on 12 of 20 PRs. Give full PRs to main a
third rest partition and run the API suite in four partitions. Every
runtime test still runs from the checked-out source. The required API
verdict also covers the third rest partition.
2026-11-19 18:53:01 +00:00

133 lines
6.3 KiB
Python

#!/usr/bin/env python3
"""Run one exhaustive, order-preserving slice of a slow backend test package.
The two fixed packages have outgrown whole-package CI balancing. Source supplies
the test inventory; measured weights only move the contiguous boundary. Reuse
the API selector without changing source order, worker counts or test deadlines.
The existing JSON recorder checks every selected root's completion, including
explicit skips, and preserves failures and unfinished output.
"""
from __future__ import annotations
import argparse
import json
import os
from pathlib import Path
import re
import subprocess
import sys
ROOT = Path(__file__).resolve().parents[2]
MODULE = "github.com/rcourtman/pulse-go-rewrite/"
PACKAGES = ("internal/monitoring", "scripts/installtests")
RECORDER = ROOT / ".github/scripts/record-internal-api-test-seconds.py"
SELECTOR = ROOT / ".github/scripts/select-internal-api-shard.sh"
WEIGHTS = {
package: ROOT / f"scripts/release_control/{package.rsplit('/', 1)[-1]}-test-seconds.txt"
for package in PACKAGES
}
def source_tests(output: str) -> list[str]:
names = [line for line in output.splitlines()
if line.startswith(("Test", "Fuzz", "Example")) and line.isidentifier()]
if not names or len(set(names)) != len(names):
raise ValueError("source listing is empty or repeats test names")
return names
def select(names: list[str], index: int, weights: Path | None = None, count: int = 2) -> list[str]:
if count not in (2, 3) or not 0 <= index < count or len(names) < count:
raise ValueError("invalid source size, slice count or index")
if weights is None:
return names[len(names) * index // count:len(names) * (index + 1) // count]
# The same selector already keeps API tests in contiguous source order.
# Ignore ambient overrides: both workers must use the committed weights
# and the same default for new, renamed or unmeasured source tests.
env = {key: value for key, value in os.environ.items() if key != "DEFAULT_WEIGHT"}
pieces = []
for part in range(count):
result = subprocess.run(["bash", str(SELECTOR), str(weights), str(count), str(part)],
input="\n".join(names) + "\n", capture_output=True,
text=True, env=env, check=True)
pieces.append(result.stdout.splitlines())
if not all(pieces) or [name for piece in pieces for name in piece] != names:
raise ValueError("weighted slices do not cover source order exactly once")
return pieces[index]
def test_filter(names: list[str], selected: list[str]) -> list[str]:
chosen = set(selected)
others = [name for name in names if name not in chosen]
pattern = lambda items: "^(" + "|".join(re.escape(name) for name in items) + ")$"
run, skip = pattern(selected), pattern(others)
flag, value = ("-skip", skip) if len(skip) < len(run) else ("-run", run)
if len(value.encode()) > 120000:
raise ValueError("slice filter exceeds the 120000-byte argument bound")
# Never combine -run with -skip: Go recompiles the latter per test then.
return [flag, value]
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--package", choices=PACKAGES, required=True)
parser.add_argument("--index", type=int, required=True)
parser.add_argument("--count", type=int, choices=(2, 3), default=2)
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
args.output.mkdir(parents=True, exist_ok=True)
package = "./" + args.package
# Like ordinary packages, slices own runtime admission rather than inherit
# a caller's selection, wrapper, short/fail-fast mode or compile-only run.
# Append overrides instead of parsing GOFLAGS: retain quoted compiler and
# module flags exactly. The command's one source-derived filter follows
# these empty selectors; listing and execution must see the same universe.
environment = dict(os.environ)
environment["GOFLAGS"] = environment.get("GOFLAGS", "") + (
" -run= -skip= -list= -exec= -fuzz= -short=false -failfast=false"
" -c=false -n=false -bench= -shuffle=off")
listing = subprocess.run(["go", "test", "-race", "-json=false", "-list", ".", package],
cwd=ROOT, env=environment, capture_output=True, text=True)
if listing.returncode:
sys.stdout.write(listing.stdout)
sys.stderr.write(listing.stderr)
return listing.returncode
try:
names = source_tests(listing.stdout)
weights = WEIGHTS[args.package]
selected = select(names, args.index, weights, args.count)
filters = test_filter(names, selected)
except (OSError, ValueError, subprocess.CalledProcessError) as error:
print(f"Backend slice selection failed: {error}", file=sys.stderr)
if isinstance(error, subprocess.CalledProcessError):
sys.stdout.write(error.stdout or "")
sys.stderr.write(error.stderr or "")
return 2
(args.output / "listed.tests").write_text("\n".join(names) + "\n")
selection = args.output / "selected.tests"
selection.write_text("\n".join(selected) + "\n")
command = ["go", "test", "-race", "-count=1", "-timeout", "50m", "-json",
*filters, package]
print(f"{args.package} slice {args.index}/{args.count}: {len(selected)} of {len(names)} roots "
f"({selected[0]} .. {selected[-1]})", flush=True)
with subprocess.Popen(command, cwd=ROOT, env=environment, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT) as process:
recorded = subprocess.run(
[sys.executable, str(RECORDER), str(args.output / "timings.txt"),
str(selection), MODULE + args.package], stdin=process.stdout, cwd=ROOT)
process.stdout.close()
test_exit = process.wait()
receipt = {"package": MODULE + args.package, "index": args.index, "count": args.count,
"listed_roots": len(names), "selected_roots": len(selected),
"weights": str(weights.relative_to(ROOT)),
"test_exit": test_exit, "recorder_exit": recorded.returncode}
(args.output / "receipt.json").write_text(json.dumps(receipt, indent=2) + "\n")
print(json.dumps(receipt), flush=True)
return 0 if test_exit == recorded.returncode == 0 else 1
if __name__ == "__main__":
raise SystemExit(main())