#!/usr/bin/env python3 """localmaxxing benchmark harness for the Bonsai2-CRACK + DFlash2 deployment. Measures, per run: engine tok/s (from the server's own timings), HTTP tok/s, TTFT, prefill rate, spec-decode acceptance, plus sampled GPU power/VRAM. """ import json, subprocess, sys, threading, time, urllib.request URL = "http://127.0.0.1:8001/v1/chat/completions" POWER_FILE = "/root/pkg/results/power_samples.txt" def sample_gpu(stop, out): while not stop.is_set(): try: r = subprocess.run(["nvidia-smi", "--query-gpu=power.draw,memory.used", "--format=csv,noheader,nounits"], capture_output=True, text=True, timeout=3) out.append(r.stdout.strip()) except Exception: pass time.sleep(0.4) def run_once(body, tag): body = dict(body) body["stream"] = True data = json.dumps(body).encode() req = urllib.request.Request(URL, data=data, headers={"Content-Type": "application/json"}) samples = [] stop = threading.Event() th = threading.Thread(target=sample_gpu, args=(stop, samples), daemon=True) th.start() t0 = time.time() ttft = None n_chunks = 0 text_len = 0 timings = None finish = None usage = None t_first_char = None with urllib.request.urlopen(req, timeout=600) as resp: buf = b"" while True: b = resp.read(1) if not b: break if b == b"\n": line = buf.decode(errors="replace").strip() buf = b"" if not line.startswith("data:"): continue payload = line[5:].strip() if payload == "[DONE]": break try: j = json.loads(payload) except Exception: continue if "timings" in j: timings = j["timings"] if "usage" in j: usage = j["usage"] for ch in j.get("choices", []): d = ch.get("delta", {}) if d.get("content"): if t_first_char is None: t_first_char = time.time() text_len += len(d["content"]) n_chunks += 1 if ch.get("finish_reason"): finish = ch["finish_reason"] else: buf += b t_end = time.time() stop.set(); th.join(timeout=2) wall = t_end - t0 res = { "tag": tag, "wall_s": round(wall, 3), "ttft_ms": round((t_first_char - t0) * 1000, 1) if t_first_char else None, "engine_tok_s": timings["predicted_per_second"] if timings else None, "http_tok_s": round((usage["completion_tokens"] / wall), 2) if usage else None, "prefill_tok_s": round(timings["prompt_per_second"], 1) if timings else None, "prompt_tokens": timings["prompt_n"] if timings else (usage or {}).get("prompt_tokens"), "output_tokens": timings["predicted_n"] if timings else (usage or {}).get("completion_tokens"), "draft_n": timings.get("draft_n") if timings else None, "draft_accepted": timings.get("draft_n_accepted") if timings else None, "acceptance": round(timings["draft_n_accepted"] / timings["draft_n"], 4) if timings and timings.get("draft_n") else None, "finish": finish, "gpu_samples": samples, } return res def main(): probe_file = sys.argv[1] n_runs = int(sys.argv[2]) if len(sys.argv) > 2 else 1 with open(probe_file) as f: body = json.load(f) results = [] for i in range(n_runs): r = run_once(body, f"{probe_file.split('/')[-1]}#{i+1}") results.append(r) print(json.dumps({k: v for k, v in r.items() if k != "gpu_samples"})) time.sleep(2) import os os.makedirs("/root/pkg/results", exist_ok=True) out = f"/root/pkg/results/bench_{probe_file.split('/')[-1].replace('.json','')}.json" with open(out, "w") as f: json.dump(results, f, indent=2) print("saved:", out) if __name__ == "__main__": main()