Debian Codegraph @

benchmark/bench_runner.py

raw
1 #!/usr/bin/env python3
2 """
3 Run each caio backend in a separate subprocess so thread pools from one
4 backend cannot influence measurements of the next.
5
6 Every per-backend CSV uses the same schema. The merge step validates that
7 schema instead of silently attaching an outdated header to wider rows.
8
9 Usage:
10 CAIO_BENCH_DATA=/tmp/caio-bench CAIO_RESULTS=/tmp/results uv run python bench_runner.py
11 """
12 import csv
13 import os
14 import pathlib
15 import subprocess
16 import sys
17
18 BACKENDS = ["linux_uring", "linux_aio", "thread_aio", "python_aio"]
19
20 RESULTS_DIR = pathlib.Path(os.environ.get("CAIO_RESULTS", "/tmp/results"))
21 DATA_DIR = pathlib.Path(os.environ.get("CAIO_BENCH_DATA", "/tmp/caio-bench"))
22 BENCH = pathlib.Path(__file__).parent / "bench.py"
23
24 env = os.environ.copy()
25
26 CSV_COLUMNS = [
27 "backend",
28 "sweep",
29 "op",
30 "concurrency",
31 "chunk_bytes",
32 "latency_us",
33 "wall_s",
34 "n_ops",
35 ]
36
37
38 def run_backend(name: str):
39 print(f"\n{'━' * 80}", flush=True)
40 print(f" backend: {name}", flush=True)
41 print(f"{'━' * 80}", flush=True)
42
43 proc = subprocess.run(
44 [sys.executable, str(BENCH), "--backend", name],
45 env=env,
46 cwd=BENCH.parent,
47 )
48
49 if proc.returncode != 0:
50 print(f"[!] {name} exited with code {proc.returncode}", flush=True)
51
52
53 def merge_results():
54 # Merge all per-backend CSVs into one, rejecting incompatible files
55 # rather than producing a syntactically valid but column-shifted result.
56 rows: list[dict[str, str]] = []
57
58 def collect(path: pathlib.Path):
59 if not path.exists():
60 return
61 with path.open(newline="") as fp:
62 reader = csv.DictReader(fp)
63 if reader.fieldnames != CSV_COLUMNS:
64 print(
65 f"[!] {path}: incompatible CSV header "
66 f"{reader.fieldnames!r}; expected {CSV_COLUMNS!r}",
67 flush=True,
68 )
69 return
70 rows.extend(reader)
71
72 for name in BACKENDS:
73 collect(RESULTS_DIR / f"bench_{name}.csv")
74
75 merged = RESULTS_DIR / "bench_all.csv"
76 with merged.open("w", newline="") as fp:
77 writer = csv.DictWriter(fp, fieldnames=CSV_COLUMNS)
78 writer.writeheader()
79 writer.writerows(rows)
80 print(f"\nMerged CSV → {merged}")
81
82
83 def main():
84 RESULTS_DIR.mkdir(parents=True, exist_ok=True)
85
86 for name in BACKENDS:
87 run_backend(name)
88
89 merge_results()
90
91
92 if __name__ == "__main__":
93 main()