#!/usr/bin/env node // CPU profile of one terminal bench workload (tool/perf/terminal_stream_bench.dart) // in headless Chrome: prints the functions with the most self and total // time while the stream runs, and the bench's own summary line. // // node tool/perf/terminal_stream_profile.mjs --build build/terminal-bench \ // [--workload seq] [--mb 8] [--top 40] [--out profile.cpuprofile] import { parseArgs } from 'node:util'; import { writeFile } from 'node:fs/promises'; import { setTimeout as delay } from 'node:timers/promises'; import { openBrowser } from '../capture/browser.mjs'; import { serveBuild } from '../capture/serve.mjs'; const { values: options } = parseArgs({ options: { build: { type: 'string' }, workload: { type: 'string', default: 'seq' }, mb: { type: 'string', default: '8' }, rate: { type: 'string', default: '30' }, top: { type: 'string', default: '40' }, out: { type: 'string' }, size: { type: 'string', default: '1280x800' }, }, }); if (!options.build) throw new Error('--build required'); const [width, height] = options.size.split('x').map(Number); const server = await serveBuild(options.build); const browser = await openBrowser(); const evaluate = async (expression) => (await browser.send('Runtime.evaluate', { expression, returnByValue: true })).result.value; try { await browser.send('Page.enable'); await browser.send('Runtime.enable'); await browser.send('Profiler.enable'); await browser.send('Profiler.setSamplingInterval', { interval: 200 }); await browser.send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: false }); const url = new URL('/', server.origin); url.searchParams.set('workload', options.workload); url.searchParams.set('mb', options.mb); url.searchParams.set('rate', options.rate); await browser.send('Page.navigate', { url: url.href }); // The bench streams 1.5 s after start-up; profile from then on. await delay(1500); await browser.send('Profiler.start'); let result = null; for (let i = 0; i < 600 && !result; i++) { await delay(250); result = await evaluate('window.__benchResult ?? null'); } const { profile } = await browser.send('Profiler.stop'); if (options.out) await writeFile(options.out, JSON.stringify(profile)); const byId = new Map(profile.nodes.map((n) => [n.id, n])); const parent = new Map(); for (const n of profile.nodes) for (const c of n.children ?? []) parent.set(c, n.id); const hits = new Map(); for (const s of profile.samples) hits.set(s, (hits.get(s) ?? 0) + 1); const name = (n) => `${n.callFrame.functionName || '(anonymous)'} ${n.callFrame.url.split('/').pop()}:${n.callFrame.lineNumber}`; const self = new Map(); const total = new Map(); let all = 0; for (const [id, count] of hits) { all += count; const key = name(byId.get(id)); self.set(key, (self.get(key) ?? 0) + count); const seen = new Set(); for (let at = id; at !== undefined; at = parent.get(at)) { const k = name(byId.get(at)); if (seen.has(k)) continue; seen.add(k); total.set(k, (total.get(k) ?? 0) + count); } } const pct = (v) => (100 * v / all).toFixed(1).padStart(5); const top = Number(options.top); console.log(`samples ${all}`); console.log('--- self'); for (const [k, v] of [...self].sort((a, b) => b[1] - a[1]).slice(0, top)) console.log(pct(v), pct(total.get(k)), k); console.log('--- total'); for (const [k, v] of [...total].sort((a, b) => b[1] - a[1]).slice(0, top)) console.log(pct(self.get(k) ?? 0), pct(v), k); console.log(result); } finally { await browser.close?.(); await server.close?.(); }