"""Synthetic throughput benchmark for the batch status/checkpoint layer.""" from __future__ import annotations import argparse import json from pathlib import Path import statistics import tempfile import time from cbb_refresh_verify.batch_status import BatchStatusSession from cbb_refresh_verify.status_bridge import BridgeResult class SyntheticBridge: def __init__(self, rotate_every: int): self.rotate_every = rotate_every self.restart_count = 0 self.latencies_ms: list[float] = [] def start(self): return None def restart(self): self.restart_count += 1 def close(self): return None def query(self, lmc_file, block_name, **kwargs): started = time.perf_counter() result = BridgeResult("READY" if int(block_name[3:]) % 3 == 0 else "PENDING", "synthetic") self.latencies_ms.append((time.perf_counter() - started) * 1000) return result def main() -> int: parser = argparse.ArgumentParser() parser.add_argument("--count", type=int, required=True) parser.add_argument("--rotate-every", type=int, default=500) parser.add_argument("--output", type=Path, required=True) args = parser.parse_args() if args.count < 1: raise SystemExit("--count must be positive") with tempfile.TemporaryDirectory(prefix="cbb-status-benchmark-") as root: root_path = Path(root) lmc = root_path / "library.lmc"; lmc.write_bytes(b"synthetic-library") bridge = SyntheticBridge(args.rotate_every) started = time.perf_counter() records = BatchStatusSession(bridge, rotate_every=args.rotate_every).scan( [f"CBB{i}" for i in range(args.count)], lmc, root_path / "scan.jsonl") elapsed = time.perf_counter() - started values = bridge.latencies_ms p95 = sorted(values)[max(0, int(len(values) * 0.95) - 1)] if values else 0 payload = { "count": args.count, "elapsed_seconds": elapsed, "completed_count": len(records), "error_count": sum(record.status not in {"READY", "PENDING"} for record in records), "mean_latency_ms": statistics.mean(values) if values else 0, "p95_latency_ms": p95, "bridge_restart_count": bridge.restart_count, "synthetic": True, } args.output.parent.mkdir(parents=True, exist_ok=True) args.output.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") print(json.dumps(payload, ensure_ascii=False)) return 0 if __name__ == "__main__": raise SystemExit(main())