Files

67 lines
2.4 KiB
Python

"""Several worker processes on one store: `pw run --shard I/N` in parallel, then one
unsharded replay that writes the sinks. Compares with a single process.
uv run python bench/bench_shards.py [-n 4000] [-w 4]
"""
import argparse
import json
import subprocess
import sys
import tempfile
import time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from bench_pipeline import build # noqa: E402
def pw(*args, cwd):
return subprocess.Popen([sys.executable, "-m", "pipewright.cli", *args], cwd=cwd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("-n", type=int, default=4000)
ap.add_argument("-w", type=int, default=4)
a = ap.parse_args()
single = Path(tempfile.mkdtemp(prefix="pw-single-"))
g1 = build(a.n, single)
t = time.perf_counter()
p = pw("run", str(g1), "--quiet", "--json", cwd=single)
out, err = p.communicate()
t_single = time.perf_counter() - t
assert p.returncode == 0, err[-2000:]
multi = Path(tempfile.mkdtemp(prefix="pw-shards-"))
g = build(a.n, multi)
t = time.perf_counter()
procs = [pw("run", str(g), "--quiet", "--json", "--shard", f"{i}/{a.w}", "--group", "bench", cwd=multi)
for i in range(a.w)]
results = [pr.communicate() for pr in procs]
t_shards = time.perf_counter() - t
for (o, e), pr in zip(results, procs):
assert pr.returncode == 0, e[-2000:]
statuses = [json.loads(o)["status"] for o, _ in results]
t = time.perf_counter()
p = pw("run", str(g), "--quiet", "--json", cwd=multi)
out2, err2 = p.communicate()
t_final = time.perf_counter() - t
final = json.loads(out2)
f = subprocess.run([sys.executable, "-m", "pipewright.cli", "funnel", str(g), "--group", "bench", "--json"],
cwd=multi, capture_output=True, text=True)
grp = json.loads(f.stdout)
print(json.dumps({
"items": a.n, "workers": a.w, "single_process_s": round(t_single, 2),
"shards_wall_s": round(t_shards, 2), "shard_statuses": statuses,
"final_replay_s": round(t_final, 2), "final_status": final["status"],
"speedup_work": round(t_single / t_shards, 2),
"group_funnel_keep_ok": grp["nodes"]["keep"]["events"].get("ok"),
"final_sink_rows": final["funnel"]["nodes"]["route"]["out"],
}, indent=1))
if __name__ == "__main__":
main()