# Qwen4-35B-A3B long-prompt benchmark, paraloncloud.com, 2026-12-04.
# Usage: URL=http://host:8000/v1/chat/completions python3 long_prompt.py 8000,32000,128000
# The argument is a size hint; the prompt comes out about 1.5x longer in tokens (printed).
import json,time,urllib.request,sys,random
import os; sizes=[int(x) for x in sys.argv[1].split(",")]
URL=os.environ.get("URL","http://localhost:8000/v1/chat/completions")
words="memory bandwidth tensor kernel cache latency scheduler register warp block shared global copy stream device host page table fabric link lane".split()
for target in sizes:
    rnd=random.Random(target+int(time.time()))
    # ~1.25 tokens per item "word123"
    n=int(target/2.6)
    text=" ".join(f"{rnd.choice(words)}{rnd.randint(0,999)}" for _ in range(n))
    body={"model":"Qwen/Qwen4-35B-A3B-FP8","stream":True,"stream_options":{"include_usage":True},"max_tokens":300,"min_tokens":300,"ignore_eos":True,
      "messages":[{"role":"user","content":f"nonce {time.time()}\n{text}\n\nSummarize the list above."}]}
    t0=time.time(); first=None; usage=None
    r=urllib.request.urlopen(urllib.request.Request(URL,json.dumps(body).encode(),{'Content-Type':'application/json'}),timeout=900)
    for line in r:
        line=line.decode().strip()
        if not line.startswith('data:') or line=='data: [DONE]': continue
        d=json.loads(line[5:])
        if d.get('usage'): usage=d['usage']
        for c in d.get('choices',[]):
            dl=c.get('delta',{})
            if (dl.get('content') or dl.get('reasoning_content') or dl.get('reasoning')) and first is None: first=time.time()
    t1=time.time(); p=usage['prompt_tokens']; o=usage['completion_tokens']
    print(f"prompt {p:>7} tokens: TTFT {first-t0:6.2f}s (prefill {p/(first-t0):6.0f} tok/s), then {o/(t1-first):4.0f} tok/s",flush=True)
