import json, pathlib, shutil, time, urllib.request, subprocess
root = pathlib.Path('/home/lab/bench/ornith/modular/vllm')
root.mkdir(parents=True, exist_ok=True)
dst=root/'baseline'
if dst.exists():
    raise SystemExit('Baseline snapshot already exists; refusing overwrite')
shutil.copytree('/home/lab/bench/ornith/vllm', dst)
subprocess.run(['/home/lab/venvs/vllm/bin/pip','freeze'],stdout=(dst/'freeze.txt').open('w'),check=True)
body={'model':'ornith-vllm','messages':[{'role':'user','content':'Explica en espanol que hace una tabla hash, en unas 100 palabras.'}], 'temperature':0, 'max_tokens':256,'stream':True,'stream_options':{'include_usage':True},'chat_template_kwargs':{'enable_thinking':False}}
start=time.monotonic(); first=None; text=''; usage={}
request=urllib.request.Request('http://127.0.0.1:8140/v1/chat/completions',data=json.dumps(body).encode(),headers={'Content-Type':'application/json'})
with urllib.request.urlopen(request,timeout=180) as response:
    for line in response:
        if not line.startswith(b'data: ') or line.strip()==b'data: [DONE]':continue
        event=json.loads(line[6:]); usage=event.get('usage') or usage
        for choice in event.get('choices',[]):
            content=choice.get('delta',{}).get('content') or ''
            if content and first is None:first=time.monotonic()
            text+=content
elapsed=time.monotonic()-start
result={'ttft_s':None if first is None else first-start,'wall_s':elapsed,'usage':usage,'text':text,'decode_tok_s':usage.get('completion_tokens',0)/max(.001,elapsed-(first-start)) if first else None}
(dst/'streaming.json').write_text(json.dumps(result,indent=2,ensure_ascii=False))
print(json.dumps(result,indent=2,ensure_ascii=False))
assert text and first
