|
| 1 | +"""Tests for scripts/check_benchmark_regression.py.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from scripts.check_benchmark_regression import ( |
| 10 | + BenchmarkDataError, |
| 11 | + check_regression, |
| 12 | + load_baseline_means, |
| 13 | + load_results, |
| 14 | + normalize_benchmark_name, |
| 15 | +) |
| 16 | + |
| 17 | +GATED_BENCH = "test_summary_cache_hit" |
| 18 | + |
| 19 | + |
| 20 | +def _write_results(path, benchmarks: list[dict]) -> None: |
| 21 | + path.write_text( |
| 22 | + json.dumps({"benchmarks": benchmarks}, indent=2), |
| 23 | + encoding="utf-8", |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +def _write_baselines(path, groups: dict[str, dict[str, float]]) -> None: |
| 28 | + path.write_text( |
| 29 | + json.dumps({"groups": groups}, indent=2), |
| 30 | + encoding="utf-8", |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def test_normalize_benchmark_name_strips_module_prefix() -> None: |
| 35 | + full = "tests/benchmarks/test_summary_cache_bench.py::test_summary_cache_hit" |
| 36 | + assert normalize_benchmark_name(full) == "test_summary_cache_hit" |
| 37 | + assert normalize_benchmark_name("test_summary_cache_hit") == "test_summary_cache_hit" |
| 38 | + |
| 39 | + |
| 40 | +def test_load_results_normalizes_full_node_id(tmp_path) -> None: |
| 41 | + path = tmp_path / "results.json" |
| 42 | + _write_results( |
| 43 | + path, |
| 44 | + [ |
| 45 | + { |
| 46 | + "name": "tests/benchmarks/test_summary_cache_bench.py::test_summary_cache_hit", |
| 47 | + "stats": {"mean": 0.0001}, |
| 48 | + } |
| 49 | + ], |
| 50 | + ) |
| 51 | + |
| 52 | + assert load_results(path)["test_summary_cache_hit"] == pytest.approx(0.0001) |
| 53 | + |
| 54 | + |
| 55 | +def test_missing_baseline_warns_without_failing( |
| 56 | + tmp_path, capsys: pytest.CaptureFixture[str] |
| 57 | +) -> None: |
| 58 | + results = tmp_path / "results.json" |
| 59 | + baselines = tmp_path / "baselines.json" |
| 60 | + _write_results( |
| 61 | + results, |
| 62 | + [ |
| 63 | + {"name": "test_new_bench", "stats": {"mean": 0.01}}, |
| 64 | + {"name": GATED_BENCH, "stats": {"mean": 0.0001}}, |
| 65 | + ], |
| 66 | + ) |
| 67 | + _write_baselines( |
| 68 | + baselines, |
| 69 | + {"summary-cache": {GATED_BENCH: 0.0001}}, |
| 70 | + ) |
| 71 | + |
| 72 | + assert check_regression(results, baselines) == 0 |
| 73 | + out = capsys.readouterr().out |
| 74 | + assert "WARN: 'test_new_bench' has no baseline yet" in out |
| 75 | + |
| 76 | + |
| 77 | +def test_regression_over_threshold_fails(tmp_path, capsys: pytest.CaptureFixture[str]) -> None: |
| 78 | + results = tmp_path / "results.json" |
| 79 | + baselines = tmp_path / "baselines.json" |
| 80 | + _write_results( |
| 81 | + results, |
| 82 | + [{"name": GATED_BENCH, "stats": {"mean": 0.00025}}], |
| 83 | + ) |
| 84 | + _write_baselines( |
| 85 | + baselines, |
| 86 | + {"summary-cache": {GATED_BENCH: 0.0002}}, |
| 87 | + ) |
| 88 | + |
| 89 | + assert check_regression(results, baselines) == 1 |
| 90 | + out = capsys.readouterr().out |
| 91 | + assert "REGRESSION" in out |
| 92 | + |
| 93 | + |
| 94 | +def test_within_threshold_passes(tmp_path) -> None: |
| 95 | + results = tmp_path / "results.json" |
| 96 | + baselines = tmp_path / "baselines.json" |
| 97 | + _write_results( |
| 98 | + results, |
| 99 | + [{"name": GATED_BENCH, "stats": {"mean": 0.00022}}], |
| 100 | + ) |
| 101 | + _write_baselines( |
| 102 | + baselines, |
| 103 | + {"summary-cache": {GATED_BENCH: 0.0002}}, |
| 104 | + ) |
| 105 | + |
| 106 | + assert check_regression(results, baselines) == 0 |
| 107 | + |
| 108 | + |
| 109 | +def test_load_results_rejects_malformed_json(tmp_path) -> None: |
| 110 | + path = tmp_path / "bad.json" |
| 111 | + path.write_text("{not json", encoding="utf-8") |
| 112 | + with pytest.raises(BenchmarkDataError, match="invalid JSON"): |
| 113 | + load_results(path) |
| 114 | + |
| 115 | + |
| 116 | +def test_load_results_requires_benchmarks_array(tmp_path) -> None: |
| 117 | + path = tmp_path / "results.json" |
| 118 | + path.write_text("{}", encoding="utf-8") |
| 119 | + with pytest.raises(BenchmarkDataError, match="'benchmarks' array"): |
| 120 | + load_results(path) |
| 121 | + |
| 122 | + |
| 123 | +def test_load_results_rejects_missing_file(tmp_path) -> None: |
| 124 | + with pytest.raises(BenchmarkDataError, match="cannot read"): |
| 125 | + load_results(tmp_path / "missing.json") |
| 126 | + |
| 127 | + |
| 128 | +def test_zero_baseline_skips_ratio_check(tmp_path, capsys: pytest.CaptureFixture[str]) -> None: |
| 129 | + results = tmp_path / "results.json" |
| 130 | + baselines = tmp_path / "baselines.json" |
| 131 | + _write_results( |
| 132 | + results, |
| 133 | + [{"name": GATED_BENCH, "stats": {"mean": 0.00025}}], |
| 134 | + ) |
| 135 | + _write_baselines( |
| 136 | + baselines, |
| 137 | + {"summary-cache": {GATED_BENCH: 0.0}}, |
| 138 | + ) |
| 139 | + |
| 140 | + assert check_regression(results, baselines) == 0 |
| 141 | + assert f"baseline for '{GATED_BENCH}' is zero" in capsys.readouterr().out |
| 142 | + |
| 143 | + |
| 144 | +def test_exactly_at_threshold_passes(tmp_path) -> None: |
| 145 | + results = tmp_path / "results.json" |
| 146 | + baselines = tmp_path / "baselines.json" |
| 147 | + _write_results( |
| 148 | + results, |
| 149 | + [{"name": GATED_BENCH, "stats": {"mean": 0.00024}}], |
| 150 | + ) |
| 151 | + _write_baselines( |
| 152 | + baselines, |
| 153 | + {"summary-cache": {GATED_BENCH: 0.0002}}, |
| 154 | + ) |
| 155 | + |
| 156 | + assert check_regression(results, baselines) == 0 |
| 157 | + |
| 158 | + |
| 159 | +def test_missing_current_result_fails(tmp_path, capsys: pytest.CaptureFixture[str]) -> None: |
| 160 | + results = tmp_path / "results.json" |
| 161 | + baselines = tmp_path / "baselines.json" |
| 162 | + _write_results(results, []) |
| 163 | + _write_baselines( |
| 164 | + baselines, |
| 165 | + {"summary-cache": {GATED_BENCH: 0.0002}}, |
| 166 | + ) |
| 167 | + |
| 168 | + assert check_regression(results, baselines) == 1 |
| 169 | + out = capsys.readouterr().out |
| 170 | + assert "MISSING" in out |
| 171 | + assert "no current result for gated baseline" in out |
| 172 | + |
| 173 | + |
| 174 | +def test_main_reports_benchmark_data_error(tmp_path, capsys: pytest.CaptureFixture[str]) -> None: |
| 175 | + from scripts.check_benchmark_regression import main |
| 176 | + |
| 177 | + bad = tmp_path / "bad.json" |
| 178 | + bad.write_text("{}", encoding="utf-8") |
| 179 | + baselines = tmp_path / "baselines.json" |
| 180 | + _write_baselines(baselines, {"summary-cache": {GATED_BENCH: 0.0002}}) |
| 181 | + |
| 182 | + assert main([str(bad), str(baselines)]) == 2 |
| 183 | + assert "ERROR:" in capsys.readouterr().err |
| 184 | + |
| 185 | + |
| 186 | +def test_duplicate_baseline_name_raises(tmp_path) -> None: |
| 187 | + baselines = tmp_path / "baselines.json" |
| 188 | + _write_baselines( |
| 189 | + baselines, |
| 190 | + { |
| 191 | + "summary-cache": {GATED_BENCH: 0.0002}, |
| 192 | + "export": {GATED_BENCH: 0.0003}, |
| 193 | + }, |
| 194 | + ) |
| 195 | + |
| 196 | + with pytest.raises(BenchmarkDataError, match="duplicate benchmark name"): |
| 197 | + load_baseline_means(baselines) |
| 198 | + |
| 199 | + |
| 200 | +def test_load_baseline_means_rejects_non_dict_group(tmp_path) -> None: |
| 201 | + baselines = tmp_path / "baselines.json" |
| 202 | + baselines.write_text( |
| 203 | + json.dumps({"groups": {"summary-cache": "not-a-dict"}}), |
| 204 | + encoding="utf-8", |
| 205 | + ) |
| 206 | + |
| 207 | + with pytest.raises(BenchmarkDataError, match="must be an object"): |
| 208 | + load_baseline_means(baselines) |
0 commit comments