| 1 | from __future__ import annotations
|
| 2 |
|
| 3 | import json
|
| 4 | import subprocess
|
| 5 | import sys
|
| 6 | import tempfile
|
| 7 | import unittest
|
| 8 | from pathlib import Path
|
| 9 |
|
| 10 |
|
| 11 | ROOT = Path(__file__).resolve().parents[1]
|
| 12 | SCRIPT = ROOT / "scripts" / "result_delta_report.py"
|
| 13 |
|
| 14 |
|
| 15 | class ResultDeltaReportTest(unittest.TestCase):
|
| 16 | def test_report_ranks_improvements_and_regressions(self) -> None:
|
| 17 | old_rows = [
|
| 18 | {
|
| 19 | "scenario": "tiny_text",
|
| 20 | "subject": "oak_installed",
|
| 21 | "operation": "status.clean",
|
| 22 | "run": 0,
|
| 23 | "elapsed_ms": 10,
|
| 24 | "estimated_tokens_total": 100,
|
| 25 | "tool_call_count": 1,
|
| 26 | },
|
| 27 | {
|
| 28 | "scenario": "tiny_text",
|
| 29 | "subject": "oak_installed",
|
| 30 | "operation": "diff.dirty",
|
| 31 | "run": 0,
|
| 32 | "elapsed_ms": 12,
|
| 33 | "estimated_tokens_total": 80,
|
| 34 | "tool_call_count": 2,
|
| 35 | },
|
| 36 | ]
|
| 37 | new_rows = [
|
| 38 | {
|
| 39 | "scenario": "tiny_text",
|
| 40 | "subject": "oak_installed",
|
| 41 | "operation": "status.clean",
|
| 42 | "run": 0,
|
| 43 | "elapsed_ms": 8,
|
| 44 | "estimated_tokens_total": 90,
|
| 45 | "tool_call_count": 1,
|
| 46 | },
|
| 47 | {
|
| 48 | "scenario": "tiny_text",
|
| 49 | "subject": "oak_installed",
|
| 50 | "operation": "diff.dirty",
|
| 51 | "run": 0,
|
| 52 | "elapsed_ms": 15,
|
| 53 | "estimated_tokens_total": 70,
|
| 54 | "tool_call_count": 2,
|
| 55 | },
|
| 56 | ]
|
| 57 |
|
| 58 | with tempfile.TemporaryDirectory() as tmp:
|
| 59 | tmpdir = Path(tmp)
|
| 60 | old_path = tmpdir / "old.jsonl"
|
| 61 | new_path = tmpdir / "new.jsonl"
|
| 62 | old_path.write_text("\n".join(json.dumps(row) for row in old_rows) + "\n")
|
| 63 | new_path.write_text("\n".join(json.dumps(row) for row in new_rows) + "\n")
|
| 64 |
|
| 65 | proc = subprocess.run(
|
| 66 | [sys.executable, str(SCRIPT), str(old_path), str(new_path), "--limit", "2"],
|
| 67 | cwd=ROOT,
|
| 68 | text=True,
|
| 69 | capture_output=True,
|
| 70 | check=False,
|
| 71 | )
|
| 72 |
|
| 73 | self.assertEqual(proc.returncode, 0, proc.stderr)
|
| 74 | self.assertIn("Top Improvements", proc.stdout)
|
| 75 | self.assertIn("`elapsed_ms`", proc.stdout)
|
| 76 | self.assertIn("Top Regressions", proc.stdout)
|
| 77 |
|
| 78 | def test_identity_keeps_profiles_and_tracks_distinct(self) -> None:
|
| 79 | old_rows = []
|
| 80 | new_rows = []
|
| 81 | for profile, track, old_ms, new_ms in (
|
| 82 | ("micro", "agent-default", 10, 8),
|
| 83 | ("smoke", "core-equivalent", 100, 90),
|
| 84 | ):
|
| 85 | old_rows.append(
|
| 86 | {
|
| 87 | "profile": profile,
|
| 88 | "benchmark_track": track,
|
| 89 | "scenario": "tiny_text",
|
| 90 | "subject": "oak_local",
|
| 91 | "subject_kind": "oak",
|
| 92 | "operation": "status.clean",
|
| 93 | "run": 0,
|
| 94 | "elapsed_ms": old_ms,
|
| 95 | }
|
| 96 | )
|
| 97 | new_rows.append(
|
| 98 | {
|
| 99 | "profile": profile,
|
| 100 | "benchmark_track": track,
|
| 101 | "scenario": "tiny_text",
|
| 102 | "subject": "oak_local",
|
| 103 | "subject_kind": "oak",
|
| 104 | "operation": "status.clean",
|
| 105 | "run": 0,
|
| 106 | "elapsed_ms": new_ms,
|
| 107 | }
|
| 108 | )
|
| 109 |
|
| 110 | with tempfile.TemporaryDirectory() as tmp:
|
| 111 | tmpdir = Path(tmp)
|
| 112 | old_path = tmpdir / "old.jsonl"
|
| 113 | new_path = tmpdir / "new.jsonl"
|
| 114 | old_path.write_text("\n".join(json.dumps(row) for row in old_rows) + "\n")
|
| 115 | new_path.write_text("\n".join(json.dumps(row) for row in new_rows) + "\n")
|
| 116 |
|
| 117 | proc = subprocess.run(
|
| 118 | [sys.executable, str(SCRIPT), str(old_path), str(new_path), "--limit", "5"],
|
| 119 | cwd=ROOT,
|
| 120 | text=True,
|
| 121 | capture_output=True,
|
| 122 | check=False,
|
| 123 | )
|
| 124 |
|
| 125 | self.assertEqual(proc.returncode, 0, proc.stderr)
|
| 126 | self.assertIn("- Shared row identities: 2", proc.stdout)
|
| 127 | self.assertIn("- Identity fields: profile, scenario, subject, subject_kind, benchmark_track, operation, run", proc.stdout)
|
| 128 | self.assertIn("profile=micro", proc.stdout)
|
| 129 | self.assertIn("profile=smoke", proc.stdout)
|
| 130 |
|
| 131 | def test_identity_ignores_dimensions_absent_from_older_baseline(self) -> None:
|
| 132 | old_rows = [
|
| 133 | {
|
| 134 | "scenario": "tiny_text",
|
| 135 | "subject": "oak_local",
|
| 136 | "operation": "status.clean",
|
| 137 | "run": 0,
|
| 138 | "elapsed_ms": 10,
|
| 139 | }
|
| 140 | ]
|
| 141 | new_rows = [
|
| 142 | {
|
| 143 | "profile": "smoke",
|
| 144 | "benchmark_track": "agent-default",
|
| 145 | "scenario": "tiny_text",
|
| 146 | "subject": "oak_local",
|
| 147 | "operation": "status.clean",
|
| 148 | "run": 0,
|
| 149 | "elapsed_ms": 8,
|
| 150 | }
|
| 151 | ]
|
| 152 | with tempfile.TemporaryDirectory() as tmp:
|
| 153 | tmpdir = Path(tmp)
|
| 154 | old_path = tmpdir / "old.jsonl"
|
| 155 | new_path = tmpdir / "new.jsonl"
|
| 156 | old_path.write_text("\n".join(json.dumps(row) for row in old_rows) + "\n")
|
| 157 | new_path.write_text("\n".join(json.dumps(row) for row in new_rows) + "\n")
|
| 158 | proc = subprocess.run(
|
| 159 | [sys.executable, str(SCRIPT), str(old_path), str(new_path)],
|
| 160 | cwd=ROOT,
|
| 161 | text=True,
|
| 162 | capture_output=True,
|
| 163 | check=False,
|
| 164 | )
|
| 165 | self.assertEqual(proc.returncode, 0, proc.stderr)
|
| 166 | self.assertIn("- Shared row identities: 1", proc.stdout)
|
| 167 | self.assertIn("- Identity fields: scenario, subject, operation, run", proc.stdout)
|
| 168 |
|
| 169 | def test_no_shared_identities_is_nonzero(self) -> None:
|
| 170 | with tempfile.TemporaryDirectory() as tmp:
|
| 171 | tmpdir = Path(tmp)
|
| 172 | old_path = tmpdir / "old.jsonl"
|
| 173 | new_path = tmpdir / "new.jsonl"
|
| 174 | old_path.write_text(json.dumps({"scenario": "a", "subject": "git", "operation": "status", "run": 0}) + "\n")
|
| 175 | new_path.write_text(json.dumps({"scenario": "b", "subject": "git", "operation": "status", "run": 0}) + "\n")
|
| 176 | proc = subprocess.run(
|
| 177 | [sys.executable, str(SCRIPT), str(old_path), str(new_path)],
|
| 178 | cwd=ROOT,
|
| 179 | text=True,
|
| 180 | capture_output=True,
|
| 181 | check=False,
|
| 182 | )
|
| 183 | self.assertEqual(proc.returncode, 1)
|
| 184 | self.assertIn("No shared row identities", proc.stdout)
|
| 185 |
|
| 186 | def test_non_finite_metric_values_are_ignored(self) -> None:
|
| 187 | old_rows = [
|
| 188 | {
|
| 189 | "scenario": "tiny_text",
|
| 190 | "subject": "oak_local",
|
| 191 | "operation": "status.clean",
|
| 192 | "run": 0,
|
| 193 | "elapsed_ms": "nan",
|
| 194 | }
|
| 195 | ]
|
| 196 | new_rows = [
|
| 197 | {
|
| 198 | "scenario": "tiny_text",
|
| 199 | "subject": "oak_local",
|
| 200 | "operation": "status.clean",
|
| 201 | "run": 0,
|
| 202 | "elapsed_ms": 1,
|
| 203 | }
|
| 204 | ]
|
| 205 | with tempfile.TemporaryDirectory() as tmp:
|
| 206 | tmpdir = Path(tmp)
|
| 207 | old_path = tmpdir / "old.jsonl"
|
| 208 | new_path = tmpdir / "new.jsonl"
|
| 209 | old_path.write_text("\n".join(json.dumps(row) for row in old_rows) + "\n")
|
| 210 | new_path.write_text("\n".join(json.dumps(row) for row in new_rows) + "\n")
|
| 211 | proc = subprocess.run(
|
| 212 | [sys.executable, str(SCRIPT), str(old_path), str(new_path)],
|
| 213 | cwd=ROOT,
|
| 214 | text=True,
|
| 215 | capture_output=True,
|
| 216 | check=False,
|
| 217 | )
|
| 218 | self.assertEqual(proc.returncode, 0, proc.stderr)
|
| 219 | self.assertIn("Top Improvements (0)", proc.stdout)
|
| 220 |
|
| 221 |
|
| 222 | if __name__ == "__main__":
|
| 223 | unittest.main()
|