|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Validate benchmark outputs against regression thresholds. |
| 3 | +
|
| 4 | +This script is intended to run in CI after ``benchmarks/bench_kll.py``. It reads |
| 5 | +CSV outputs from ``bench_out`` (or a supplied directory) and enforces |
| 6 | +conservative performance and accuracy targets so regressions surface early. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +import json |
| 13 | +from pathlib import Path |
| 14 | +from typing import Dict, List, Tuple |
| 15 | + |
| 16 | +import pandas as pd |
| 17 | + |
| 18 | + |
| 19 | +ACCURACY_ABS_ERROR_MAX = 0.5 |
| 20 | +THROUGHPUT_MIN_UPS = 15_000 |
| 21 | +LATENCY_P95_MAX_US = 1_000.0 |
| 22 | +MERGE_TIME_MAX_S = 2.0 |
| 23 | + |
| 24 | + |
| 25 | +def _load_csv(path: Path) -> pd.DataFrame: |
| 26 | + if not path.exists(): |
| 27 | + raise FileNotFoundError(f"Expected benchmark artifact missing: {path}") |
| 28 | + return pd.read_csv(path) |
| 29 | + |
| 30 | + |
| 31 | +def _check_accuracy(df: pd.DataFrame) -> Tuple[bool, Dict[str, float]]: |
| 32 | + worst = df.groupby(["mode"])["abs_error"].max().to_dict() |
| 33 | + overall = float(df["abs_error"].max()) if not df.empty else 0.0 |
| 34 | + ok = overall <= ACCURACY_ABS_ERROR_MAX |
| 35 | + worst.setdefault("overall", overall) |
| 36 | + return ok, worst |
| 37 | + |
| 38 | + |
| 39 | +def _check_throughput(df: pd.DataFrame) -> Tuple[bool, float]: |
| 40 | + minimum = float(df["updates_per_sec"].min()) if not df.empty else float("inf") |
| 41 | + return minimum >= THROUGHPUT_MIN_UPS, minimum |
| 42 | + |
| 43 | + |
| 44 | +def _check_latency(df: pd.DataFrame) -> Tuple[bool, float]: |
| 45 | + if df.empty: |
| 46 | + return True, 0.0 |
| 47 | + p95 = float(df["latency_us"].quantile(0.95)) |
| 48 | + return p95 <= LATENCY_P95_MAX_US, p95 |
| 49 | + |
| 50 | + |
| 51 | +def _check_merge(df: pd.DataFrame) -> Tuple[bool, float]: |
| 52 | + if df.empty: |
| 53 | + return True, 0.0 |
| 54 | + maximum = float(df["merge_time_s"].max()) |
| 55 | + return maximum <= MERGE_TIME_MAX_S, maximum |
| 56 | + |
| 57 | + |
| 58 | +def _summarise(results: Dict[str, Dict[str, object]]) -> str: |
| 59 | + lines: List[str] = ["# Benchmark validation summary", ""] |
| 60 | + lines.append("| Check | Threshold | Observed | Status |") |
| 61 | + lines.append("| --- | --- | --- | --- |") |
| 62 | + for name, payload in results.items(): |
| 63 | + threshold = payload["threshold"] |
| 64 | + observed = payload["observed"] |
| 65 | + status = "PASS" if payload["ok"] else "FAIL" |
| 66 | + lines.append(f"| {name} | {threshold} | {observed} | {status} |") |
| 67 | + lines.append("") |
| 68 | + lines.append("```json") |
| 69 | + lines.append(json.dumps(results, indent=2, sort_keys=True)) |
| 70 | + lines.append("```") |
| 71 | + return "\n".join(lines) |
| 72 | + |
| 73 | + |
| 74 | +def main() -> None: |
| 75 | + parser = argparse.ArgumentParser(description=__doc__) |
| 76 | + parser.add_argument("outdir", nargs="?", default="bench_out", help="Directory containing benchmark CSVs") |
| 77 | + parser.add_argument("--summary", default="bench_summary.md", help="Filename for the generated markdown summary") |
| 78 | + args = parser.parse_args() |
| 79 | + |
| 80 | + outdir = Path(args.outdir) |
| 81 | + accuracy = _load_csv(outdir / "accuracy.csv") |
| 82 | + throughput = _load_csv(outdir / "update_throughput.csv") |
| 83 | + latency = _load_csv(outdir / "query_latency.csv") |
| 84 | + merge = _load_csv(outdir / "merge.csv") |
| 85 | + |
| 86 | + summary: Dict[str, Dict[str, object]] = {} |
| 87 | + |
| 88 | + accuracy_ok, accuracy_obs = _check_accuracy(accuracy) |
| 89 | + summary["Accuracy abs error"] = { |
| 90 | + "threshold": f"<= {ACCURACY_ABS_ERROR_MAX}", |
| 91 | + "observed": {mode: round(value, 6) for mode, value in accuracy_obs.items()}, |
| 92 | + "ok": accuracy_ok, |
| 93 | + } |
| 94 | + |
| 95 | + throughput_ok, throughput_obs = _check_throughput(throughput) |
| 96 | + summary["Update throughput"] = { |
| 97 | + "threshold": f">= {THROUGHPUT_MIN_UPS} updates/sec", |
| 98 | + "observed": round(throughput_obs, 2), |
| 99 | + "ok": throughput_ok, |
| 100 | + } |
| 101 | + |
| 102 | + latency_ok, latency_obs = _check_latency(latency) |
| 103 | + summary["Query latency p95"] = { |
| 104 | + "threshold": f"<= {LATENCY_P95_MAX_US} µs", |
| 105 | + "observed": round(latency_obs, 2), |
| 106 | + "ok": latency_ok, |
| 107 | + } |
| 108 | + |
| 109 | + merge_ok, merge_obs = _check_merge(merge) |
| 110 | + summary["Merge time"] = { |
| 111 | + "threshold": f"<= {MERGE_TIME_MAX_S} s", |
| 112 | + "observed": round(merge_obs, 3), |
| 113 | + "ok": merge_ok, |
| 114 | + } |
| 115 | + |
| 116 | + summary_path = outdir / args.summary |
| 117 | + summary_path.write_text(_summarise(summary), encoding="utf-8") |
| 118 | + |
| 119 | + print(summary_path.read_text(encoding="utf-8")) |
| 120 | + |
| 121 | + if not all(item["ok"] for item in summary.values()): |
| 122 | + raise SystemExit("Benchmark regression detected; see summary above.") |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + main() |
0 commit comments