"""load client for any OpenAI-shaped POST /v1/audio/speech (ours, and the bentoml / litserve / ray serve / ... adapters, which speak the same). single: 20 sequential requests (bench texts 1-20), streamed 16-bit 24 kHz PCM -> first byte p50, x real time (audio s / request s) p50. parallel: 10 clients, 60 requests closed-loop -> requests/s, audio s per s, first byte p50/p90. usage: python server_bench.py [--wav] [--token T] [--model kale-tts-1-nano] [--voice kenzie] [--path /v1/audio/speech] [--k 10]""" import argparse, json, statistics as S, threading, time, urllib.request from pathlib import Path ap = argparse.ArgumentParser(); ap.add_argument("base"); ap.add_argument("out"); ap.add_argument("--wav", action="store_true"); ap.add_argument("--token") ap.add_argument("--model", default="kale-tts-1-nano"); ap.add_argument("--path", default="/v1/audio/speech"); ap.add_argument("--k", type=int, default=10) ap.add_argument("--n", type=int, default=60); ap.add_argument("--label", default=""); ap.add_argument("--voice", default="kenzie") a = ap.parse_args() texts = [json.loads(l)["text"] for l in open(Path(__file__).with_name("lines80.jsonl"))] def call(text): body = json.dumps(dict(model=a.model, input=text, voice=a.voice, response_format="wav" if a.wav else "pcm", stream=not a.wav)).encode() h = {"content-type": "application/json"} | ({"authorization": f"Bearer {a.token}"} if a.token else {}) t = time.perf_counter() with urllib.request.urlopen(urllib.request.Request(a.base.rstrip("/") + a.path, data=body, headers=h), timeout=300) as r: first = r.read(1); tf = time.perf_counter() - t; rest = r.read(); tt = time.perf_counter() - t audio = (len(first) + len(rest) - (44 if a.wav else 0)) / 2 / 24000 return dict(first_byte_s=tf, total_s=tt, audio_s=audio) for x in texts[:3]: call(x) # warm-up seq = [call(x) for x in texts[:20]] res = dict(label=a.label, base=a.base, model=a.model, format="wav" if a.wav else "pcm stream", single=dict(n=len(seq), first_byte_p50_s=round(S.median(r["first_byte_s"] for r in seq), 3), xrt_p50=round(S.median(r["audio_s"] / r["total_s"] for r in seq), 1), audio_p50_s=round(S.median(r["audio_s"] for r in seq), 2))) rec, lock, idx = [], threading.Lock(), [0] def w(): while True: with lock: if idx[0] >= a.n: return i = idx[0]; idx[0] += 1 r = call(texts[i % len(texts)]) with lock: rec.append(r) t = time.perf_counter(); th = [threading.Thread(target=w) for _ in range(a.k)]; [x.start() for x in th]; [x.join() for x in th]; wall = time.perf_counter() - t fb = sorted(r["first_byte_s"] for r in rec) res[f"parallel_{a.k}"] = dict(requests=len(rec), req_per_s=round(len(rec) / wall, 2), audio_s_per_s=round(sum(r["audio_s"] for r in rec) / wall, 1), first_byte_p50_s=round(fb[len(fb) // 2], 3), first_byte_p90_s=round(fb[int(len(fb) * 0.9)], 3)) Path(a.out).write_text(json.dumps(res, indent=1)); print(json.dumps(res))