|
| 1 | +"""Compare two pytest-benchmark JSON result files and output a Markdown report. |
| 2 | +
|
| 3 | +Usage: |
| 4 | + python scripts/benchmark_compare.py <baseline.json> <current.json> |
| 5 | +
|
| 6 | +The report is printed to stdout and is suitable for posting as a GitHub PR |
| 7 | +comment. The script always exits 0 — it is informational only. |
| 8 | +""" |
| 9 | + |
| 10 | +import json |
| 11 | +import pathlib |
| 12 | +import sys |
| 13 | + |
| 14 | +REGRESSION_THRESHOLD_PCT = 20 # warn marker above this percentage |
| 15 | + |
| 16 | + |
| 17 | +def load_benchmarks(path: str) -> dict[str, dict]: |
| 18 | + """Return a dict mapping test name -> stats from a pytest-benchmark JSON.""" |
| 19 | + with pathlib.Path(path).open() as file: |
| 20 | + data = json.load(file) |
| 21 | + return {bench["name"]: bench["stats"] for bench in data["benchmarks"]} |
| 22 | + |
| 23 | + |
| 24 | +def short_name(full_name: str) -> str: |
| 25 | + """Extract the parametrize ID from a full test name.""" |
| 26 | + start = full_name.find("[") |
| 27 | + end = full_name.rfind("]") |
| 28 | + if start != -1 and end != -1: |
| 29 | + return full_name[start + 1 : end] |
| 30 | + return full_name |
| 31 | + |
| 32 | + |
| 33 | +def format_table( |
| 34 | + rows: list[tuple[str, float, float, float]], |
| 35 | +) -> list[str]: |
| 36 | + """Format comparison rows into a Markdown table. |
| 37 | +
|
| 38 | + Each row is (name, baseline_ms, current_ms, change_pct). |
| 39 | + """ |
| 40 | + lines = [ |
| 41 | + "| Test | Baseline (ms) | Current (ms) | Change |", |
| 42 | + "|---|--:|--:|--:|", |
| 43 | + ] |
| 44 | + for name, baseline_ms, current_ms, change_pct in rows: |
| 45 | + warn = " :warning:" if change_pct > REGRESSION_THRESHOLD_PCT else "" |
| 46 | + lines.append( |
| 47 | + f"| {name} | {baseline_ms:.2f} | {current_ms:.2f} " |
| 48 | + f"| {change_pct:+.1f}%{warn} |" |
| 49 | + ) |
| 50 | + return lines |
| 51 | + |
| 52 | + |
| 53 | +def compare( |
| 54 | + baseline: dict[str, dict], |
| 55 | + current: dict[str, dict], |
| 56 | + name_filter: str, |
| 57 | +) -> tuple[list[tuple[str, float, float, float]], float]: |
| 58 | + """Compare benchmarks whose names contain *name_filter*. |
| 59 | +
|
| 60 | + Returns (rows, average_change_pct). |
| 61 | + """ |
| 62 | + rows: list[tuple[str, float, float, float]] = [] |
| 63 | + for name in sorted(baseline): |
| 64 | + if name_filter not in name: |
| 65 | + continue |
| 66 | + if name not in current: |
| 67 | + continue |
| 68 | + baseline_ms = baseline[name]["mean"] * 1000 |
| 69 | + current_ms = current[name]["mean"] * 1000 |
| 70 | + change_pct = ((current_ms - baseline_ms) / baseline_ms) * 100 |
| 71 | + rows.append((short_name(name), baseline_ms, current_ms, change_pct)) |
| 72 | + |
| 73 | + average = sum(r[3] for r in rows) / len(rows) if rows else 0.0 |
| 74 | + return rows, average |
| 75 | + |
| 76 | + |
| 77 | +def main() -> None: |
| 78 | + if len(sys.argv) != 3: |
| 79 | + print(f"Usage: {sys.argv[0]} <baseline.json> <current.json>", file=sys.stderr) |
| 80 | + sys.exit(1) |
| 81 | + |
| 82 | + baseline = load_benchmarks(sys.argv[1]) |
| 83 | + current = load_benchmarks(sys.argv[2]) |
| 84 | + |
| 85 | + output: list[str] = ["## Benchmark Comparison", ""] |
| 86 | + |
| 87 | + # --- Detection --- |
| 88 | + detect_rows, detect_avg = compare(baseline, current, "test_bench_detect[") |
| 89 | + # Exclude harmonize tests that also contain "detect" |
| 90 | + detect_rows = [r for r in detect_rows if "harmonize" not in r[0]] |
| 91 | + if detect_rows: |
| 92 | + # Recalculate average after filtering |
| 93 | + detect_avg = ( |
| 94 | + sum(r[3] for r in detect_rows) / len(detect_rows) if detect_rows else 0.0 |
| 95 | + ) |
| 96 | + output.append("### Detection (`detect_file`)") |
| 97 | + output.append("") |
| 98 | + output.extend(format_table(detect_rows)) |
| 99 | + output.append("") |
| 100 | + output.append(f"**Average: {detect_avg:+.1f}%**") |
| 101 | + output.append("") |
| 102 | + |
| 103 | + # --- Full pipeline --- |
| 104 | + harmonize_rows, harmonize_avg = compare( |
| 105 | + baseline, current, "test_bench_detect_and_harmonize[" |
| 106 | + ) |
| 107 | + if harmonize_rows: |
| 108 | + output.append("### Full Pipeline (`detect_file` + `as_table`)") |
| 109 | + output.append("") |
| 110 | + output.extend(format_table(harmonize_rows)) |
| 111 | + output.append("") |
| 112 | + output.append(f"**Average: {harmonize_avg:+.1f}%**") |
| 113 | + output.append("") |
| 114 | + |
| 115 | + # --- Summary --- |
| 116 | + if not detect_rows and not harmonize_rows: |
| 117 | + output.append( |
| 118 | + "No matching benchmarks found in both baseline and current results." |
| 119 | + ) |
| 120 | + else: |
| 121 | + regressions = [ |
| 122 | + r for r in (detect_rows + harmonize_rows) if r[3] > REGRESSION_THRESHOLD_PCT |
| 123 | + ] |
| 124 | + if regressions: |
| 125 | + output.append( |
| 126 | + f":warning: **{len(regressions)} test(s) regressed " |
| 127 | + f"by more than {REGRESSION_THRESHOLD_PCT}%**" |
| 128 | + ) |
| 129 | + else: |
| 130 | + output.append("No significant regressions detected.") |
| 131 | + |
| 132 | + print("\n".join(output)) |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + main() |
0 commit comments