Web: WebAssembly build served cross-origin isolated, immutable and precompressed

The web app is built with --wasm (skwasm, JS/CanvasKit fallback beside it).
The proxy serves it with COOP same-origin + COEP credentialless so skwasm
renders on a worker; an installed build loads from /_v/<buildId>/ cached as
immutable, other files revalidate by ETag, and the installer precompresses
.br/.gz variants the proxy sends by Accept-Encoding. The bootstrap no
longer registers Flutter's deprecated service worker (a reload per install).
Read aloud on the web uses the Web Speech API through package:web: the
flutter_tts web plugin's interop casts fail under wasm. Bench kit: isolation
and headed modes, renderer and memory reporting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 21:05:17 +00:00
parent 23d8658e80
commit 3b1760fe8e
17 changed files with 737 additions and 17 deletions
+4
View File
@@ -13,6 +13,10 @@ no longer matches the app.
removes Chrome, the profile and the server.
- It needs no npm packages: Node 22+ (global `WebSocket` and `fetch`) and
Google Chrome (`CHROME_PATH` overrides the macOS default).
- `CAPTURE_ISOLATE=1` serves the build with the web proxy's cross-origin
isolation headers, so a `--wasm` build renders multi-threaded as installed.
`CAPTURE_HEADED=1` opens a real X11 window instead of headless (run under
`xvfb-run -a`; Xvfb has no WebGL, so the wasm build falls back to CanvasKit).
## The fixture app
+2 -1
View File
@@ -14,7 +14,8 @@ const DEFAULT_CHROME ='/Applications/Google Chrome.app/Contents/MacOS/Google Chr
async function launch(chromePath, gpu, args = []) {
const profile = await mkdtemp(join(tmpdir(), 'pipelines-capture-chrome-'));
const chrome = spawn(chromePath, [
'--headless=new', '--no-first-run', '--no-default-browser-check',
// CAPTURE_HEADED=1 opens a real X11 window (under xvfb-run) instead.
...(process.env.CAPTURE_HEADED === '1' ? ['--ozone-platform=x11'] : ['--headless=new']), '--no-first-run', '--no-default-browser-check',
// Without a GPU CanvasKit rasterises on the CPU; `gpu` keeps WebGL (Metal
// through ANGLE on a Mac) for benches that measure raster time.
// Linux keeps the GPU through ANGLE on Vulkan.
+8
View File
@@ -21,6 +21,13 @@ const MIME = {
'.woff2': 'font/woff2',
};
// `CAPTURE_ISOLATE=1` adds the Pipelines proxy's cross-origin isolation
// headers (web/src/appFiles.js), so a `--wasm` build renders multi-threaded as
// it does when installed; without them skwasm runs single-threaded.
const ISOLATE = process.env.CAPTURE_ISOLATE === '1'
? { 'cross-origin-opener-policy': 'same-origin', 'cross-origin-embedder-policy': 'credentialless' }
: {};
/** Serves [dir]; resolves to `{ origin, close() }`. */
export async function serveBuild(dir) {
const root = resolve(dir);
@@ -41,6 +48,7 @@ export async function serveBuild(dir) {
res.writeHead(200, {
'content-type': MIME[extname(file)] ?? 'application/octet-stream',
'cache-control': 'no-store',
...ISOLATE,
}).end(body);
} catch {
res.writeHead(404).end('not found');
+24 -4
View File
@@ -7,6 +7,9 @@
// Startup reports the first rasterised frame (ms since navigation) and the
// main.dart.js / canvaskit fetch times.
//
// --memory (Linux) adds `rssMB`: the resident memory of every process of the
// private Chrome (renderer, GPU, workers) once the measured steps settle.
//
// --cpuprofile records a V8 CPU profile of the measured steps (first rep),
// writes `<scenario>.cpuprofile` and prints the functions with the most
// inclusive and self time; use a `flutter build web --profile` build so the
@@ -22,7 +25,7 @@
// CanvasKit rasterises on the CPU, so compare builds with each other rather
// than predicting a device. --gpu keeps the GPU (WebGL), which is what a
// desktop user has: use it to judge raster time.
import { mkdir, writeFile } from 'node:fs/promises';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { parseArgs } from 'node:util';
import { setTimeout as delay } from 'node:timers/promises';
@@ -33,7 +36,7 @@ import { attribute, loadSourceMap } from './cpu_attribution.mjs';
const { values: opts } = parseArgs({
options: {
build: { type: 'string' }, out: { type: 'string' }, repeat: { type: 'string', default: '5' },
only: { type: 'string' }, cpuprofile: { type: 'boolean' }, frames: { type: 'boolean' }, shots: { type: 'boolean' }, gpu: { type: 'boolean' }, size: { type: 'string', default: '560x900' },
only: { type: 'string' }, cpuprofile: { type: 'boolean' }, frames: { type: 'boolean' }, shots: { type: 'boolean' }, gpu: { type: 'boolean' }, memory: { type: 'boolean' }, size: { type: 'string', default: '560x900' },
},
});
if (!opts.build || !opts.out) throw new Error('usage: --build <dir> --out <dir>');
@@ -106,6 +109,16 @@ const lookup = opts.cpuprofile ? await loadSourceMap(opts.build) : null;
const server = await serveBuild(opts.build);
const browser = await openBrowser({ gpu: Boolean(opts.gpu) });
const { send, on } = browser;
// Sums VmRSS over the browser's processes (SystemInfo ids are OS pids).
async function chromeRssMB() {
const { processInfo } = await browser.sendBrowser('SystemInfo.getProcessInfo');
let kb = 0;
for (const { id } of processInfo) {
const status = await readFile(`/proc/${id}/status`, 'utf8').catch(() => '');
kb += Number(status.match(/^VmRSS:\s+(\d+)/m)?.[1] ?? 0);
}
return kb / 1024;
}
const evaluate = async (expression) => (await send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true })).result?.value;
let frames = [];
let firstFrame = null;
@@ -191,8 +204,14 @@ try {
await delay(2000);
const startup = s.name.startsWith('startup');
const loads = startup ? await evaluate(`Object.fromEntries(performance.getEntriesByType('resource')
.filter((e) => /main\\.dart\\.js|canvaskit\\.wasm/.test(e.name))
.filter((e) => /main\\.dart\\.(js|wasm)$|canvaskit\\.wasm|skwasm(_heavy)?\\.wasm/.test(e.name))
.map((e) => [e.name.split('/').pop(), Math.round(e.responseEnd)]))`) : null;
if (startup && rep === 0) {
// Which build the loader picked: WebAssembly (skwasm, threaded only
// when cross-origin isolated) or JavaScript (CanvasKit).
console.log(` renderer: ${await evaluate(`(window._flutter_skwasmInstance ? 'skwasm' : window.flutterCanvasKit ? 'canvaskit' : '?')
+ (window.crossOriginIsolated ? ' isolated' : '')`)}`);
}
await run(s.setup);
await settle();
const startFrames = startup ? 0 : frames.length;
@@ -218,7 +237,8 @@ try {
const sum = (i) => f.reduce((a, x) => a + x[i], 0) / 1000;
const max = (i) => f.reduce((a, x) => Math.max(a, x[i]), 0) / 1000;
reps.push({ frames: f.length, buildSum: sum(0), buildMax: max(0), rasterSum: sum(1), rasterMax: max(1),
longest: max(2), over16: f.filter((x) => x[2] > 16667).length, firstFrame, ...loads });
longest: max(2), over16: f.filter((x) => x[2] > 16667).length, firstFrame, ...loads,
...(opts.memory ? { rssMB: await chromeRssMB() } : {}) });
}
const keys = Object.keys(reps[0]).filter((k) => typeof reps[0][k] === 'number');
results[s.name] = Object.fromEntries(keys.map((k) => [k, r1(median(reps.map((r) => r[k])))]));