|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import json |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +def parse_args() -> argparse.Namespace: |
| 11 | + parser = argparse.ArgumentParser(description="Compare codedb benchmark JSON results.") |
| 12 | + parser.add_argument("base", help="baseline benchmark JSON") |
| 13 | + parser.add_argument("head", help="candidate benchmark JSON") |
| 14 | + parser.add_argument("--threshold-pct", type=float, default=10.0, help="maximum allowed latency regression percentage") |
| 15 | + parser.add_argument("--markdown-out", help="write markdown report to this path") |
| 16 | + return parser.parse_args() |
| 17 | + |
| 18 | + |
| 19 | +def load_tools(path: str) -> dict[str, dict]: |
| 20 | + data = json.loads(Path(path).read_text(encoding="utf-8")) |
| 21 | + return {tool["tool"]: tool for tool in data["tools"]} |
| 22 | + |
| 23 | + |
| 24 | +def pct_change(base_ns: int, head_ns: int) -> float: |
| 25 | + if base_ns == 0: |
| 26 | + return 0.0 |
| 27 | + return ((head_ns - base_ns) / base_ns) * 100.0 |
| 28 | + |
| 29 | + |
| 30 | +def render_markdown(rows: list[tuple[str, int, int, float]], threshold_pct: float) -> str: |
| 31 | + lines = [ |
| 32 | + "## Benchmark Regression Report", |
| 33 | + "", |
| 34 | + f"Threshold: {threshold_pct:.2f}%", |
| 35 | + "", |
| 36 | + "| Tool | Base (ns) | Head (ns) | Delta | Status |", |
| 37 | + "| --- | ---: | ---: | ---: | --- |", |
| 38 | + ] |
| 39 | + for tool, base_ns, head_ns, delta in rows: |
| 40 | + status = "FAIL" if delta > threshold_pct else "OK" |
| 41 | + lines.append(f"| `{tool}` | {base_ns} | {head_ns} | {delta:+.2f}% | {status} |") |
| 42 | + return "\n".join(lines) + "\n" |
| 43 | + |
| 44 | + |
| 45 | +def main() -> int: |
| 46 | + args = parse_args() |
| 47 | + base = load_tools(args.base) |
| 48 | + head = load_tools(args.head) |
| 49 | + |
| 50 | + missing = sorted(set(base) ^ set(head)) |
| 51 | + if missing: |
| 52 | + print(f"error: tool mismatch: {', '.join(missing)}", file=sys.stderr) |
| 53 | + return 1 |
| 54 | + |
| 55 | + rows: list[tuple[str, int, int, float]] = [] |
| 56 | + failures: list[str] = [] |
| 57 | + |
| 58 | + for tool in sorted(base): |
| 59 | + base_ns = int(base[tool]["avg_latency_ns"]) |
| 60 | + head_ns = int(head[tool]["avg_latency_ns"]) |
| 61 | + delta = pct_change(base_ns, head_ns) |
| 62 | + rows.append((tool, base_ns, head_ns, delta)) |
| 63 | + if delta > args.threshold_pct: |
| 64 | + failures.append(f"{tool} regressed by {delta:.2f}%") |
| 65 | + |
| 66 | + report = render_markdown(rows, args.threshold_pct) |
| 67 | + sys.stdout.write(report) |
| 68 | + |
| 69 | + if args.markdown_out: |
| 70 | + Path(args.markdown_out).write_text(report, encoding="utf-8") |
| 71 | + |
| 72 | + if failures: |
| 73 | + for failure in failures: |
| 74 | + print(failure, file=sys.stderr) |
| 75 | + return 1 |
| 76 | + return 0 |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + raise SystemExit(main()) |
0 commit comments