Files
pipewright/bench/bench_pipeline.py

104 lines
4.4 KiB
Python

"""Scheduler/store benchmark: a fake pipeline over N items (fake model + fake sandbox).
uv run python bench/bench_pipeline.py [-n 10000] [--profile]
Reports wall time and items/s for a cold run and a fully memoized replay, plus the
number of node executions, so per-execution overhead (scheduler + store) is visible.
"""
from __future__ import annotations
import argparse
import cProfile
import json
import pstats
import tempfile
import time
from pathlib import Path
import yaml
def build(n: int, root: Path) -> Path:
data = root / "data.jsonl"
with data.open("w") as f:
for i in range(n):
f.write(json.dumps({"id": f"it{i}", "title": f"item {i}", "body": "word " * (i % 17 + 3),
"group": f"g{i // 2}"}) + "\n")
(root / "pipewright.yaml").write_text(yaml.safe_dump({
"store": ".pipewright",
"providers": {"fake": {"type": "fake"}},
"models": {"m": {"provider": "fake", "model": "fake-m"}},
"sandboxes": {"sb": {"type": "fake", "slots": 64, "rules": [{"match": "fail", "exit_code": 1}]}},
}))
(root / "graph.yaml").write_text(yaml.safe_dump({
"name": "bench",
"nodes": {
"src": {"type": "source.file", "params": {"path": "data.jsonl", "kind": "rec"}},
"heur": {"type": "label.expr", "inputs": {"in": "src"},
"params": {"labels": {"words": "words(payload.body)"}}},
"keep": {"type": "filter.expr", "inputs": {"in": "heur"}, "params": {"expr": "labels.words >= 4"}},
"cls": {"type": "llm.label", "inputs": {"in": "keep"},
"params": {"model": "m", "prompt": "Classify {{ payload.title }}",
"labels": {"cat": {"type": "string", "enum": ["a", "b"]}}}},
"check": {"type": "sandbox.check", "inputs": {"in": "cls"},
"params": {"pool": "sb", "checks": [{"name": "ok", "command": "true"}]}},
"rep": {"type": "flatmap.repeat", "inputs": {"in": "check"}, "params": {"k": 2}},
"score": {"type": "label.expr", "inputs": {"in": "rep"},
"params": {"labels": {"passed": "len(key) % 2 == 0"}}},
"group": {"type": "join.group", "inputs": {"in": "score"},
"params": {"by": "labels.group", "aggregates": {"rate": ["mean", "labels.passed"]},
"keep_members": False}},
"route": {"type": "route.expr", "inputs": {"in": "group"},
"params": {"routes": {"hi": "labels.rate >= 0.5"}, "default": "lo"}},
"sink": {"type": "sink.jsonl", "inputs": {"in": "route.hi"}, "params": {"path": "out/hi.jsonl",
"fields": ["key"]}},
}}, sort_keys=False))
return root / "graph.yaml"
def run(graph: Path) -> tuple[float, dict]:
from pipewright import ops
t = time.perf_counter()
res = ops.run_graph(graph)
dt = time.perf_counter() - t
return dt, res
def executions(res: dict) -> int:
return sum(sum(v for k, v in n["events"].items() if k in ("ok",) or k.startswith("error"))
for n in res["funnel"]["nodes"].values())
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("-n", type=int, default=10000)
ap.add_argument("--profile", action="store_true")
ap.add_argument("--dir")
a = ap.parse_args()
root = Path(a.dir or tempfile.mkdtemp(prefix="pw-bench-"))
root.mkdir(parents=True, exist_ok=True)
graph = build(a.n, root)
prof = cProfile.Profile() if a.profile else None
if prof:
prof.enable()
cold, res = run(graph)
if prof:
prof.disable()
ex = executions(res)
warm, res2 = run(graph)
out = {"items": a.n, "node_executions": ex, "status": res["status"],
"cold_s": round(cold, 2), "cold_items_per_s": round(a.n / cold, 1),
"cold_us_per_execution": round(cold / ex * 1e6, 1),
"replay_s": round(warm, 2), "replay_items_per_s": round(a.n / warm, 1),
"replay_us_per_execution": round(warm / max(1, executions(res2)) * 1e6, 1),
"store_mb": round(sum(p.stat().st_size for p in (root / ".pipewright").glob("index.sqlite*")) / 1e6, 1)}
print(json.dumps(out, indent=2))
if prof:
pstats.Stats(prof).sort_stats("cumulative").print_stats(35)
if __name__ == "__main__":
main()