diff --git a/tests/ab_summary_test.py b/tests/ab_summary_test.py new file mode 100644 index 00000000..867757f4 --- /dev/null +++ b/tests/ab_summary_test.py @@ -0,0 +1,61 @@ +"""A/B log reports keep absolute timings when a percentage has a zero baseline.""" +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + + +def window(frame, interval, replay, b, n=60, world=20): + return (f'perf: frame {frame}: interval avg {interval:.2f} ms max 100.00 ms ({1000 / interval:.1f} fps); ' + f'replay avg {replay:.2f} ms; ab {b}/{n}; world {world:.1f} s\n') + + +class AbSummaryTest(unittest.TestCase): + def summarize(self, text): + with tempfile.TemporaryDirectory() as tmp: + log = Path(tmp) / 'synthetic.log' + log.write_text(text, encoding='utf-8') + return subprocess.run([sys.executable, str(ROOT / 'tools' / 'ab_summary.py'), str(log)], + capture_output=True, text=True) + + def test_no_replay_on_either_side_keeps_the_interval_comparison(self): + run = self.summarize(window(60, 20, 0, 0) + window(120, 10, 0, 60)) + self.assertEqual(run.returncode, 0, run.stderr) + self.assertIn('B - A: interval -10.00 ms (-50.0%), replay +0.00 ms (n/a: A is zero)', run.stdout) + + def test_replay_appearing_in_b_reports_its_absolute_cost(self): + run = self.summarize(window(60, 10, 0, 0) + window(120, 12, 2, 60)) + self.assertEqual(run.returncode, 0, run.stderr) + self.assertIn('replay +2.00 ms (n/a: A is zero)', run.stdout) + + def test_replay_disappearing_in_b_still_has_a_defined_percentage(self): + run = self.summarize(window(60, 12, 2, 0) + window(120, 10, 0, 60)) + self.assertEqual(run.returncode, 0, run.stderr) + self.assertIn('replay -2.00 ms (-100.0%)', run.stdout) + + def test_nonzero_means_remain_weighted_by_each_windows_frames(self): + text = window(60, 10, 2, 0, n=30) + window(120, 20, 4, 0) + text += window(180, 10, 2, 60) + run = self.summarize(text) + self.assertEqual(run.returncode, 0, run.stderr) + self.assertIn('interval -6.67 ms (-40.0%), replay -1.33 ms (-40.0%)', run.stdout) + + def test_load_and_mixed_windows_stay_excluded(self): + text = window(60, 100, 100, 0, world=5) + window(120, 100, 100, 30) + text += window(180, 10, 2, 0) + window(240, 12, 3, 60) + run = self.summarize(text) + self.assertEqual(run.returncode, 0, run.stderr) + self.assertIn('interval +2.00 ms (+20.0%), replay +1.00 ms (+50.0%)', run.stdout) + + def test_missing_side_keeps_the_diagnostic_and_failure_exit(self): + run = self.summarize(window(60, 10, 2, 0)) + self.assertEqual(run.returncode, 1) + self.assertIn('no whole A and B windows', run.stdout) + self.assertEqual(run.stderr, '') + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/run_all.py b/tests/run_all.py index fc74a277..00bf5944 100644 --- a/tests/run_all.py +++ b/tests/run_all.py @@ -64,6 +64,7 @@ TESTS = { 'exefs_patches_test': ('.venv', None, [], ''), 'exefs_plugins_test': ('.venv', None, [], ''), 'lift_cache_test': ('.venv', None, [], ''), + 'ab_summary_test': ('.venv', None, [], ''), 'settings_doc_check': ('.venv', None, [], ''), 'mouse_direct_check': ('.venv', 'cc', ['--cc', ''], 'compiles its checks with clang'), 'cheats_vm_check': ('.venv', 'cc', ['--cc', ''], 'compiles runtime/cheats.c into a harness with clang'), diff --git a/tools/ab_summary.py b/tools/ab_summary.py index 12f7b819..ce6e6fc2 100644 --- a/tools/ab_summary.py +++ b/tools/ab_summary.py @@ -34,7 +34,8 @@ def main(): iv, rp, secs = means(side[k]) print(f'{name}: {len(side[k]):3d} windows ({secs:5.1f} s), interval {iv:6.2f} ms ({1000 / iv:5.1f} fps), replay {rp:5.2f} ms') (ia, ra, _), (ib, rb, _) = means(side[0]), means(side[1]) - print(f'B - A: interval {ib - ia:+.2f} ms ({100 * (ib - ia) / ia:+.1f}%), replay {rb - ra:+.2f} ms ({100 * (rb - ra) / ra:+.1f}%)') + replay_change = f'{100 * (rb - ra) / ra:+.1f}%' if ra else 'n/a: A is zero' + print(f'B - A: interval {ib - ia:+.2f} ms ({100 * (ib - ia) / ia:+.1f}%), replay {rb - ra:+.2f} ms ({replay_change})') return 0