|
| 1 | +// Manual, read-only fixture measurement. Never sends an Accept request. |
| 2 | +import { spawn } from 'node:child_process'; |
| 3 | +import { resolve } from 'node:path'; |
| 4 | +import { setTimeout as delay } from 'node:timers/promises'; |
| 5 | +import { performance } from 'node:perf_hooks'; |
| 6 | + |
| 7 | +const [executable, app, expectedWord] = process.argv.slice(2); |
| 8 | +if (!executable || !app || !expectedWord) { |
| 9 | + console.error('Usage: node scripts/measure-word-prediction.mjs <executable> <app-label> <expected-word>'); |
| 10 | + process.exit(1); |
| 11 | +} |
| 12 | +console.log('Focus a synthetic test field now. Sampling starts in five seconds. No input will be injected.'); |
| 13 | +await delay(5000); |
| 14 | +const child = spawn(resolve(executable), [ |
| 15 | + '--switchify-prediction-worker', |
| 16 | + resolve('src-tauri/resources/WordData2017051601.db'), |
| 17 | + '[]', |
| 18 | +], { stdio: ['pipe', 'pipe', 'ignore'], windowsHide: true }); |
| 19 | +let buffer = Buffer.alloc(0); |
| 20 | +let pending; |
| 21 | +let failed = false; |
| 22 | +const fail = () => { |
| 23 | + failed = true; |
| 24 | + pending?.reject(new Error('Worker unavailable')); |
| 25 | + pending = undefined; |
| 26 | +}; |
| 27 | +child.on('error', fail); |
| 28 | +child.on('exit', fail); |
| 29 | +child.stdin.on('error', fail); |
| 30 | +child.stdout.on('data', chunk => { |
| 31 | + buffer = Buffer.concat([buffer, chunk]); |
| 32 | + if (buffer.length < 4) return; |
| 33 | + const length = buffer.readUInt32LE(); |
| 34 | + if (!length || length > 16384 || buffer.length > length + 4) return fail(); |
| 35 | + if (buffer.length !== length + 4) return; |
| 36 | + try { |
| 37 | + const response = JSON.parse(buffer.subarray(4).toString('utf8')); |
| 38 | + buffer = Buffer.alloc(0); |
| 39 | + const request = pending; |
| 40 | + pending = undefined; |
| 41 | + if (!request) return fail(); |
| 42 | + request.resolve(response); |
| 43 | + } catch { fail(); } |
| 44 | +}); |
| 45 | +async function query(generation) { |
| 46 | + if (failed) throw new Error('Worker unavailable'); |
| 47 | + const bytes = Buffer.from(JSON.stringify({ Query: { |
| 48 | + generation, edit: null, reset: true, shift: false, caps: false, |
| 49 | + } })); |
| 50 | + const header = Buffer.alloc(4); |
| 51 | + header.writeUInt32LE(bytes.length); |
| 52 | + let timer; |
| 53 | + try { |
| 54 | + return await new Promise((resolve, reject) => { |
| 55 | + pending = { resolve, reject }; |
| 56 | + timer = setTimeout(() => { fail(); child.kill(); }, 2000); |
| 57 | + child.stdin.write(Buffer.concat([header, bytes])); |
| 58 | + }); |
| 59 | + } finally { clearTimeout(timer); } |
| 60 | +} |
| 61 | +const stop = () => { fail(); child.kill(); }; |
| 62 | +process.on('SIGINT', stop); |
| 63 | +process.on('SIGTERM', stop); |
| 64 | +const times = []; |
| 65 | +let unsupported = 0; |
| 66 | +let failures = 0; |
| 67 | +let fixtureMatches = 0; |
| 68 | +let firstRequestMs; |
| 69 | +try { |
| 70 | + // Up to 200 attempts, stopping after 100 supported samples. |
| 71 | + for (let generation = 0; generation < 200 && times.length < 100; generation++) { |
| 72 | + const start = performance.now(); |
| 73 | + const response = await query(generation); |
| 74 | + const elapsed = performance.now() - start; |
| 75 | + if (generation === 0) firstRequestMs = elapsed; |
| 76 | + const result = response.Suggestions; |
| 77 | + if (!result || result.generation !== generation) throw new Error('Invalid response'); |
| 78 | + if (result.batch?.words?.length) { |
| 79 | + if (generation !== 0) times.push(elapsed); |
| 80 | + if (result.batch.words.some(word => word.toLowerCase() === expectedWord.toLowerCase())) fixtureMatches++; |
| 81 | + } else unsupported++; |
| 82 | + await delay(Math.max(0, 250 - elapsed)); |
| 83 | + } |
| 84 | +} catch { failures++; } |
| 85 | +finally { |
| 86 | + child.kill(); |
| 87 | + process.off('SIGINT', stop); |
| 88 | + process.off('SIGTERM', stop); |
| 89 | +} |
| 90 | +times.sort((a, b) => a - b); |
| 91 | +const percentile = fraction => times.length ? +times[Math.ceil(times.length * fraction) - 1].toFixed(2) : null; |
| 92 | +console.log(JSON.stringify({ |
| 93 | + app, successfulSamples: times.length, unsupported, failures, fixtureMatches, |
| 94 | + firstRequestMs: firstRequestMs === undefined ? null : +firstRequestMs.toFixed(2), |
| 95 | + medianMs: percentile(0.5), p95Ms: percentile(0.95), maximumMs: percentile(1), |
| 96 | +}, null, 2)); |
| 97 | +if (failures || times.length < 100) process.exitCode = 1; |
0 commit comments