import measure as m
original_run=m.orig_run;original_synth=m.p.PocketDaemon.synthesize
import stream_experiment
stream_run=m.orig_run;stream_synth=m.p.PocketDaemon.synthesize
import asyncio,json,time,wave
from pathlib import Path
async def main():
 rows=[];done=asyncio.Event()
 def out(e):
  if e['type']=='synthesis-audio' and not any(x['type']=='remote-first' for x in m.events):m.events.append({'type':'remote-first','ms':(time.perf_counter()-m.t0)*1000})
  if e['type']=='synthesis-result':done.set()
 rt=m.Runtime(m.ROOT/'parakeet',pocket_root=m.ROOT/'pocket',output=out)
 try:
  await rt._prepare_speech(remote=True)
  for i in range(5):
   for mode in (('original','stream') if i%2==0 else ('stream','original')):
    m.orig_run=original_run if mode=='original' else stream_run
    m.p.PocketDaemon.synthesize=original_synth if mode=='original' else stream_synth
    m.events=[];done.clear();m.t0=time.perf_counter()
    await rt.command({'type':'synthesis-start','requestId':f'{mode}-{i}','synthesisId':f'{mode}-{i}','text':m.texts['typical']});await asyncio.wait_for(done.wait(),30)
    row={'mode':mode,'trial':i,'events':m.events.copy()};rows.append(row);print(json.dumps({'mode':mode,'trial':i,'times':{e['type']:round(e['ms'],2) for e in m.events if e['type']!='chunk'}}),flush=True)
 finally:
  await rt.command({'type':'shutdown','requestId':'bye'});Path('/tmp/jarvis-pr8-perf-results/ab.json').write_text(json.dumps(rows,indent=2))
asyncio.run(main())
