| 1 | from __future__ import annotations
|
| 2 |
|
| 3 | import contextlib
|
| 4 | import io
|
| 5 | import json
|
| 6 | from pathlib import Path
|
| 7 | import sys
|
| 8 | import tempfile
|
| 9 | import unittest
|
| 10 | from unittest.mock import patch
|
| 11 |
|
| 12 | sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
|
| 13 |
|
| 14 | import benchmark_stats
|
| 15 | from oakbench.baseline_book import BaselineCell, Target
|
| 16 | from oakbench.populations import population_conflicts
|
| 17 | from oakbench.scorecard import evaluate_targets
|
| 18 |
|
| 19 |
|
| 20 | def row(**extra):
|
| 21 | return dict(subject="oak_local", lane="core", profile="smoke", scenario="tiny",
|
| 22 | operation="status.clean", returncode=0, elapsed_ms=1.0, **extra)
|
| 23 |
|
| 24 |
|
| 25 | class PopulationReportingTest(unittest.TestCase):
|
| 26 | def test_incompatible_identity_is_rejected_by_cli_and_scorecard(self):
|
| 27 | variants = [
|
| 28 | ({"runner_class": "arm"}, {"runner_class": "x86"}, "runner_class"),
|
| 29 | ({"benchmark_track": "core-equivalent"}, {"benchmark_track": "agent-default"}, "benchmark_track"),
|
| 30 | ({"cache_state": "cold"}, {"cache_state": "warm"}, "cache_state"),
|
| 31 | ({"load_tier": "none"}, {"load_tier": "cpu50"}, "load_tier"),
|
| 32 | ({"fixture_version": 1}, {"fixture_version": 2}, "fixture_version"),
|
| 33 | ({"fixture": {"fixture_version": 1}}, {"fixture": {"fixture_version": 2}}, "fixture.fixture_version"),
|
| 34 | ({"scenario_version": 1}, {"scenario_version": 2}, "scenario_version"),
|
| 35 | ({"oracle_version": 1}, {"oracle_version": 2}, "oracle_version"),
|
| 36 | ({"remote_server": "a"}, {"remote_server": "b"}, "remote_server"),
|
| 37 | ({"netshape_profile": "lan-1ms"}, {"netshape_profile": "wan-100ms"}, "netshape_profile"),
|
| 38 | ({"env_isolation_version": 1}, {"env_isolation_version": 2}, "env_isolation_version"),
|
| 39 | ({"calibration_version": "a"}, {"calibration_version": "b"}, "calibration_version"),
|
| 40 | ({"remote_transport": "network"}, {"remote_transport": "local_file"}, "remote_transport"),
|
| 41 | ({"agent": {"instruction_level": "zero-shot"}}, {"agent": {"instruction_level": "full-docs"}}, "agent.instruction_level"),
|
| 42 | ({"agent": {"model": "a"}}, {"agent": {"model": "b"}}, "agent.model"),
|
| 43 | ({"runner_id": "known"}, {}, "runner_id"),
|
| 44 | ({"subject_details": {"oak_local": {"binary_sha256": "a"}}},
|
| 45 | {"subject_details": {"oak_local": {"binary_sha256": "b"}}}, "subject_details.binary_sha256"),
|
| 46 | ]
|
| 47 | for left, right, field in variants:
|
| 48 | with self.subTest(field=field):
|
| 49 | rows = [row(**left), row(**right)]
|
| 50 | with tempfile.TemporaryDirectory() as tmp:
|
| 51 | path = Path(tmp) / "rows.jsonl"
|
| 52 | path.write_text("\n".join(json.dumps(item) for item in rows))
|
| 53 | output = io.StringIO()
|
| 54 | with patch.object(sys, "argv", ["benchmark_stats.py", str(path)]), contextlib.redirect_stdout(output):
|
| 55 | self.assertEqual(benchmark_stats.main(), 1)
|
| 56 | self.assertIn(field, output.getvalue())
|
| 57 | self.assertNotIn("median CI", output.getvalue())
|
| 58 | target = Target("t", "core", "smoke", "tiny", "status.clean", "oak_local", "git",
|
| 59 | "elapsed_ms", "median", "ratio_at_most", .8, {})
|
| 60 | baseline = BaselineCell("core", "smoke", "tiny", "status.clean", "git", "elapsed_ms", "median", 10)
|
| 61 | result = evaluate_targets(rows, [target], {baseline.key: baseline})[0]
|
| 62 | self.assertEqual(result["verdict"], "INVALID")
|
| 63 | self.assertIn(field, " ".join(result["messages"]))
|
| 64 |
|
| 65 | def test_legacy_rows_and_repetitions_do_not_invent_identity(self):
|
| 66 | self.assertEqual(population_conflicts([row(run=1), row(run=2)]), [])
|
| 67 | self.assertEqual(population_conflicts([row(runner_id="same", run=1), row(runner_id="same", run=2)]), [])
|
| 68 |
|
| 69 | def test_subject_metadata_for_other_subjects_does_not_change_identity(self):
|
| 70 | left = row(subject_details={"oak_local": {"binary_sha256": "same"}, "git": {"version": "a"}})
|
| 71 | right = row(subject_details={"oak_local": {"binary_sha256": "same"}, "git": {"version": "b"}})
|
| 72 | self.assertEqual(population_conflicts([left, right]), [])
|
| 73 |
|
| 74 |
|
| 75 | if __name__ == "__main__":
|
| 76 | unittest.main()
|