-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_check_benchmark_regression.py
More file actions
342 lines (262 loc) · 11.1 KB
/
Copy pathtest_check_benchmark_regression.py
File metadata and controls
342 lines (262 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""Tests for scripts/check_benchmark_regression.py."""
from __future__ import annotations
import json
import pytest
from scripts.check_benchmark_regression import (
EXCLUDED_FROM_GATE,
BenchmarkDataError,
check_regression,
load_baseline_means,
load_results,
normalize_benchmark_name,
)
GATED_BENCH = "test_fingerprint_workspace_entries[10]"
# Pytest benchmark node IDs (parametrize ids) that must stay in EXCLUDED_FROM_GATE.
# Keep in sync with scripts/check_benchmark_regression.py — see that file for rationale
# per exclusion. Export ZIP benches (composers-10/50): ungatable CI variance; see
# benchmarks/README.md § "Export ZIP benchmarks".
EXPECTED_EXCLUDED_FROM_GATE = (
"test_summary_cache_round_trip",
"test_summary_cache_lookup[hit]",
"test_summary_cache_lookup[miss]",
"test_composer_map_cache_lookup[hit]",
"test_composer_map_cache_lookup[miss]",
"test_tab_summary_cache_lookup[hit]",
"test_tab_summary_cache_lookup[miss]",
"test_post_export_zip[composers-10]",
"test_post_export_zip[composers-50]",
)
def _write_results(path, benchmarks: list[dict]) -> None:
path.write_text(
json.dumps({"benchmarks": benchmarks}, indent=2),
encoding="utf-8",
)
def _write_baselines(path, groups: dict[str, dict[str, float]]) -> None:
path.write_text(
json.dumps({"groups": groups}, indent=2),
encoding="utf-8",
)
def test_excluded_from_gate_matches_benchmark_ids() -> None:
assert EXCLUDED_FROM_GATE == frozenset(EXPECTED_EXCLUDED_FROM_GATE)
@pytest.mark.parametrize("excluded_bench", EXPECTED_EXCLUDED_FROM_GATE)
def test_excluded_benchmark_skips_regression_gate(
tmp_path, excluded_bench: str
) -> None:
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(
results,
[{"name": excluded_bench, "stats": {"mean": 1.0}}],
)
_write_baselines(
baselines,
{"summary-cache": {excluded_bench: 0.1}},
)
assert check_regression(results, baselines) == 0
@pytest.mark.parametrize("excluded_bench", EXPECTED_EXCLUDED_FROM_GATE)
def test_excluded_benchmark_missing_result_does_not_fail(
tmp_path, excluded_bench: str
) -> None:
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(results, [])
_write_baselines(
baselines,
{"summary-cache": {excluded_bench: 0.1}},
)
assert check_regression(results, baselines) == 0
def test_normalize_benchmark_name_strips_module_prefix() -> None:
full = "tests/benchmarks/test_summary_cache_bench.py::test_summary_cache_lookup[hit]"
assert normalize_benchmark_name(full) == "test_summary_cache_lookup[hit]"
assert normalize_benchmark_name("test_summary_cache_lookup[hit]") == "test_summary_cache_lookup[hit]"
def test_normalize_benchmark_name_preserves_colons_in_param_values() -> None:
short = "test_x[param::v]"
full = f"tests/benchmarks/test_x.py::{short}"
assert normalize_benchmark_name(short) == short
assert normalize_benchmark_name(full) == short
def test_load_results_normalizes_full_node_id(tmp_path) -> None:
path = tmp_path / "results.json"
_write_results(
path,
[
{
"name": "tests/benchmarks/test_summary_cache_bench.py::test_summary_cache_lookup[hit]",
"stats": {"mean": 0.0001},
}
],
)
assert load_results(path)["test_summary_cache_lookup[hit]"] == pytest.approx(0.0001)
def test_missing_baseline_warns_without_failing(
tmp_path, capsys: pytest.CaptureFixture[str]
) -> None:
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(
results,
[
{"name": "test_new_bench", "stats": {"mean": 0.01}},
{"name": GATED_BENCH, "stats": {"mean": 0.001}},
],
)
_write_baselines(
baselines,
{"summary-cache": {GATED_BENCH: 0.001}},
)
assert check_regression(results, baselines) == 0
out = capsys.readouterr().out
assert "WARN: 'test_new_bench' has no baseline yet" in out
def test_regression_over_threshold_fails(tmp_path, capsys: pytest.CaptureFixture[str]) -> None:
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(
results,
[{"name": GATED_BENCH, "stats": {"mean": 0.0025}}],
)
_write_baselines(
baselines,
{"summary-cache": {GATED_BENCH: 0.002}},
)
assert check_regression(results, baselines) == 1
out = capsys.readouterr().out
assert "REGRESSION" in out
def test_within_threshold_passes(tmp_path) -> None:
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(
results,
[{"name": GATED_BENCH, "stats": {"mean": 0.0022}}],
)
_write_baselines(
baselines,
{"summary-cache": {GATED_BENCH: 0.002}},
)
assert check_regression(results, baselines) == 0
def test_load_results_rejects_malformed_json(tmp_path) -> None:
path = tmp_path / "bad.json"
path.write_text("{not json", encoding="utf-8")
with pytest.raises(BenchmarkDataError, match="invalid JSON"):
load_results(path)
def test_load_results_requires_benchmarks_array(tmp_path) -> None:
path = tmp_path / "results.json"
path.write_text("{}", encoding="utf-8")
with pytest.raises(BenchmarkDataError, match="'benchmarks' array"):
load_results(path)
def test_load_results_rejects_missing_file(tmp_path) -> None:
with pytest.raises(BenchmarkDataError, match="cannot read"):
load_results(tmp_path / "missing.json")
def test_zero_baseline_skips_ratio_check(tmp_path, capsys: pytest.CaptureFixture[str]) -> None:
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(
results,
[{"name": GATED_BENCH, "stats": {"mean": 0.0025}}],
)
_write_baselines(
baselines,
{"summary-cache": {GATED_BENCH: 0.0}},
)
assert check_regression(results, baselines) == 0
assert f"baseline for '{GATED_BENCH}' is zero" in capsys.readouterr().out
def test_exactly_at_threshold_passes(tmp_path) -> None:
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(
results,
[{"name": GATED_BENCH, "stats": {"mean": 0.0024}}],
)
_write_baselines(
baselines,
{"summary-cache": {GATED_BENCH: 0.002}},
)
assert check_regression(results, baselines) == 0
def test_missing_current_result_fails(tmp_path, capsys: pytest.CaptureFixture[str]) -> None:
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(results, [])
_write_baselines(
baselines,
{"summary-cache": {GATED_BENCH: 0.002}},
)
assert check_regression(results, baselines) == 1
out = capsys.readouterr().out
assert "MISSING" in out
assert "no current result for gated baseline" in out
def test_main_reports_benchmark_data_error(tmp_path, capsys: pytest.CaptureFixture[str]) -> None:
from scripts.check_benchmark_regression import main
bad = tmp_path / "bad.json"
bad.write_text("{}", encoding="utf-8")
baselines = tmp_path / "baselines.json"
_write_baselines(baselines, {"summary-cache": {GATED_BENCH: 0.002}})
assert main([str(bad), str(baselines)]) == 2
assert "ERROR:" in capsys.readouterr().err
def test_duplicate_baseline_name_raises(tmp_path) -> None:
baselines = tmp_path / "baselines.json"
_write_baselines(
baselines,
{
"summary-cache": {GATED_BENCH: 0.002},
"export": {GATED_BENCH: 0.003},
},
)
with pytest.raises(BenchmarkDataError, match="duplicate benchmark name"):
load_baseline_means(baselines)
def test_load_baseline_means_rejects_non_dict_group(tmp_path) -> None:
baselines = tmp_path / "baselines.json"
baselines.write_text(
json.dumps({"groups": {"summary-cache": "not-a-dict"}}),
encoding="utf-8",
)
with pytest.raises(BenchmarkDataError, match="must be an object"):
load_baseline_means(baselines)
def test_stale_baseline_fails(tmp_path, capsys: pytest.CaptureFixture[str]) -> None:
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(
results,
[{"name": GATED_BENCH, "stats": {"mean": 0.0005}}],
)
_write_baselines(
baselines,
{"summary-cache": {GATED_BENCH: 0.002}},
)
assert check_regression(results, baselines) == 1
out = capsys.readouterr().out
assert "STALE" in out
def test_main_rejects_invalid_threshold(tmp_path, capsys: pytest.CaptureFixture[str]) -> None:
from scripts.check_benchmark_regression import main
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(results, [{"name": GATED_BENCH, "stats": {"mean": 0.001}}])
_write_baselines(baselines, {"summary-cache": {GATED_BENCH: 0.002}})
assert main([str(results), str(baselines), "--threshold", "1.0"]) == 2
assert "threshold must be greater than 1" in capsys.readouterr().err
def test_main_rejects_invalid_stale_floor(tmp_path, capsys: pytest.CaptureFixture[str]) -> None:
from scripts.check_benchmark_regression import main
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(results, [{"name": GATED_BENCH, "stats": {"mean": 0.001}}])
_write_baselines(baselines, {"summary-cache": {GATED_BENCH: 0.002}})
assert main([str(results), str(baselines), "--stale-floor", "1.5"]) == 2
assert "stale_floor must be between 0 and 1" in capsys.readouterr().err
def test_check_regression_rejects_invalid_threshold(tmp_path) -> None:
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(results, [{"name": GATED_BENCH, "stats": {"mean": 0.001}}])
_write_baselines(baselines, {"summary-cache": {GATED_BENCH: 0.002}})
with pytest.raises(BenchmarkDataError, match="threshold must be greater than 1"):
check_regression(results, baselines, threshold=1.0)
def test_check_regression_rejects_non_finite_threshold(tmp_path) -> None:
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(results, [{"name": GATED_BENCH, "stats": {"mean": 0.001}}])
_write_baselines(baselines, {"summary-cache": {GATED_BENCH: 0.002}})
with pytest.raises(BenchmarkDataError, match="threshold must be finite"):
check_regression(results, baselines, threshold=float("nan"))
def test_main_rejects_non_finite_threshold(tmp_path, capsys: pytest.CaptureFixture[str]) -> None:
from scripts.check_benchmark_regression import main
results = tmp_path / "results.json"
baselines = tmp_path / "baselines.json"
_write_results(results, [{"name": GATED_BENCH, "stats": {"mean": 0.001}}])
_write_baselines(baselines, {"summary-cache": {GATED_BENCH: 0.002}})
assert main([str(results), str(baselines), "--threshold", "inf"]) == 2
assert "threshold must be finite" in capsys.readouterr().err