"""Render the captured browser measurements: python3 render-measurements.py measurements.jsonl output.png"""
import json, statistics, sys
from PIL import Image, ImageDraw, ImageFont
rows=[json.loads(s) for s in open(sys.argv[1])]
shas=list(dict.fromkeys(r['sha'] for r in rows))
values=[[r['milliseconds'] for r in rows if r['sha']==sha] for sha in shas]
font='/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'
def f(size): return ImageFont.truetype(font,size)
im=Image.new('RGB',(1280,900),'#f8fafc'); d=ImageDraw.Draw(im)
d.text((48,32),'Connect to interact → editable thread',font=f(32),fill='#0f172a')
d.text((48,82),'Linux x64 · Chromium · 1280×800 · fresh synthetic state on every run',font=f(20),fill='#475569')
d.text((48,116),'Preferred address reachable; fallback discovery request held until client abort.',font=f(20),fill='#475569')
for i,(sha,v) in enumerate(zip(shas,values)):
 x=48+i*620; color=['#c2410c','#047857'][i]
 d.text((x,180),['BEFORE · BASE','AFTER · HEAD'][i],font=f(22),fill=color)
 d.text((x,216),sha[:10],font=f(20),fill='#475569')
 d.text((x,258),f'{statistics.median(v)/1000:.3f} s median',font=f(35),fill=color)
 d.rectangle((x,322,x+500*statistics.median(v)/8000,350),fill=color)
 for j,n in enumerate(v): d.text((x,386+j*43),f'Run {j+1}     {n:,.1f} ms',font=f(23),fill='#0f172a')
 d.text((x,642),f'Range: {min(v):,.1f}–{max(v):,.1f} ms',font=f(21),fill='#475569')
 d.text((x,678),f'Spread: {max(v)-min(v):.1f} ms',font=f(21),fill='#475569')
delta=statistics.median(values[0])-statistics.median(values[1])
d.text((48,747),f'Median improvement: {delta:,.1f} ms ({100*delta/statistics.median(values[0]):.1f}%)',font=f(28),fill='#0f172a')
d.text((48,801),'All runs opened the expected thread and accepted the same unsent draft.',font=f(22),fill='#0f172a')
d.text((48,842),'Measured from DOM click to new thread route + visible editable composer; 5 ms sampling.',font=f(19),fill='#475569')
im.save(sys.argv[2])
