Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cpp/web/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# cpp/web/build.sh lib/web/codepad.{js,wasm} and lib/web/grammar-imports.json
# cpp/web/build.sh --test build/web-test/codepad.{js,wasm} with the test hooks
# cpp/web/build.sh --grammar <src> <out> one grammar side module from <src>/parser.c and scanner.c
# cpp/web/build.sh --names lib/web with WASM function names, for bench profiles only
set -euo pipefail

EMSCRIPTEN_VERSION=7.0.0
Expand Down Expand Up @@ -36,6 +37,11 @@ fi
OUT=$ROOT/lib/web
OBJ=$ROOT/build/web/obj
CORE_DEFS=
LINK_EXTRA=
if [ "${1:-}" = --names ]; then
LINK_EXTRA=--profiling-funcs
echo "Building lib/web with function names for bench profiles. Run cpp/web/build.sh again before shipping." >&2
fi
if [ "${1:-}" = --test ]; then
OUT=$ROOT/build/web-test
OBJ=$ROOT/build/web-test/obj
Expand Down Expand Up @@ -67,7 +73,7 @@ export OBJ OPT TS PC CORE ROOT CORE_DEFS
xargs -P 8 -I{} bash -c 'compile "$1"' _ {}

exports=$(sed 's/^/_/' "$WEB/exports.txt" | paste -sd, -)
em++ $OPT -sMAIN_MODULE=2 -sALLOW_MEMORY_GROWTH=1 -sFORCE_FILESYSTEM=1 -sMODULARIZE=1 \
em++ $OPT $LINK_EXTRA -sMAIN_MODULE=2 -sALLOW_MEMORY_GROWTH=1 -sFORCE_FILESYSTEM=1 -sMODULARIZE=1 \
-sEXPORT_NAME=createCodepadCore -sENVIRONMENT=web,node \
-sEXPORTED_FUNCTIONS="$exports" \
-sEXPORTED_RUNTIME_METHODS=FS,HEAPU8,HEAPU16,HEAP32,UTF8ToString,stringToNewUTF8 \
Expand Down
84 changes: 72 additions & 12 deletions scripts/bench-web.mjs
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
// Typing bench in Chrome on a phone: yarn bench:web [lines] [port], the example served on that port through adb reverse.
// Typing bench in Chrome: yarn bench:web [lines] [port] [--desktop [rate]] [--profile]
// Without --desktop it drives Chrome on an Android device through adb, the example served on that port with adb reverse.
// --desktop opens the example in a local Chromium at phone size with the CPU slowed by rate. --profile adds a CPU profile.
import { execFileSync } from 'node:child_process';
import { chromium } from 'playwright';
import { mkdirSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { chromium, devices } from 'playwright';
import { summarize } from './profile-summary.mjs';

const lines = Number(process.argv[2] ?? 300);
// Key work matches the Redmi at 8x and engine tasks at 3x (calibrated on 73219f3): gate engine tasks with --desktop 3.
const DEFAULT_RATE = 8;

const args = process.argv.slice(2);
const positional = [];
let desktop = null;
let profile = false;
for (let i = 0; i < args.length; i++) {
if (args[i] === '--profile') profile = true;
else if (args[i] === '--desktop') {
desktop = /^\d+(\.\d+)?$/.test(args[i + 1] ?? '') ? Number(args[++i]) : DEFAULT_RATE;
Comment thread
SectionTN marked this conversation as resolved.
Resolved
} else positional.push(args[i]);
}
const lines = Number(positional[0] ?? 300);
const WEB_PORT = positional[1] ?? '8082';
const CHARS = 60;
const GAP_MS = 150;
const TEXT = 'let total = value + 1; '.repeat(3).slice(0, CHARS);
const PORT = 9222;
const WEB_PORT = process.argv[3] ?? '8082';
const ROOTS = { drawKeyed: 'key draw', runEngine: 'engine', applyInput: 'handler' };

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const pct = (sorted, q) => sorted[Math.min(sorted.length - 1, Math.floor(q * sorted.length))];
const ms = (n) => `${n.toFixed(1)} ms`;

execFileSync('adb', ['forward', `tcp:${PORT}`, 'localabstract:chrome_devtools_remote']);
const browser = await chromium.connectOverCDP(`http://127.0.0.1:${PORT}`);
try {
async function openPage() {
if (desktop !== null) {
const browser = await chromium.launch();
const context = await browser.newContext({ ...devices['Pixel 7'] });
const page = await context.newPage();
await page.goto(`http://localhost:${WEB_PORT}/`);
return { browser, page, close: () => browser.close() };
}
execFileSync('adb', ['forward', `tcp:${PORT}`, 'localabstract:chrome_devtools_remote']);
const browser = await chromium.connectOverCDP(`http://127.0.0.1:${PORT}`);
const page = browser
.contexts()
.flatMap((c) => c.pages())
.find((p) => new RegExp(`//(localhost|127\\.0\\.0\\.1):${WEB_PORT}/`).test(p.url()));
if (!page) throw new Error(`open http://localhost:${WEB_PORT} in Chrome on the phone first`);
const close = async () => {
await browser.close();
execFileSync('adb', ['forward', '--remove', `tcp:${PORT}`]);
};
if (!page) {
await close();
throw new Error(`open http://localhost:${WEB_PORT} in Chrome on the device first`);
}
return { browser, page, close };
}

const { page, close } = await openPage();
try {
await page.evaluate(() => localStorage.setItem('codepad_debug', '1'));
await page.reload();
await page.waitForFunction(() => globalThis.__codepadBench && globalThis.__codepadProbe);
Expand All @@ -34,28 +72,50 @@ try {
if (!box) throw new Error('the editor drew no line 2');
await page.mouse.click(box.x + 4, box.y + box.height / 2);
await sleep(1000);
const cdp = await page.context().newCDPSession(page);
if (desktop !== null) await cdp.send('Emulation.setCPUThrottlingRate', { rate: desktop });
await page.evaluate(() => {
for (const list of Object.values(globalThis.__codepadProbe)) list.length = 0;
});
const cdp = await page.context().newCDPSession(page);
if (profile) {
await cdp.send('Profiler.enable');
await cdp.send('Profiler.setSamplingInterval', { interval: 100 });
await cdp.send('Profiler.start');
}
for (const ch of TEXT) {
await cdp.send('Input.insertText', { text: ch });
await sleep(GAP_MS);
}
await sleep(500);
const taken = profile ? (await cdp.send('Profiler.stop')).profile : null;
if (desktop !== null) await cdp.send('Emulation.setCPUThrottlingRate', { rate: 1 });
const probe = await page.evaluate(() => globalThis.__codepadProbe);
const stats = (list) => {
const sorted = [...list].sort((a, b) => a - b);
return `p50 ${ms(pct(sorted, 0.5))}, p95 ${ms(pct(sorted, 0.95))}, max ${ms(sorted.at(-1) ?? 0)}`;
};
console.log(`${lines} lines, ${probe.work.length} of ${CHARS} keys`);
const where = desktop !== null ? `desktop at ${desktop}x` : 'device';
console.log(`${lines} lines, ${where}, ${probe.work.length} of ${CHARS} keys`);
for (const part of ['work', 'handler', 'engine', 'draw']) {
console.log(` ${part.padEnd(8)}${stats(probe[part])}`);
}
console.log(` largest frame ${ms(Math.max(0, ...probe.pumps))} of ${probe.pumps.length}`);
console.log(` engine tasks ${stats(probe.tasks)} of ${probe.tasks.length}`);
if (taken) {
const dir = path.join(import.meta.dirname, '..', 'build', 'bench');
mkdirSync(dir, { recursive: true });
const file = path.join(dir, `typing-${lines}-${Date.now()}.cpuprofile`);
writeFileSync(file, JSON.stringify(taken));
const all = summarize(taken, ROOTS, Number.POSITIVE_INFINITY);
const top = summarize(taken, ROOTS, 12);
for (const group of ['key draw', 'handler', 'engine', 'other']) {
const total = (all[group] ?? []).reduce((sum, f) => sum + f.selfMs, 0);
console.log(` profile ${group}: ${ms(total)} over the 60 keys`);
for (const f of top[group] ?? []) console.log(` ${ms(f.selfMs).padStart(9)} ${f.fn}`);
}
console.log(` profile saved to ${file}`);
}
await page.evaluate(() => localStorage.removeItem('codepad_debug'));
} finally {
await browser.close();
execFileSync('adb', ['forward', '--remove', `tcp:${PORT}`]);
await close();
}
46 changes: 46 additions & 0 deletions scripts/profile-summary.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Splits a CDP CPU profile into groups by the nearest named root on each sample's stack, then ranks self time.

/**
* @param {{ nodes: { id: number, callFrame: { functionName: string }, children?: number[] }[], samples: number[], timeDeltas: number[] }} profile
* @param {Record<string, string>} roots function name to group name
* @param {number} top functions kept per group
* @returns {Record<string, { fn: string, selfMs: number }[]>}
*/
export function summarize(profile, roots, top) {
const byId = new Map();
const parent = new Map();
for (const node of profile.nodes) {
byId.set(node.id, node);
for (const child of node.children ?? []) parent.set(child, node.id);
}
const groupOf = new Map();
const group = (id) => {
const known = groupOf.get(id);
if (known) return known;
const name = byId.get(id)?.callFrame.functionName;
const up = parent.get(id);
let found = 'other';
if (Object.hasOwn(roots, name)) found = roots[name];
else if (up !== undefined) found = group(up);
groupOf.set(id, found);
return found;
};
// Microseconds per group and function, so sums stay exact.
const totals = new Map();
profile.samples.forEach((id, i) => {
const fn = byId.get(id)?.callFrame.functionName || '(anonymous)';
if (fn === '(idle)') return;
const g = group(id);
const fns = totals.get(g) ?? new Map();
fns.set(fn, (fns.get(fn) ?? 0) + (profile.timeDeltas[i] ?? 0));
totals.set(g, fns);
});
const out = {};
for (const [g, fns] of totals) {
out[g] = [...fns]
.sort((a, b) => b[1] - a[1])
.slice(0, top)
.map(([fn, us]) => ({ fn, selfMs: us / 1000 }));
}
return out;
}
26 changes: 15 additions & 11 deletions src/web/pump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,27 @@ function frame(): void {
const left = core && urgent ? core._cpw_pump(PUMP_BUDGET_MS) : 0;
const pumped = probe ? performance.now() - start : 0;
for (const view of [...views]) {
const drawStart = probe ? performance.now() : 0;
view.draw();
if (probe && view === keyView) {
const drawn = performance.now() - drawStart;
probe.work.push(keyMs + pumped + drawn);
probe.handler.push(keyMs);
probe.engine.push(pumped);
probe.draw.push(drawn);
keyView = null;
keyMs = 0;
}
if (probe && view === keyView) drawKeyed(probe, view, pumped);
else view.draw();
}
if (probe) probe.pumps.push(performance.now() - start);
if (left > 0) requestPump();
else postEngine();
}

// The draw the bench counts as a key's work, a named call so a profile can tell it from other frames.
function drawKeyed(p: Probe, view: View, pumped: number): void {
const drawStart = performance.now();
view.draw();
const drawn = performance.now() - drawStart;
p.work.push(keyMs + pumped + drawn);
p.handler.push(keyMs);
p.engine.push(pumped);
p.draw.push(drawn);
keyView = null;
keyMs = 0;
}

let channel: MessageChannel | null = null;
let engineQueued = false;

Expand Down
72 changes: 72 additions & 0 deletions tests/web/profile-summary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
// @ts-expect-error plain JavaScript script without types
import { summarize } from '../../scripts/profile-summary.mjs';

type Node = { id: number; callFrame: { functionName: string }; children?: number[] };

// A CDP profile: nodes by id with children, one sample per node id, timeDeltas in microseconds.
function profile(nodes: Node[], samples: number[], intervalUs: number) {
return {
nodes,
startTime: 0,
endTime: samples.length * intervalUs,
samples,
timeDeltas: samples.map(() => intervalUs),
};
}

const node = (id: number, functionName: string, children: number[] = []): Node => ({
id,
callFrame: { functionName },
children,
});

const ROOTS = { drawKeyed: 'key draw', runEngine: 'engine', applyInput: 'handler' };

test('samples go to the nearest root on their stack, snapshot frames to other', () => {
const nodes = [
node(1, '(root)', [2, 7]),
node(2, 'frame', [3, 10]),
node(3, 'drawKeyed', [4]),
node(4, 'draw', [5]),
node(5, 'frame', [6]),
node(6, 'paint'),
node(7, 'runEngine', [8]),
node(8, 'wasm-function[cp_reparse]'),
node(10, 'draw', [11]),
node(11, 'frame', [12]),
node(12, 'paint'),
];
const samples = [6, 6, 6, 12, 12, 12, 12, 8, 8];
const out = summarize(profile(nodes, samples, 100), ROOTS, 12);
assert.deepEqual(out['key draw'], [{ fn: 'paint', selfMs: 0.3 }]);
assert.deepEqual(out.engine, [{ fn: 'wasm-function[cp_reparse]', selfMs: 0.2 }]);
assert.deepEqual(out.other, [{ fn: 'paint', selfMs: 0.4 }]);
});

test('each group lists at most top functions, largest self time first', () => {
const nodes = [
node(1, '(root)', [2]),
node(2, 'runEngine', [3, 4, 5]),
node(3, 'a'),
node(4, 'b'),
node(5, 'c'),
];
const out = summarize(profile(nodes, [3, 4, 4, 5, 5, 5], 1000), ROOTS, 2);
assert.deepEqual(out.engine, [
{ fn: 'c', selfMs: 3 },
{ fn: 'b', selfMs: 2 },
]);
});

test('a frame named like an Object method stays in its root group', () => {
const nodes = [
node(1, '(root)', [2]),
node(2, 'runEngine', [3, 4]),
node(3, 'constructor'),
node(4, 'toString'),
];
const out = summarize(profile(nodes, [3, 4], 1000), ROOTS, 12);
assert.deepEqual(Object.keys(out), ['engine']);
});
Loading