bitchat/scripts/tests/test_check_perf_floors.py
Jozef Koval 217b522ec3 Fail the floor gate on a benchmark with no floor
The gate fails when a floored benchmark stops reporting (exit 3, no
retry) so that a benchmark cannot be dropped without an explicit
floors-file change. The other direction was advisory: a benchmark that
reports a PERF line with no floor printed "consider adding a floor"
into the job log and passed. Nothing else surfaces that line, so a new
hot-path benchmark could measure on every run and guard nothing, and
the file the gate reads would still look complete.

That is the shape of the localization gap closed in #1654, where a key
missing from every catalog compiled and shipped English while CI stayed
green, and the move is the one made in #1646 and #1648: an advisory
check becomes blocking once its backlog is zero. perf-floors.json
carries 12 floors and PerformanceBaselineTests has 12 benchmarks, so
flipping this costs nothing today and holds the two in step from now
on.

An unfloored benchmark now fails alongside a missing one, with the same
exit code and for the same reason: the floors file and the benchmark
suite must change together, and no re-measurement can repair a drift
between them. The message names the file to edit.

The test pinned in the previous commit flips from "reported but does
not fail" to "fails without a retry"; the philosophy note in the floors
file says "added, renamed or removed" instead of "renamed or removed".
2026-09-24 22:34:02 +02:00

173 lines
7.0 KiB
Python

