import json,time,urllib.request
from pathlib import Path
root=Path('/home/lab/bench/ornith/modular/sglang')
rows=[]
for rep in range(3):
    data={'model':'ornith-sglang','messages':[{'role':'user','content':'Write a Python function merge_sorted(a, b) that merges two sorted lists. Include a short explanation.'}],'max_tokens':256,'temperature':0,'stream':True,'stream_options':{'include_usage':True},'chat_template_kwargs':{'enable_thinking':False}}
    start=time.monotonic(); first=None; text=''; usage=None
    req=urllib.request.Request('http://127.0.0.1:8130/v1/chat/completions',data=json.dumps(data).encode(),headers={'Content-Type':'application/json'})
    with urllib.request.urlopen(req,timeout=180) as resp:
        for line in resp:
            if not line.startswith(b'data: '):continue
            raw=line[6:].strip()
            if raw==b'[DONE]':break
            item=json.loads(raw)
            if item.get('usage'):usage=item['usage']
            for ch in item.get('choices',[]):
                token=ch.get('delta',{}).get('content') or ''
                if token:
                    if first is None:first=time.monotonic()
                    text+=token
    end=time.monotonic()
    row={'repeat':rep,'warmup':rep==0,'elapsed_s':end-start,'ttft_s':None if first is None else first-start,'decode_tokens_per_s':None if first is None or not usage else (usage['completion_tokens']-1)/(end-first),'usage':usage,'content':text}
    assert text and usage
    rows.append(row);print(json.dumps(row),flush=True)
(root/'baseline-stream.json').write_text(json.dumps(rows,indent=2))
