Skip to content

Commit f945e40

Browse files
David Engelhartclaude
andcommitted
lumopolis: the typing e2e also reports how often the page paints during the compiles, how much of each long frame was blocking work, and how busy the whole machine is, so a slow echo on macOS shows whether the runner or the app is short of time
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c3b15d6 commit f945e40

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

‎e2e/world-view.spec.ts‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,13 @@ test.describe.serial('Lumopolis in the real app', () => {
414414
// And the page's side: its own 10 ms timer's lateness and its long tasks (50 ms or more of its own script or
415415
// layout), so a slow last leg shows whether the page's thread was busy with its own work or not run at all.
416416
await page.evaluate(() => {
417-
const s = { lag: [] as number[][], long: [] as number[][], frames: [] as { t: number; d: number; render: number; scripts: [string, number][] }[], stop: () => {} }
417+
const s = { lag: [] as number[][], long: [] as number[][], frames: [] as { t: number; d: number; render: number; blocking: number; scripts: [string, number][] }[], raf: [] as number[][], stop: () => {} }
418418
;(window as unknown as { __pageDiag: typeof s }).__pageDiag = s
419+
// The page's frames as requestAnimationFrame runs them: how often the page gets to paint.
420+
let lastRaf = performance.now()
421+
let rafId = 0
422+
const onRaf = (now: number): void => { s.raf.push([now, now - lastRaf]); lastRaf = now; rafId = requestAnimationFrame(onRaf) }
423+
rafId = requestAnimationFrame(onRaf)
419424
let last = performance.now()
420425
const timer = setInterval(() => { const now = performance.now(); s.lag.push([now, now - last - 10]); last = now }, 10)
421426
let obs: PerformanceObserver | null = null
@@ -428,13 +433,13 @@ test.describe.serial('Lumopolis in the real app', () => {
428433
let frameObs: PerformanceObserver | null = null
429434
try {
430435
frameObs = new PerformanceObserver((list) => {
431-
for (const e of list.getEntries() as unknown as { startTime: number; duration: number; renderStart: number; scripts?: { invoker?: string; sourceURL?: string; duration: number }[] }[]) {
432-
s.frames.push({ t: e.startTime, d: e.duration, render: e.renderStart ? e.startTime + e.duration - e.renderStart : 0, scripts: (e.scripts ?? []).map((x) => [`${x.invoker ?? '?'} ${(x.sourceURL ?? '').split('/').pop()}`, x.duration]) })
436+
for (const e of list.getEntries() as unknown as { startTime: number; duration: number; renderStart: number; blockingDuration?: number; scripts?: { invoker?: string; sourceURL?: string; duration: number }[] }[]) {
437+
s.frames.push({ t: e.startTime, d: e.duration, render: e.renderStart ? e.startTime + e.duration - e.renderStart : 0, blocking: e.blockingDuration ?? -1, scripts: (e.scripts ?? []).map((x) => [`${x.invoker ?? '?'} ${(x.sourceURL ?? '').split('/').pop()}`, x.duration]) })
433438
}
434439
})
435440
frameObs.observe({ type: 'long-animation-frame', buffered: false })
436441
} catch { frameObs = null }
437-
s.stop = () => { clearInterval(timer); obs?.disconnect(); frameObs?.disconnect() }
442+
s.stop = () => { clearInterval(timer); cancelAnimationFrame(rafId); obs?.disconnect(); frameObs?.disconnect() }
438443
})
439444
// It also stamps, in main, each typed letter's arrival (a second terminal:write listener, run after the real
440445
// one) and each terminal:data message as it leaves for the page, so every key's echo splits into page to main,
@@ -453,8 +458,17 @@ test.describe.serial('Lumopolis in the real app', () => {
453458
let last = Date.now()
454459
const lagTimer = setInterval(() => { const now = Date.now(); s.lag.push([now, now - last - 10]); last = now }, 10)
455460
eapp.getAppMetrics()
461+
// The whole machine's busy share (all processes, not only Electron's) from the cores' own counters.
462+
type Cpu = { times: { user: number; nice: number; sys: number; idle: number; irq: number } }
463+
const os = (process as unknown as { getBuiltinModule?: (m: string) => { cpus: () => Cpu[] } | undefined }).getBuiltinModule?.('os')
464+
const ticks = (): [number, number] => (os ? os.cpus() : []).reduce<[number, number]>((a, c) => { const t = c.times; return [a[0] + t.idle, a[1] + t.user + t.nice + t.sys + t.idle + t.irq] }, [0, 0])
465+
let prevTicks = ticks()
456466
const cpuTimer = setInterval(() => {
457-
s.cpu.push({ t: Date.now(), p: eapp.getAppMetrics().map((m) => ({ k: m.name ? `${m.type}:${m.name}` : m.type, c: m.cpu.percentCPUUsage })) })
467+
const now = ticks()
468+
const total = now[1] - prevTicks[1]
469+
const machine = os && total > 0 ? 100 * (1 - (now[0] - prevTicks[0]) / total) * os.cpus().length : -1
470+
prevTicks = now
471+
s.cpu.push({ t: Date.now(), p: [...eapp.getAppMetrics().map((m) => ({ k: m.name ? `${m.type}:${m.name}` : m.type, c: m.cpu.percentCPUUsage })), { k: 'machine', c: machine }] })
458472
}, 250)
459473
s.stop = () => { clearInterval(lagTimer); clearInterval(cpuTimer); ipcMain.removeListener('terminal:write', onWrite); wc.send = send }
460474
})
@@ -512,9 +526,9 @@ test.describe.serial('Lumopolis in the real app', () => {
512526
return { lag: s.lag, cpu: s.cpu, writes: s.writes, out: s.out }
513527
})
514528
const pageDiag = await page.evaluate(() => {
515-
const s = (window as unknown as { __pageDiag: { lag: number[][]; long: number[][]; frames: { t: number; d: number; render: number; scripts: [string, number][] }[]; stop: () => void } }).__pageDiag
529+
const s = (window as unknown as { __pageDiag: { lag: number[][]; long: number[][]; frames: { t: number; d: number; render: number; blocking: number; scripts: [string, number][] }[]; raf: number[][]; stop: () => void } }).__pageDiag
516530
s.stop()
517-
return { lag: s.lag, long: s.long, frames: s.frames }
531+
return { lag: s.lag, long: s.long, frames: s.frames, raf: s.raf }
518532
})
519533
const pageIn = (t: number): boolean => r.runs.some((w) => t >= w.start && t < w.end)
520534
const longOf = (during: boolean): string => {
@@ -527,7 +541,7 @@ test.describe.serial('Lumopolis in the real app', () => {
527541
const by: Record<string, number> = {}
528542
for (const x of xs) for (const [k, d] of x.scripts) by[k] = (by[k] ?? 0) + d
529543
const top = Object.entries(by).sort((p, q) => q[1] - p[1]).slice(0, 4).map(([k, d]) => `${k} ${d.toFixed(0)} ms`).join(', ')
530-
return `${xs.length} long frames, ${xs.reduce((a, x) => a + x.d, 0).toFixed(0)} ms in all (rendering ${xs.reduce((a, x) => a + x.render, 0).toFixed(0)} ms), longest ${Math.max(...xs.map((x) => x.d)).toFixed(0)} ms; scripts ${top || 'none'}`
544+
return `${xs.length} long frames, ${xs.reduce((a, x) => a + x.d, 0).toFixed(0)} ms in all (rendering ${xs.reduce((a, x) => a + x.render, 0).toFixed(0)} ms, blocking ${xs.reduce((a, x) => a + x.blocking, 0).toFixed(0)} ms), longest ${Math.max(...xs.map((x) => x.d)).toFixed(0)} ms; scripts ${top || 'none'}`
531545
}
532546
// The page's clock as epoch ms: timeOrigin + now(), corrected by its offset from Date.now() at the end.
533547
const { origin, cores } = await page.evaluate(() => ({ origin: Date.now() - performance.now(), cores: navigator.hardwareConcurrency }))
@@ -540,10 +554,11 @@ test.describe.serial('Lumopolis in the real app', () => {
540554
const cpuOf = (during: boolean): string => {
541555
const rows = diag.cpu.filter((x) => inRun(x.t - 125) === during)
542556
const sum: Record<string, number> = {}
543-
for (const x of rows) for (const q of x.p) sum[q.k] = (sum[q.k] ?? 0) + q.c
557+
let machine = 0
558+
for (const x of rows) for (const q of x.p) { if (q.k === 'machine') machine += q.c; else sum[q.k] = (sum[q.k] ?? 0) + q.c }
544559
const total = Object.values(sum).reduce((a, b) => a + b, 0) / Math.max(1, rows.length)
545560
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(', ')
546-
return `${rows.length} samples, total ${total.toFixed(0)}% (${top})`
561+
return `${rows.length} samples, total ${total.toFixed(0)}% (${top}); whole machine ${(machine / Math.max(1, rows.length)).toFixed(0)}% of ${cores * 100}%`
547562
}
548563
// Each key pressed after the hooks went in, in order, to the next letter main received, then to the first
549564
// terminal:data after that write that holds a letter, then to the page seeing its echo.
@@ -571,6 +586,13 @@ test.describe.serial('Lumopolis in the real app', () => {
571586
}
572587
console.log(`[typing while compiling, stages] during compiles ${stageOf(true)}; with none running ${stageOf(false)}`)
573588
const compileMs = r.runs.reduce((a, w) => a + (w.end - w.start), 0)
589+
const rafOf = (during: boolean): string => {
590+
const xs = pageDiag.raf.filter((x) => pageIn(x[0]) === during).map((x) => x[1])
591+
if (!xs.length) return 'no frames'
592+
const span = pageDiag.raf.filter((x) => pageIn(x[0]) === during).reduce((a, x) => a + x[1], 0)
593+
return `${xs.length} frames (${(1000 * xs.length / Math.max(1, span)).toFixed(0)} a second), gap ${lagOf(xs)}`
594+
}
595+
console.log(`[typing while compiling, frames] page frames during compiles ${rafOf(true)}; with none running ${rafOf(false)}`)
574596
console.log(`[typing while compiling, page] page timer lateness during compiles ${lagOf(pageDiag.lag.filter((x) => pageIn(x[0])).map((x) => x[1]))}, with none running ${lagOf(pageDiag.lag.filter((x) => !pageIn(x[0])).map((x) => x[1]))}; during ${compileMs.toFixed(0)} ms of compiles ${longOf(true)}; with none running ${longOf(false)}; during compiles ${framesOf(true)}; with none running ${framesOf(false)}`)
575597
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)}`)
576598
expect(r.compiling, 'the compiles were still running after 60 s of typing').toBe(false)

0 commit comments

Comments
 (0)