|
| 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 check_regression |
| 10 | + |
| 11 | + |
| 12 | +def _write_results(path, benchmarks: list[dict]) -> None: |
| 13 | + path.write_text( |
| 14 | + json.dumps({"benchmarks": benchmarks}, indent=2), |
| 15 | + encoding="utf-8", |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +def _write_baselines(path, groups: dict[str, dict[str, float]]) -> None: |
| 20 | + path.write_text( |
| 21 | + json.dumps({"groups": groups}, indent=2), |
| 22 | + encoding="utf-8", |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +def test_missing_baseline_warns_without_failing( |
| 27 | + tmp_path, capsys: pytest.CaptureFixture[str] |
| 28 | +) -> None: |
| 29 | + results = tmp_path / "results.json" |
| 30 | + baselines = tmp_path / "baselines.json" |
| 31 | + _write_results( |
| 32 | + results, |
| 33 | + [ |
| 34 | + {"name": "test_new_bench", "stats": {"mean": 0.01}}, |
| 35 | + {"name": "test_parse_session_small", "stats": {"mean": 0.0001}}, |
| 36 | + ], |
| 37 | + ) |
| 38 | + _write_baselines( |
| 39 | + baselines, |
| 40 | + {"parse": {"test_parse_session_small": 0.0001}}, |
| 41 | + ) |
| 42 | + |
| 43 | + assert check_regression(results, baselines) == 0 |
| 44 | + out = capsys.readouterr().out |
| 45 | + assert "WARN: 'test_new_bench' has no baseline yet" in out |
| 46 | + |
| 47 | + |
| 48 | +def test_regression_over_threshold_fails(tmp_path, capsys: pytest.CaptureFixture[str]) -> None: |
| 49 | + results = tmp_path / "results.json" |
| 50 | + baselines = tmp_path / "baselines.json" |
| 51 | + _write_results( |
| 52 | + results, |
| 53 | + [{"name": "test_parse_session_small", "stats": {"mean": 0.0002}}], |
| 54 | + ) |
| 55 | + _write_baselines( |
| 56 | + baselines, |
| 57 | + {"parse": {"test_parse_session_small": 0.0001}}, |
| 58 | + ) |
| 59 | + |
| 60 | + assert check_regression(results, baselines) == 1 |
| 61 | + out = capsys.readouterr().out |
| 62 | + assert "REGRESSION" in out |
| 63 | + |
| 64 | + |
| 65 | +def test_within_threshold_passes(tmp_path) -> None: |
| 66 | + results = tmp_path / "results.json" |
| 67 | + baselines = tmp_path / "baselines.json" |
| 68 | + _write_results( |
| 69 | + results, |
| 70 | + [{"name": "test_parse_session_small", "stats": {"mean": 0.00011}}], |
| 71 | + ) |
| 72 | + _write_baselines( |
| 73 | + baselines, |
| 74 | + {"parse": {"test_parse_session_small": 0.0001}}, |
| 75 | + ) |
| 76 | + |
| 77 | + assert check_regression(results, baselines) == 0 |
0 commit comments