import json, time, urllib.request

def call(messages, max_tokens=300, label=""):
    req = urllib.request.Request("http://localhost:8080/v1/chat/completions",
        data=json.dumps({"model":"Qwen3.8-27B-Uncensored-HauhauCS-Aggressive-Q4_K_P.gguf",
            "messages":messages,"max_tokens":max_tokens,"stream":True}).encode(),
        headers={"Content-Type":"application/json"})
    t0=time.time(); ttf=None; n_gen=0; n_reason=0
    with urllib.request.urlopen(req) as r:
        for line in r:
            line=line.decode().strip()
            if not line.startswith("data: ") or line[6:]=="[DONE]": continue
            d=json.loads(line[6:])
            delta=d["choices"][0].get("delta",{})
            c=delta.get("content"); rc=delta.get("reasoning_content")
            if rc: n_reason+=1
            if c: n_gen+=1
            if (c or rc) and ttf is None: ttf=time.time()-t0
    tot=time.time()-t0
    n=n_gen+n_reason
    print(f"{label}: ttft={ttf:.2f}s total={tot:.2f}s out={n} (gen={n_gen} reason={n_reason}) speed={n/max(tot-ttf,0.01):.1f} t/s")

long_ctx = "背景资料。" + ("人工智能是计算机科学的一个重要分支。" * 700)
msgs1=[{"role":"user","content":long_ctx+" 总结50字。"}]
msgs2=[{"role":"system","content":"你是一个助手。"},{"role":"user","content":"你好，1+1等于几？简短回答。"}]

call(msgs2, label="[short A]")
call(msgs2, label="[short B (repeat)]")
call(msgs1, label="[8k ctx 1st]")
call(msgs1, label="[8k ctx 2nd (prefix cache)]")
