-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbatch_benchmark.py
More file actions
171 lines (140 loc) · 5.44 KB
/
Copy pathbatch_benchmark.py
File metadata and controls
171 lines (140 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
"""
batch_benchmark.py — Automated benchmark runner.
Iterates over all GPU × API × difficulty combinations and runs the benchmark
executable for each, collecting results into ~/.gpu_bench/results.json.
Usage:
python scripts/batch_benchmark.py # auto-detect GPUs & APIs
python scripts/batch_benchmark.py --exe build/Release/gpu_benchmark.exe
python scripts/batch_benchmark.py --apis vulkan dx11 # only these APIs
python scripts/batch_benchmark.py --gpus 0 1 # only these GPU indices
python scripts/batch_benchmark.py --particles 1048576 16777216 # sweep particle counts
python scripts/batch_benchmark.py --frames 500 # frames per run
python scripts/batch_benchmark.py --dry-run # print commands without running
"""
import argparse
import itertools
import json
import os
import subprocess
import sys
import time
from pathlib import Path
DEFAULT_EXE_WIN = Path("build/Release/gpu_benchmark.exe")
DEFAULT_EXE_UNIX = Path("build/gpu_benchmark")
DIFFICULTY_MAP = {
"Low": 65536,
"Medium": 1048576,
"High": 4194304,
"Extreme": 16777216,
}
ALL_APIS = ["vulkan", "dx12", "dx11", "opengl", "metal"]
def find_exe() -> Path:
if sys.platform == "win32" and DEFAULT_EXE_WIN.exists():
return DEFAULT_EXE_WIN
if DEFAULT_EXE_UNIX.exists():
return DEFAULT_EXE_UNIX
print("ERROR: could not find gpu_benchmark executable.\n"
" Build the project first, or pass --exe <path>.", file=sys.stderr)
sys.exit(1)
def probe_gpus(exe: Path) -> list[dict]:
"""Run --help and parse available APIs; run briefly to detect GPUs."""
try:
out = subprocess.run(
[str(exe), "--help"],
capture_output=True, text=True, timeout=10
)
lines = out.stdout + out.stderr
except Exception:
lines = ""
apis = []
for api in ALL_APIS:
if api in lines.lower():
apis.append(api)
return apis
def run_single(exe: Path, api: str, gpu: int, particles: int,
frames: int, dry_run: bool) -> bool:
cmd = [
str(exe),
"--backend", api,
"--gpu", str(gpu),
"--benchmark", str(frames),
"--particles", str(particles),
]
label = f"[GPU {gpu}] {api.upper()} @ {particles:,} particles"
if dry_run:
print(f" DRY-RUN: {' '.join(cmd)}")
return True
print(f"\n{'='*60}")
print(f" {label}")
print(f" Command: {' '.join(cmd)}")
print(f"{'='*60}\n")
start = time.time()
try:
result = subprocess.run(cmd, timeout=300)
elapsed = time.time() - start
success = result.returncode == 0
status = "OK" if success else f"FAILED (exit {result.returncode})"
print(f"\n [{status}] {label} — {elapsed:.1f}s")
return success
except subprocess.TimeoutExpired:
print(f"\n [TIMEOUT] {label} — exceeded 300s limit")
return False
except Exception as e:
print(f"\n [ERROR] {label} — {e}")
return False
def main():
parser = argparse.ArgumentParser(
description="Batch-run benchmarks across GPU × API × particle count")
parser.add_argument("--exe", type=Path, default=None,
help="Path to gpu_benchmark executable")
parser.add_argument("--apis", nargs="+", default=None,
help="APIs to test (default: auto-detect)")
parser.add_argument("--gpus", nargs="+", type=int, default=[0],
help="GPU indices to test (default: 0)")
parser.add_argument("--particles", nargs="+", type=int, default=None,
help="Particle counts to sweep (default: all difficulties)")
parser.add_argument("--frames", type=int, default=500,
help="Benchmark frames per run (default: 500)")
parser.add_argument("--dry-run", action="store_true",
help="Print commands without executing")
args = parser.parse_args()
exe = args.exe or find_exe()
if not exe.exists():
print(f"ERROR: {exe} not found.", file=sys.stderr)
sys.exit(1)
apis = args.apis or probe_gpus(exe)
if not apis:
apis = ["vulkan"]
print(f"Executable: {exe}")
print(f"APIs: {', '.join(apis)}")
print(f"GPUs: {args.gpus}")
particle_counts = args.particles or list(DIFFICULTY_MAP.values())
print(f"Particles: {', '.join(f'{p:,}' for p in particle_counts)}")
print(f"Frames: {args.frames}")
combos = list(itertools.product(args.gpus, apis, particle_counts))
print(f"\nTotal runs: {len(combos)}")
if args.dry_run:
print("\n--- DRY RUN ---\n")
passed = 0
failed = 0
skipped_combos = []
for gpu, api, particles in combos:
ok = run_single(exe, api, gpu, particles, args.frames, args.dry_run)
if ok:
passed += 1
else:
failed += 1
skipped_combos.append(f"GPU {gpu} / {api} / {particles:,}")
print(f"\n{'='*60}")
print(f" Batch complete: {passed} passed, {failed} failed "
f"out of {len(combos)} total")
if skipped_combos:
print(f" Failed runs:")
for s in skipped_combos:
print(f" - {s}")
print(f"{'='*60}")
print(f"\nResults saved to {Path.home() / '.gpu_bench' / 'results.json'}")
print(f"Run 'python scripts/plot_results.py' to generate charts.")
if __name__ == "__main__":
main()