diff --git a/tests/perf_summary_test.py b/tests/perf_summary_test.py new file mode 100644 index 00000000..61fbbd5f --- /dev/null +++ b/tests/perf_summary_test.py @@ -0,0 +1,61 @@ +"""The performance log summary reports total frames divided by elapsed time. + + python tests/perf_summary_test.py +""" +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + + +def window(frame, fps, count=60): + # Synthetic text in runtime/perf.c perf_present's format; no game log is read. + line = (f'perf: frame {frame}: interval avg {1000 / fps:.2f} ms max 50.00 ms ({fps:.1f} fps); ' + 'render thread blocked: pacing 0% fence 0% drain 0% pipeline build 0% shader xlate 0% texture 0%') + return line + (f'; replay avg 1.00 ms; ab 0/{count}; world 20.0 s' if count is not None else '') + '\n' + + +class PerfSummaryTest(unittest.TestCase): + def summarize(self, text, lo=0, hi=1000): + with tempfile.TemporaryDirectory() as tmp: + log = Path(tmp) / 'perf.log' + log.write_text(text, encoding='utf-8') + result = subprocess.run([sys.executable, str(ROOT / 'tools' / 'perf_summary.py'), + str(log), str(lo), str(hi)], capture_output=True, text=True) + self.assertEqual(result.returncode, 0, result.stderr) + return result.stdout + + def test_equal_frame_windows_use_their_combined_duration(self): + # 120 frames in 0.6 + 2.4 seconds = 40 fps, not (100 + 25) / 2. + out = self.summarize(window(60, 100) + window(120, 25)) + self.assertIn('mean fps 40.0, min window fps 25.0, max frame 50.0 ms', out) + + def test_partial_window_uses_its_logged_frame_count(self): + # 30 frames in 0.3 seconds and 60 in 2.4 seconds = 33.3 fps. + out = self.summarize(window(60, 100, 30) + window(120, 25)) + self.assertIn('mean fps 33.3,', out) + + def test_older_logs_without_counts_keep_equal_window_weights(self): + out = self.summarize(window(60, 100, None) + window(120, 25, None)) + self.assertIn('mean fps 40.0,', out) + + def test_frame_bounds_only_include_the_selected_windows(self): + text = window(60, 100) + window(120, 50) + window(180, 25) + window(240, 200) + out = self.summarize(text, lo=60, hi=180) + self.assertIn('SUMMARY frames 60-180: mean fps 33.3, min window fps 25.0,', out) + self.assertNotIn('perf 60:', out) + self.assertNotIn('perf 240:', out) + + def test_constant_rate_is_unchanged(self): + out = self.summarize(window(60, 60) + window(120, 60)) + self.assertIn('mean fps 60.0, min window fps 60.0,', out) + + def test_no_perf_windows_does_not_print_a_frame_summary(self): + self.assertNotIn('SUMMARY frames', self.summarize('unrelated diagnostic\n')) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/run_all.py b/tests/run_all.py index fc74a277..f489d8c0 100644 --- a/tests/run_all.py +++ b/tests/run_all.py @@ -53,6 +53,7 @@ TESTS = { 'excl_mt': ('.venv', 'cc', [], 'compiles the threaded exclusive-monitor harness with clang'), 'check_isa_test': ('.venv', None, [], ''), 'compile_budget_test': ('.venv', None, [], ''), + 'perf_summary_test': ('.venv', None, [], ''), 'pack_shader_cache_test': ('.venv', None, [], ''), 'spin_waits_test': ('.venv', None, [], ''), 'worktree_test': ('.venv', None, [], ''), diff --git a/tools/perf_summary.py b/tools/perf_summary.py index 68725a7e..60289185 100644 --- a/tools/perf_summary.py +++ b/tools/perf_summary.py @@ -11,6 +11,7 @@ import sys PERF = re.compile(r'perf: frame (\d+): interval avg ([\d.]+) ms max ([\d.]+) ms \(([\d.]+) fps\); render thread blocked: ' r'pacing (\d+)%.*?pipeline build (\d+)%.*?texture (\d+)%') +FRAME_COUNT = re.compile(r'; ab \d+/(\d+);') GPU = re.compile(r'perf: frame (\d+): gpu avg (?:([\d.]+) ms|n/a).*?fence wait per frame: render thread ([\d.]+) ms, ' r'other threads ([\d.]+) ms; vram (?:([\d.]+) of ([\d.]+) MiB|n/a).*?ram working set ([\d.]+) MiB, ' r'private ([\d.]+) MiB; bound: (\w+)') @@ -69,6 +70,7 @@ def main(): lo = int(sys.argv[2]) if len(sys.argv) > 2 else 1560 hi = int(sys.argv[3]) if len(sys.argv) > 3 else 10**9 fr, fps, mx, gpu, vram, ram, bound = None, [], [], [], [], [], {} + frame_counts = [] dirty, dframes, dlast = [], [], None for line in open(sys.argv[1], encoding='utf-8', errors='replace'): if line.startswith('run_game:'): @@ -86,6 +88,8 @@ def main(): f = int(m[1]) if lo < f <= hi: fps.append(float(m[4])) + count = FRAME_COUNT.search(line) + frame_counts.append(int(count[1]) if count else 60) # assume full windows when older logs omit the count mx.append(float(m[3])) print(f'perf {f}: {m[4]} fps max {m[3]} pacing {m[5]}% pipe {m[6]}% tex {m[7]}%') elif m := GPU.search(line): @@ -109,7 +113,9 @@ def main(): elif (m := RWAIT.search(line)) and fr and lo < fr <= hi: print(f' rw{m[1]}: {m[2]}') if fps: - print(f'SUMMARY frames {lo}-{hi}: mean fps {statistics.mean(fps):.1f}, min window fps {min(fps):.1f}, ' + # Windows contain frames, not equal durations: total frames / total time is the weighted harmonic mean. + mean_fps = statistics.harmonic_mean(fps, weights=frame_counts) + print(f'SUMMARY frames {lo}-{hi}: mean fps {mean_fps:.1f}, min window fps {min(fps):.1f}, ' f'max frame {max(mx):.1f} ms') if ram: g = f'gpu mean {statistics.mean(gpu):.2f} ms' if gpu else 'gpu n/a'