import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
REPOSITORY_ROOT = Path(__file__).resolve().parents[2]
GATE = REPOSITORY_ROOT / "scripts/check-perf-floors.sh"
class CheckPerfFloorsTests(unittest.TestCase):
"""The floor gate is what stands between an order-of-magnitude regression
and main, and it decides by parsing captured test output. These run the
real script against fixture logs and pin its exit-code contract: which
outcomes skip, which are re-measured, and which fail without a retry.
"""
def setUp(self) -> None:
self.tmp = Path(tempfile.mkdtemp(prefix="perf-floors-"))
self.addCleanup(shutil.rmtree, self.tmp, ignore_errors=True)
self.invocations = self.tmp / "remeasure-invocations"
def floors(self, **floors: int) -> Path:
path = self.tmp / "floors.json"
entries = ",\n".join(f' "{name}": {floor}' for name, floor in floors.items())
path.write_text('{\n "floors": {\n' + entries + "\n }\n}\n", encoding="utf-8")
return path
def log(self, *lines: str) -> Path:
path = self.tmp / "perf-output.log"
path.write_text("".join(line + "\n" for line in lines), encoding="utf-8")
return path
def remeasure(self, append: str | None = None) -> Path:
"""A stand-in for `swift test`: records each invocation and appends one
line to the log the gate points it at, the way a real re-run would."""
script = self.tmp / "remeasure.sh"
body = ["#!/bin/sh", f'echo run >> "{self.invocations}"']
if append is not None:
body.append(f"printf '%s\\n' '{append}' >> \"$BITCHAT_PERF_LOG\"")
script.write_text("\n".join(body) + "\n", encoding="utf-8")
script.chmod(0o755)
return script
def run_gate(
self,
log: Path,
floors: Path,
*,
remeasure: Path | None = None,
attempts: int | None = None,
skip: bool = False,
) -> subprocess.CompletedProcess:
env = {k: v for k, v in os.environ.items() if not k.startswith("BITCHAT_")}
# Never fall through to the default `swift test` re-measure: a test
# that unexpectedly retries must fail fast, not build the app.
env["BITCHAT_PERF_REMEASURE_CMD"] = str(remeasure or self.remeasure())
if attempts is not None:
env["BITCHAT_PERF_GATE_ATTEMPTS"] = str(attempts)
if skip:
env["BITCHAT_SKIP_PERF_BASELINES"] = "1"
return subprocess.run(
[str(GATE), str(log), str(floors)],
cwd=self.tmp,
env=env,
capture_output=True,
text=True,
check=False,
)
def remeasure_count(self) -> int:
if not self.invocations.exists():
return 0
return len(self.invocations.read_text(encoding="utf-8").splitlines())
def test_every_floor_met_passes(self) -> None:
result = self.run_gate(
self.log("PERF[a.one]: 1000 ops/sec (avg 1 ms)", "PERF[b.two]: 250.5 items/sec"),
self.floors(**{"a.one": 100, "b.two": 200}),
)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("all benchmarks at or above their floors", result.stdout)
self.assertEqual(self.remeasure_count(), 0)
def test_best_value_across_appended_attempts_is_what_counts(self) -> None:
# Re-measurements append to the same log, so one line clearing the
# floor is enough even when an earlier attempt sat under it.
result = self.run_gate(
self.log("PERF[a.one]: 50 ops/sec", "PERF[a.one]: 500 ops/sec"),
self.floors(**{"a.one": 100}),
)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertEqual(self.remeasure_count(), 0)
def test_below_floor_is_remeasured_and_passes_once_it_clears(self) -> None:
result = self.run_gate(
self.log("PERF[a.one]: 50 ops/sec"),
self.floors(**{"a.one": 100}),
remeasure=self.remeasure(append="PERF[a.one]: 500 ops/sec"),
)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertEqual(self.remeasure_count(), 1)
self.assertIn("re-measuring (attempt 2", result.stdout)
def test_below_floor_on_every_attempt_fails(self) -> None:
result = self.run_gate(
self.log("PERF[a.one]: 50 ops/sec"),
self.floors(**{"a.one": 100}),
remeasure=self.remeasure(append="PERF[a.one]: 60 ops/sec"),
attempts=2,
)
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertEqual(self.remeasure_count(), 1)
self.assertIn("BELOW a.one: 60 ops/sec is under floor 100", result.stdout)
self.assertIn("treating as a real regression", result.stderr)
def test_missing_floored_benchmark_fails_without_a_retry(self) -> None:
# A re-measurement that happened to produce the missing line would
# paper over a silently dropped benchmark, so this must not retry.
result = self.run_gate(
self.log("PERF[a.one]: 1000 ops/sec"),
self.floors(**{"a.one": 100, "b.two": 200}),
remeasure=self.remeasure(append="PERF[b.two]: 900 ops/sec"),
)
self.assertEqual(result.returncode, 3, result.stdout + result.stderr)
self.assertEqual(self.remeasure_count(), 0)
self.assertIn("MISSING b.two: floored benchmark reported no PERF line", result.stdout)
def test_unfloored_benchmark_fails_without_a_retry(self) -> None:
# The mirror of the missing case: a benchmark that measures but has no
# floor guards nothing, and no re-measurement can supply the floor.
result = self.run_gate(
self.log("PERF[a.one]: 1000 ops/sec", "PERF[c.new]: 1000 ops/sec"),
self.floors(**{"a.one": 100}),
)
self.assertEqual(result.returncode, 3, result.stdout + result.stderr)
self.assertIn("NO-FLOOR c.new: 1000 ops/sec reports a PERF line but has no floor", result.stdout)
self.assertEqual(self.remeasure_count(), 0)
def test_output_without_perf_lines_skips(self) -> None:
# Package-only matrix entries produce no benchmarks; the gate must
# not fail them for it.
result = self.run_gate(
self.log("Test Suite 'All tests' passed", "Executed 12 tests"),
self.floors(**{"a.one": 100}),
)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("no PERF lines", result.stdout)
def test_skip_flag_bypasses_the_gate_entirely(self) -> None:
result = self.run_gate(
self.log("PERF[a.one]: 1 ops/sec"),
self.floors(**{"a.one": 100}),
skip=True,
)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("BITCHAT_SKIP_PERF_BASELINES=1", result.stdout)
self.assertEqual(self.remeasure_count(), 0)
if __name__ == "__main__":
unittest.main()