diff --git a/ci/bench_history.py b/ci/bench_history.py index 0f4a93b6..8b588097 100644 --- a/ci/bench_history.py +++ b/ci/bench_history.py @@ -15,6 +15,7 @@ import subprocess import importlib.util from urllib.error import HTTPError from urllib.parse import quote +import yaml from badge_store import BADGE_BRANCH, publish_file, request_json from bench_host import identity as benchmark_host @@ -26,6 +27,12 @@ bench_spec.loader.exec_module(bench_badge) INDEX = "history/index.json" MAX_INDEX_ENTRIES = 50 +BENCH_INPUT_PATHS = ( + "Cargo.toml", "Cargo.lock", ".cargo", "crates", "thirdparty", "vendor", + "rust-toolchain.toml", "ci/bench-badge.py", "ci/bench_host.py", + "ci/ensure-build-deps.sh", "ci/ensure-wx-prefix.sh", + "ci/configure-rust-cache.sh", "ci/wx-cache-key.sh", +) def read_artifact(api: str, repo: str, token: str, filename: str): @@ -51,6 +58,8 @@ def valid_benches(metrics: dict) -> dict[str, float]: def git_distance(ancestor: str, sha: str) -> int | None: + if not re.fullmatch(r"[0-9a-f]{40}", ancestor) or not re.fullmatch(r"[0-9a-f]{40}", sha): + return None result = subprocess.run(("git", "rev-list", "--count", f"{ancestor}..{sha}"), capture_output=True, text=True, check=False) if result.returncode != 0: @@ -60,7 +69,7 @@ def git_distance(ancestor: str, sha: str) -> int | None: return int(result.stdout.strip()) if ancestor_check.returncode == 0 else None -def previous_measurement(index: dict, sha: str, distance=None, host=None) -> dict: +def previous_entry(index: dict, sha: str, distance=None, host=None) -> dict | None: distance = distance or git_distance choices = [] for entry in index.get("entries", []): @@ -73,8 +82,41 @@ def previous_measurement(index: dict, sha: str, distance=None, host=None) -> dic if gap is not None and gap > 0: choices.append((gap, entry)) if not choices: - return {} - return valid_benches(min(choices, key=lambda item: item[0])[1]["metrics"]) + return None + return min(choices, key=lambda item: item[0])[1] + + +def previous_measurement(index: dict, sha: str, distance=None, host=None) -> dict: + entry = previous_entry(index, sha, distance, host) + return valid_benches(entry["metrics"]) if entry else {} + + +def benchmark_build_settings(sha: str, root: Path | str = ".") -> dict: + source = subprocess.check_output( + ("git", "show", f"{sha}:.gitea/workflows/release.yml"), + cwd=root, text=True) + job = yaml.load(source, Loader=yaml.BaseLoader)["jobs"]["release"] + commands = {step.get("name", ""): step.get("run") for step in job["steps"]} + selected = ("Install build dependencies", "Configure bounded Rust compiler cache", + "Build wxWidgets (once, then cached)", "Build release", "Microbenchmarks") + return {"image": job["container"]["image"], "env": job.get("env", {}), + "commands": {name: commands.get(name) for name in selected}} + + +def same_benchmark_inputs(ancestor: str, sha: str, root: Path | str = ".") -> bool: + """Identify changes that only reroute CI without changing benchmark inputs.""" + if not re.fullmatch(r"[0-9a-f]{40}", ancestor) or not re.fullmatch(r"[0-9a-f]{40}", sha): + raise ValueError("invalid benchmark comparison commit") + diff = subprocess.run(("git", "diff", "--quiet", ancestor, sha, "--", + *BENCH_INPUT_PATHS), cwd=root, check=False) + if diff.returncode not in (0, 1): + raise ValueError("cannot inspect benchmark input changes") + if diff.returncode == 1: + return False + try: + return benchmark_build_settings(ancestor, root) == benchmark_build_settings(sha, root) + except (KeyError, TypeError, subprocess.CalledProcessError, yaml.YAMLError): + return False def updated_index(index: dict, sha: str, metrics: dict, timestamp: str) -> dict: @@ -101,6 +143,12 @@ def render_html(index: dict) -> str: host = entry["metrics"].get("bench_host", {}) host_label = host.get("cpu_model", "unknown") if isinstance(host, dict) else "unknown" cells.append(f'
Measured on main CI. Lower ns/op is faster. Results depend on runner load and are " - "not board performance. A new benchmark has no earlier comparison.
" + "Measured on main CI. Lower ns/op is faster. Runner load affects results. " + "Same inputs marks a repeat of the benchmark source and build settings. " + "A new benchmark has no earlier comparison.
" f"Most recent {len(entries)} measured commits. Full per-commit JSON is retained " "in the private ci-badges branch.
" f"| Commit | " - f"UTC | CPU | {headings}UTC | CPU | " + f"Inputs | {headings}{''.join(rows)}" "
|---|