-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_check_benchmark_regression.py
More file actions
107 lines (83 loc) · 3.12 KB
/
Copy pathtest_check_benchmark_regression.py
File metadata and controls
107 lines (83 loc) · 3.12 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
"""Tests for scripts/check_benchmark_regression.py."""
from __future__ import annotations
import json
import pytest
from scripts.check_benchmark_regression import BenchmarkDataError, check_regression, load_results
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_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": "test_parse_session_small", "stats": {"mean": 0.0001}},
],
)
_write_baselines(
baselines,
{"parse": {"test_parse_session_small": 0.0001}},
)
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": "test_parse_session_small", "stats": {"mean": 0.0002}}],
)
_write_baselines(
baselines,
{"parse": {"test_parse_session_small": 0.0001}},
)
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": "test_parse_session_small", "stats": {"mean": 0.00011}}],
)
_write_baselines(
baselines,
{"parse": {"test_parse_session_small": 0.0001}},
)
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_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": "test_parse_session_small", "stats": {"mean": 0.0002}}],
)
_write_baselines(
baselines,
{"parse": {"test_parse_session_small": 0.0}},
)
assert check_regression(results, baselines) == 0
assert "baseline for 'test_parse_session_small' is zero" in capsys.readouterr().out