"""Decode speed of one vLLM server at concurrency c, streamed, greedy; session J's client. Used by scripts/gx10/ripple-65b/session-j.sh for content/standalone/06-serving-in-production/13-ripple-65b-serving. Runs with the session venv's Python (openai is a vLLM dependency). Per request: time to first token, and the decode rate (completion tokens - 1) / (last token time - first token time). Per run: the median of those rates, the aggregate output rate (all completion tokens / wall time from the first request sent to the last token), failed requests, and, from /metrics before and after, the speculative counters: mean acceptance length = 1 + accepted / drafts (the bonus token included), as vLLM's own log reports it. python -I ripple_bench.py --port 8132 --prompts short.json --concurrency 4 --num 8 --max-tokens 128 --out r.json """ import argparse import asyncio import json import re import statistics import time import urllib.request from openai import AsyncOpenAI SPEC = ("vllm:spec_decode_num_drafts_total", "vllm:spec_decode_num_draft_tokens_total", "vllm:spec_decode_num_accepted_tokens_total", "vllm:generation_tokens_total", "vllm:num_preemptions_total") def metrics(port): try: txt = urllib.request.urlopen(f"http://localhost:{port}/metrics", timeout=5).read().decode() except Exception: return {} out = {} for name in SPEC: out[name] = sum(float(v) for v in re.findall(rf"^{re.escape(name)}(?:{{[^}}]*}})? ([0-9.e+]+)$", txt, re.M)) pos = re.findall(r'^vllm:spec_decode_num_accepted_tokens_per_pos_total{[^}]*position="(\d+)"[^}]*} ([0-9.e+]+)$', txt, re.M) out["per_pos"] = {int(p): float(v) for p, v in pos} return out async def one(client, model, p, max_tokens, t_start): t0 = time.perf_counter() first = last = None usage = None text = [] try: s = await client.chat.completions.create( model=model, messages=[{"role": "user", "content": p["prompt"]}], max_tokens=max_tokens, temperature=0, stream=True, stream_options={"include_usage": True}) async for ch in s: if ch.usage: usage = ch.usage if ch.choices and (ch.choices[0].delta.content or getattr(ch.choices[0].delta, "reasoning_content", None)): now = time.perf_counter() first = first or now last = now text.append(ch.choices[0].delta.content or "") except Exception as e: # counted as failed; the session discards the run return {"id": p["id"], "error": repr(e)[:300]} if usage is None or first is None: return {"id": p["id"], "error": "no usage or no token"} n = usage.completion_tokens rate = (n - 1) / (last - first) if n > 1 and last > first else None return {"id": p["id"], "prompt_tokens": usage.prompt_tokens, "completion_tokens": n, "ttft_s": first - t0, "decode_tok_s": rate, "sent_s": t0 - t_start, "end_s": last - t_start, "text": "".join(text)} async def main(): ap = argparse.ArgumentParser() ap.add_argument("--port", type=int, required=True) ap.add_argument("--model", default="ripple") ap.add_argument("--prompts", required=True, help="short.json (a list) or long-.json (one prompt)") ap.add_argument("--concurrency", type=int, default=1) ap.add_argument("--num", type=int, default=1) ap.add_argument("--offset", type=int, default=0, help="first prompt index (different prompts per run)") ap.add_argument("--max-tokens", type=int, default=128) ap.add_argument("--out", required=True) a = ap.parse_args() data = json.load(open(a.prompts)) data = data if isinstance(data, list) else [data] prompts = [data[(a.offset + i) % len(data)] for i in range(a.num)] client = AsyncOpenAI(base_url=f"http://localhost:{a.port}/v1", api_key="x", timeout=3600, max_retries=0) sem = asyncio.Semaphore(a.concurrency) m0 = metrics(a.port) t_start = time.perf_counter() async def guarded(p): async with sem: return await one(client, a.model, p, a.max_tokens, t_start) res = await asyncio.gather(*(guarded(p) for p in prompts)) wall = time.perf_counter() - t_start m1 = metrics(a.port) ok = [r for r in res if "error" not in r] rates = [r["decode_tok_s"] for r in ok if r["decode_tok_s"]] d = {k: m1.get(k, 0) - m0.get(k, 0) for k in SPEC} summary = { "concurrency": a.concurrency, "num": a.num, "completed": len(ok), "failed": len(res) - len(ok), "max_tokens": a.max_tokens, "wall_s": wall, "prompt_tokens_mean": statistics.mean(r["prompt_tokens"] for r in ok) if ok else None, "completion_tokens": sum(r["completion_tokens"] for r in ok), "decode_tok_s_median": statistics.median(rates) if rates else None, "decode_tok_s_min": min(rates) if rates else None, "decode_tok_s_max": max(rates) if rates else None, "ttft_s_median": statistics.median(r["ttft_s"] for r in ok) if ok else None, "output_tok_s_aggregate": sum(r["completion_tokens"] for r in ok) / max(1e-9, max((r["end_s"] for r in ok), default=0)), "spec_drafts": d[SPEC[0]], "spec_draft_tokens": d[SPEC[1]], "spec_accepted": d[SPEC[2]], "mean_acceptance_length": 1 + d[SPEC[2]] / d[SPEC[0]] if d[SPEC[0]] else None, "acceptance_per_pos": {p: (m1.get("per_pos", {}).get(p, 0) - m0.get("per_pos", {}).get(p, 0)) / d[SPEC[0]] for p in sorted(m1.get("per_pos", {}))} if d[SPEC[0]] else None, "preemptions": d[SPEC[4]], } json.dump({"summary": summary, "requests": res}, open(a.out, "w"), indent=1) s = summary print(f"c={s['concurrency']} n={s['num']} ok={s['completed']} failed={s['failed']} " f"decode median {s['decode_tok_s_median'] or 0:.2f} tok/s (min {s['decode_tok_s_min'] or 0:.2f}), " f"aggregate {s['output_tok_s_aggregate']:.2f} tok/s, TTFT median {s['ttft_s_median'] or 0:.1f} s, " f"prompt {s['prompt_tokens_mean'] or 0:.0f} tok, acceptance {s['mean_acceptance_length'] or 0:.2f}, " f"preemptions {s['preemptions']:.0f}") if __name__ == "__main__": asyncio.run(main())