Skip to content

Commit e94d2ea

Browse files
David Engelhartclaude
andcommitted
lumopolis: the typing e2e splits each key's echo into page to main, main to the shell and back, and main to the page's screen, stamped in main as the letter arrives and as its terminal data leaves, so a slow echo on macOS or Linux shows which leg is slow
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1736fba commit e94d2ea

1 file changed

Lines changed: 43 additions & 6 deletions

File tree

‎e2e/world-view.spec.ts‎

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,27 @@ test.describe.serial('Lumopolis in the real app', () => {
388388
// the pty through main (Windows types straight to the pty host), so the report shows whether main's event
389389
// loop stalls during a compile (a 10 ms timer's lateness) or the machine is short of CPU (Electron's own
390390
// per-process CPU use, sampled every 250 ms). Times are epoch ms, as the renderer's timeOrigin + now().
391-
await app.evaluate(({ app: eapp }) => {
392-
const s = { lag: [] as number[][], cpu: [] as { t: number; p: { k: string; c: number }[] }[], stop: () => {} }
391+
// It also stamps, in main, each typed letter's arrival (a second terminal:write listener, run after the real
392+
// one) and each terminal:data message as it leaves for the page, so every key's echo splits into page to main,
393+
// main to the shell and back (the pty host and the shell), and main to the page's screen.
394+
await app.evaluate(({ app: eapp, ipcMain, BrowserWindow }) => {
395+
const s = { lag: [] as number[][], cpu: [] as { t: number; p: { k: string; c: number }[] }[], writes: [] as number[], out: [] as [number, string][], stop: () => {} }
393396
;(globalThis as unknown as { __mainDiag: typeof s }).__mainDiag = s
397+
const onWrite = (_e: unknown, p: { data?: unknown }): void => { if (typeof p?.data === 'string' && /^[a-z]$/.test(p.data)) s.writes.push(Date.now()) }
398+
ipcMain.on('terminal:write', onWrite)
399+
const wc = BrowserWindow.getAllWindows()[0].webContents
400+
const send = wc.send
401+
wc.send = function (channel: string, ...args: unknown[]) {
402+
if (channel === 'terminal:data' && typeof args[1] === 'string') s.out.push([Date.now(), args[1]])
403+
return send.call(this, channel, ...args)
404+
}
394405
let last = Date.now()
395406
const lagTimer = setInterval(() => { const now = Date.now(); s.lag.push([now, now - last - 10]); last = now }, 10)
396407
eapp.getAppMetrics()
397408
const cpuTimer = setInterval(() => {
398409
s.cpu.push({ t: Date.now(), p: eapp.getAppMetrics().map((m) => ({ k: m.name ? `${m.type}:${m.name}` : m.type, c: m.cpu.percentCPUUsage })) })
399410
}, 250)
400-
s.stop = () => { clearInterval(lagTimer); clearInterval(cpuTimer) }
411+
s.stop = () => { clearInterval(lagTimer); clearInterval(cpuTimer); ipcMain.removeListener('terminal:write', onWrite); wc.send = send }
401412
})
402413

403414
// The load: main builds the bigger project's code graph, then its utility process compiles it.
@@ -448,11 +459,12 @@ test.describe.serial('Lumopolis in the real app', () => {
448459
return { down: s.down, echo: s.echo, compiling: s.compiling, runs: s.runs, compileError: s.compileError }
449460
})
450461
const diag = await app.evaluate(() => {
451-
const s = (globalThis as unknown as { __mainDiag: { lag: number[][]; cpu: { t: number; p: { k: string; c: number }[] }[]; stop: () => void } }).__mainDiag
462+
const s = (globalThis as unknown as { __mainDiag: { lag: number[][]; cpu: { t: number; p: { k: string; c: number }[] }[]; writes: number[]; out: [number, string][]; stop: () => void } }).__mainDiag
452463
s.stop()
453-
return { lag: s.lag, cpu: s.cpu }
464+
return { lag: s.lag, cpu: s.cpu, writes: s.writes, out: s.out }
454465
})
455-
const { origin, cores } = await page.evaluate(() => ({ origin: performance.timeOrigin, cores: navigator.hardwareConcurrency }))
466+
// The page's clock as epoch ms: timeOrigin + now(), corrected by its offset from Date.now() at the end.
467+
const { origin, cores } = await page.evaluate(() => ({ origin: Date.now() - performance.now(), cores: navigator.hardwareConcurrency }))
456468
const inRun = (t: number): boolean => r.runs.some((w) => t >= origin + w.start && t < origin + w.end)
457469
const lagOf = (xs: number[]): string => {
458470
if (!xs.length) return 'none'
@@ -467,6 +479,31 @@ test.describe.serial('Lumopolis in the real app', () => {
467479
const top = Object.entries(sum).sort((a, b) => b[1] - a[1]).slice(0, 4).map(([k, c]) => `${k} ${(c / Math.max(1, rows.length)).toFixed(0)}%`).join(', ')
468480
return `${rows.length} samples, total ${total.toFixed(0)}% (${top})`
469481
}
482+
// Each key pressed after the hooks went in, in order, to the next letter main received, then to the first
483+
// terminal:data after that write that holds a letter, then to the page seeing its echo.
484+
const stages: { during: boolean; t1: number; t2: number; t3: number }[] = []
485+
{
486+
let wi = 0
487+
let oi = 0
488+
const first = diag.writes.length ? diag.writes[0] - 50 : Infinity
489+
r.down.forEach((d, i) => {
490+
const D = origin + d
491+
if (D < first || !r.echo[i]) return
492+
while (wi < diag.writes.length && diag.writes[wi] < D - 2) wi++
493+
if (wi >= diag.writes.length) return
494+
const W = diag.writes[wi++]
495+
while (oi < diag.out.length && (diag.out[oi][0] < W || !/[a-z]/.test(diag.out[oi][1]))) oi++
496+
if (oi >= diag.out.length) return
497+
const S = diag.out[oi][0]
498+
stages.push({ during: r.runs.some((w) => d >= w.start && d < w.end), t1: W - D, t2: S - W, t3: origin + r.echo[i] - S })
499+
})
500+
}
501+
const stageOf = (during: boolean): string => {
502+
const xs = stages.filter((x) => x.during === during)
503+
const q = (k: 't1' | 't2' | 't3'): string => { const v = xs.map((x) => x[k]).sort((a, b) => a - b); return v.length ? `p50 ${v[Math.floor(v.length / 2)].toFixed(0)} p95 ${v[Math.min(v.length - 1, Math.ceil(v.length * 0.95) - 1)].toFixed(0)}` : '-' }
504+
return `${xs.length} keys: page to main ${q('t1')}, main to shell and back ${q('t2')}, main to the page's screen ${q('t3')} ms`
505+
}
506+
console.log(`[typing while compiling, stages] during compiles ${stageOf(true)}; with none running ${stageOf(false)}`)
470507
console.log(`[typing while compiling, main] ${cores} cores; main timer lateness during compiles ${lagOf(diag.lag.filter((x) => inRun(x[0])).map((x) => x[1]))}, with none running ${lagOf(diag.lag.filter((x) => !inRun(x[0])).map((x) => x[1]))}; Electron CPU during compiles ${cpuOf(true)}; with none running ${cpuOf(false)}`)
471508
expect(r.compiling, 'the compiles were still running after 60 s of typing').toBe(false)
472509
expect(r.compileError ?? null).toBeNull()

0 commit comments

Comments
 (0)