// Times one Kokoro generation on the phone for a given dtype/threads, in a throwaway worker. import { chromium } from '@playwright/test'; const [dtype, threads] = [process.argv[2], Number(process.argv[3] || 4)]; const b = await chromium.connectOverCDP('http://localhost:9333'); const p = b.contexts()[0].pages().find((x) => x.url().startsWith('http://localhost:4175')); if (process.env.TRACE) { const cdp = await b.contexts()[0].newCDPSession(p); await cdp.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: false, flatten: true }); p.on('worker', (w) => console.log('worker', w.url().slice(-40))); b.contexts()[0].on('requestfailed', (r) => console.log('FAILED', r.url().slice(0, 120), r.failure()?.errorText)); b.contexts()[0].on('response', (r) => { if (/models|onnx|\.bin|json/.test(r.url())) console.log('RESP', r.status(), r.url().slice(0, 120)); }); } const workerUrl = await p.evaluate(async () => { const idx = performance.getEntriesByType('resource').map((r) => r.name).find((n) => /assets\/index-.*\.js$/.test(n)); const src = await (await fetch(idx)).text(); return new URL('assets/' + src.match(/tts-worker-[\w-]+\.js/)[0], location.href).href; }); const r = await p.evaluate(async ({ workerUrl, dtype, threads }) => { const w = new Worker(workerUrl, { type: 'module' }); const t0 = performance.now(); const res = await new Promise((resolve) => { const runs = []; w.onmessage = (e) => { const m = e.data; if (m.type === 'ready') { runs.push({ loadMs: Math.round(performance.now() - t0) }); w.postMessage({ type: 'gen', id: 1, text: 'She had been told never to come here at night.', voice: 'af_heart' }); } else if (m.type === 'audio') { runs.push({ genMs: m.ms, audioMs: Math.round((m.pcm.length / m.rate) * 1000) }); if (m.id === 1) w.postMessage({ type: 'gen', id: 2, text: 'But tonight, the lamp was lit, and the wind was calm.', voice: 'af_heart' }); else resolve(runs); } else if (m.type === 'loadError' || m.type === 'error') resolve({ err: m.message }); }; w.postMessage({ type: 'load', device: 'wasm', dtype, threads, ortBase: new URL('ort/', location.href).href, modelBase: new URL('models/kokoro/', location.href).href }); }); w.terminate(); return res; }, { workerUrl, dtype, threads }); console.log(dtype, threads, JSON.stringify(r)); await b.close().catch(() => {});