~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
545 lines · 22.5 KB Raw History
// How smoothly a long feed scrolls in a workspace column, and what it holds
// once scrolled through: the renderer's `Tree` drawing 300 made-up posts
// (harness/scroll/feed.ts: pictures of real shapes, link cards, quotes,
// videos with posters) as the bundled views/bsky-feed.json draws them, the
// answer given its `_meta.ui` by `target/debug/hud resolve` as the server
// would, in a fixed-height scroller as a column is
// (harness/scroll/Bench.svelte), built on its own from the renderer's
// sources (no dev server, no HUD server, no network: Bluesky's CDN URLs are
// answered with local pictures made once into target/scroll/img), in
// Chromium at 1440×900, twice the pixels.
//
//   node harness/scroll.mjs [options]    `just scroll ...`
//
// Each variant is built into target/scroll/build/<name> and measured in
// turn, each scenario under a CDP trace:
//
//   steady   a wheel of 60 px every 16 ms for 10 s, from the top
//   flicks   six flicks, each a trackpad's momentum: 400 px a frame,
//            slowing, about 5,700 px
//   end      flicks on to the end; then the page's JS heap (after a
//            garbage collection) and its DOM nodes
//   remount  scrolled back to the middle, the column drawn again with its
//            scroll put back (as one coming back into view): how far what
//            was at its top moved, as drawn and once settled
//
// and, in a second pass with no trace, `screen`: the steady scroll and the
// flicks again, the screen's own frames (CDP screencast, what the
// compositor shows, though the main thread is busy) checked for a post
// drawn blank: a run of more than 80 px of the column's background. The
// flicks show two such frames (156 px, the screen's foot) whatever the
// list does: with no window (`--window none`) and no `content-visibility`,
// every post laid out, the same two: Chrome's tiles rastered behind 400 px
// a frame, not a post the list left undrawn. More than two is the list's.
//
// For each it prints long tasks (over 50 ms) and the longest, scripting,
// style and layout time, layouts and forced ones (a layout or style
// recalculation inside a script), frame times from requestAnimationFrame
// (p50, p95, max), and the frames whose view held an item let go (drawn as
// space; an IntersectionObserver's, sampled each frame).
//
// Options:
//
//   --variant <name>    start a variant, built from the tree but for the
//                       files given after it; none given: `tree`, the
//                       working tree as it is
//   --window <file>     its web/renderer/src/view/window.ts, or `none`: a
//                       ListWindow that never lets an item go
//   --list <file>       its web/renderer/src/tree/List.svelte
//   --runs <n>          measure each variant n times, interleaved (1)
//   --posts <n>         the posts in the feed (300)
//   --cpu <rate>        slow the page's CPU down so many times (1), as a
//                       slower machine or a busier page would be
//   --keep              keep each trace, target/scroll/<variant>-<scenario>-<run>.json
//
// To compare with the commit: `git show HEAD:web/renderer/src/view/window.ts
// > target/head-window.ts`, then `just scroll --variant head --window
// target/head-window.ts --variant tree`.

import { execFileSync } from 'node:child_process';
import { createServer } from 'node:http';
import { createRequire } from 'node:module';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, extname, join, resolve } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { chromium } from 'playwright';

const here = dirname(fileURLToPath(import.meta.url));
const root = resolve(here, '..');
const web = join(root, 'web');
const out = join(root, 'target/scroll');
const say = (line) => console.log(`==> ${line}`);
const fail = (why) => {
	say(why);
	process.exit(2);
};

const variants = [];
let runs = 1;
let posts = 300;
let keep = false;
let cpu = 1;
const argv = process.argv.slice(2);
for (let i = 0; i < argv.length; i++) {
	const a = argv[i];
	const next = () => argv[++i] ?? fail(`${a} needs a value`);
	const last = () => variants.at(-1) ?? fail(`${a} comes after a --variant`);
	if (a === '--variant') variants.push({ name: next() });
	else if (a === '--window') last().window = next();
	else if (a === '--list') last().list = next();
	else if (a === '--runs') runs = Number(next());
	else if (a === '--posts') posts = Number(next());
	else if (a === '--keep') keep = true;
	else if (a === '--cpu') cpu = Number(next());
	else fail(`unknown option ${a}`);
}
if (variants.length === 0) variants.push({ name: 'tree' });

/** A ListWindow that never lets an item go: the list drawn whole, the browser alone skipping what is out of view (`content-visibility`). */
const NO_WINDOW = `import { SvelteSet } from 'svelte/reactivity';
export const FRAMED = false;
export const WINDOWED = 24;
export class ListWindow {
	idle = new SvelteSet<number>();
	constructor(_items: () => readonly unknown[]) {}
	height(_item: unknown): number { return 240; }
	grow(_length: number) {}
	watch = (_element: HTMLElement, _idle: boolean) => {};
	attach = (_element: HTMLElement) => {};
}
`;

// Vite and the Svelte plugin, as web/ has them.
const fromWeb = createRequire(join(web, 'package.json'));
const vite = await import(pathToFileURL(fromWeb.resolve('vite')).href);
const { svelte } = await import(pathToFileURL(fromWeb.resolve('@sveltejs/vite-plugin-svelte')).href);

const sources = {
	window: join(web, 'renderer/src/view/window.ts'),
	list: join(web, 'renderer/src/tree/List.svelte')
};

const hud = join(root, 'target/debug/hud');
if (!existsSync(hud)) fail('no target/debug/hud: `cargo build -p hud`');
const { feed } = await import(pathToFileURL(join(here, 'scroll/feed.ts')).href);
const view = JSON.parse(readFileSync(join(root, 'views/bsky-feed.json'), 'utf8'));

/** `count` posts of the feed from `from`, as the query tool answers getFeed with them: its `_meta.ui`, built by `hud resolve`. */
function tree(count, from) {
	const params = { feed: 'at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/whats-hot', limit: 100 };
	const answer = {
		content: [],
		structuredContent: { method: 'app.bsky.feed.getFeed', service: 'did:web:api.bsky.app#bsky_appview', params, response: { feed: feed(count, from) } },
		_meta: { view: { doc: view, mode: 'query', trusted: true, origins: view.origins } }
	};
	const args = { method: 'app.bsky.feed.getFeed', params };
	return JSON.parse(execFileSync(hud, ['resolve', 'query', JSON.stringify(args)], { input: JSON.stringify(answer), encoding: 'utf8', maxBuffer: 1 << 28 }))._meta.ui;
}

async function build(variant) {
	const swaps = new Map();
	if (variant.window) swaps.set(sources.window, variant.window === 'none' ? NO_WINDOW : readFileSync(resolve(variant.window), 'utf8'));
	if (variant.list) swaps.set(sources.list, readFileSync(resolve(variant.list), 'utf8'));
	const dir = join(out, 'build', variant.name);
	await vite.build({
		configFile: false,
		root: join(here, 'scroll'),
		base: './',
		logLevel: 'warn',
		// HUD's packages as their source (web/vite.config.ts).
		resolve: { conditions: ['@hud/source', ...vite.defaultClientConditions] },
		plugins: [
			{
				// The page's own imports of packages found as web/'s are.
				name: 'scroll-from-web',
				enforce: 'pre',
				resolveId(id, importer, options) {
					if (!importer?.startsWith(join(here, 'scroll')) || /^[./\0]/.test(id) || id.includes(':')) return null;
					return this.resolve(id, join(web, 'index.html'), { ...options, skipSelf: true });
				}
			},
			{
				// The variant's files in place of the tree's, their imports still the tree's.
				name: 'scroll-variant',
				enforce: 'pre',
				load(id) {
					return swaps.get(id) ?? null;
				}
			},
			svelte({ compilerOptions: { runes: true } })
		],
		build: { outDir: dir, emptyOutDir: true, reportCompressedSize: false, target: 'esnext' }
	});
	return dir;
}

/** The pictures Bluesky's CDN is answered with, made once: one per shape of feed.ts's SHAPES, an avatar and a link card's. */
async function pictures(browser) {
	const dir = join(out, 'img');
	const shapes = [
		[1000, 1000],
		[1000, 750],
		[750, 1000],
		[1000, 563],
		[563, 1000],
		[1000, 667],
		[667, 1000],
		[1000, 524],
		[800, 1000],
		[1000, 400]
	];
	const files = [...shapes.map((s, k) => ({ file: `r${k}.jpg`, size: s })), { file: 'avatar.jpg', size: [128, 128] }];
	if (files.every(({ file }) => existsSync(join(dir, file)))) return dir;
	mkdirSync(dir, { recursive: true });
	const page = await browser.newPage();
	for (const [k, { file, size }] of files.entries()) {
		const data = await page.evaluate(
			([w, h, seed]) => {
				const canvas = document.createElement('canvas');
				canvas.width = w;
				canvas.height = h;
				const g = canvas.getContext('2d');
				const sky = g.createLinearGradient(0, 0, w, h);
				sky.addColorStop(0, `hsl(${seed * 37} 60% 70%)`);
				sky.addColorStop(1, `hsl(${seed * 37 + 120} 50% 30%)`);
				g.fillStyle = sky;
				g.fillRect(0, 0, w, h);
				let a = seed * 9973 + 1;
				const rand = () => ((a = (a * 16807) % 2147483647) / 2147483647);
				for (let i = 0; i < 400; i++) {
					g.fillStyle = `hsl(${rand() * 360} ${40 + rand() * 40}% ${30 + rand() * 50}% / ${0.2 + rand() * 0.6})`;
					g.beginPath();
					g.arc(rand() * w, rand() * h, 4 + rand() * w * 0.08, 0, Math.PI * 2);
					g.fill();
				}
				const noise = g.getImageData(0, 0, w, h);
				for (let i = 0; i < noise.data.length; i += 4) {
					const n = (rand() - 0.5) * 30;
					noise.data[i] += n;
					noise.data[i + 1] += n;
					noise.data[i + 2] += n;
				}
				g.putImageData(noise, 0, 0);
				return canvas.toDataURL('image/jpeg', 0.82).split(',')[1];
			},
			[...size, k + 1]
		);
		writeFileSync(join(dir, file), Buffer.from(data, 'base64'));
	}
	await page.close();
	return dir;
}

/** A static server of the variants' builds, and at /tree?posts=<n>&from=<k> the feed's tree (`tree`). */
async function serve() {
	const types = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css' };
	const trees = new Map();
	const server = createServer((req, res) => {
		const url = new URL(req.url, 'http://x');
		if (url.pathname === '/tree') {
			const key = `${url.searchParams.get('posts')} ${url.searchParams.get('from')}`;
			if (!trees.has(key)) trees.set(key, JSON.stringify(tree(Number(url.searchParams.get('posts')), Number(url.searchParams.get('from')))));
			return res.writeHead(200, { 'Content-Type': 'application/json' }).end(trees.get(key));
		}
		const path = join(out, 'build', decodeURIComponent(url.pathname));
		if (!path.startsWith(join(out, 'build')) || !existsSync(path)) return res.writeHead(404).end();
		res.writeHead(200, { 'Content-Type': types[extname(path)] ?? 'application/octet-stream' }).end(readFileSync(path));
	});
	await new Promise((done) => server.listen(0, '127.0.0.1', done));
	return { server, base: `http://127.0.0.1:${server.address().port}` };
}

const CATEGORIES = ['devtools.timeline', 'disabled-by-default-devtools.timeline', 'v8.execute', 'toplevel', 'blink.user_timing'];
const SCRIPT = new Set(['FunctionCall', 'EvaluateScript', 'TimerFire', 'FireAnimationFrame', 'EventDispatch', 'RunMicrotasks', 'FireIdleCallback', 'v8.compile', 'v8.callFunction', 'v8.run']);
const LAYOUT = new Set(['Layout', 'UpdateLayoutTree']);

/** What a trace says of the page's main thread. */
function analyse(trace) {
	const events = trace.traceEvents;
	const names = new Map(events.filter((e) => e.ph === 'M' && e.name === 'thread_name' && e.args.name === 'CrRendererMain').map((e) => [`${e.pid}:${e.tid}`, 0]));
	for (const e of events) {
		const k = `${e.pid}:${e.tid}`;
		if (names.has(k)) names.set(k, names.get(k) + 1);
	}
	const main = [...names].sort((a, b) => b[1] - a[1])[0]?.[0];
	const mine = events.filter((e) => `${e.pid}:${e.tid}` === main && e.ph === 'X' && e.dur !== undefined).sort((a, b) => a.ts - b.ts || b.dur - a.dur);
	const result = { long: 0, longest: 0, script: 0, layoutMs: 0, styleMs: 0, layouts: 0, forced: 0 };
	// Open events, as a stack, to tell what each runs inside.
	const stack = [];
	let scriptOpen = 0;
	let layoutInScript = 0;
	for (const e of mine) {
		while (stack.length && stack.at(-1).ts + stack.at(-1).dur <= e.ts) {
			const done = stack.pop();
			if (SCRIPT.has(done.name)) scriptOpen--;
		}
		if (e.name === 'RunTask' && stack.length === 0) {
			if (e.dur > 50_000) result.long++;
			result.longest = Math.max(result.longest, e.dur / 1000);
		}
		if (SCRIPT.has(e.name) && scriptOpen === 0) result.script += e.dur / 1000;
		if (LAYOUT.has(e.name)) {
			if (e.name === 'Layout') {
				result.layouts++;
				result.layoutMs += e.dur / 1000;
			} else result.styleMs += e.dur / 1000;
			if (scriptOpen > 0) {
				result.forced++;
				layoutInScript += e.dur / 1000;
			}
		}
		if (SCRIPT.has(e.name)) scriptOpen++;
		stack.push(e);
	}
	result.script -= layoutInScript;
	return result;
}

const percentile = (list, p) => {
	if (!list.length) return 0;
	const sorted = [...list].sort((a, b) => a - b);
	return sorted[Math.min(sorted.length - 1, Math.floor((sorted.length - 1) * p))];
};

/** Wheel events at the column's middle: `deltas`, one every 16 ms, kept to time. */
async function wheel(page, deltas) {
	const t0 = Date.now();
	for (const [i, d] of deltas.entries()) {
		const wait = t0 + i * 16 - Date.now();
		if (wait > 0) await new Promise((done) => setTimeout(done, wait));
		await page.mouse.wheel(0, d);
	}
}

const STEADY = Array.from({ length: 625 }, () => 60);
const FLICK = [...Array.from({ length: 50 }, (_, k) => Math.round(400 * 0.93 ** k)), ...Array.from({ length: 15 }, () => 0)];
const FLICKS = Array.from({ length: 6 }, () => FLICK).flat();

/** One scenario under a trace: its trace's numbers and the page's frames. */
async function traced(browser, page, label, drive) {
	await browser.startTracing(page, { categories: CATEGORIES });
	await page.evaluate(() => bench.start());
	await drive();
	const sampled = await page.evaluate(() => bench.stop());
	await page.waitForTimeout(300);
	const buffer = await browser.stopTracing();
	if (keep) writeFileSync(join(out, `${label}.json`), buffer);
	const trace = analyse(JSON.parse(buffer.toString()));
	const f = sampled.frames;
	return { ...trace, p50: percentile(f, 0.5), p95: percentile(f, 0.95), max: Math.max(0, ...f), idleSeen: sampled.blanks.blank, sampled: sampled.blanks.frames };
}

/** The screencast's frames checked for a post drawn blank: a run of rows of the column's background, inside its scroller, over 80 px. The first few found go to target/scroll/<label>-blank-<frame>.jpg, to look at. */
async function blankFrames(browser, frames, box, scale, label) {
	const page = await browser.newPage();
	const result = await page.evaluate(
		async ({ frames, box, scale }) => {
			const found = [];
			let worst = 0;
			const canvas = document.createElement('canvas');
			const g = canvas.getContext('2d', { willReadFrequently: true });
			for (const [n, data] of frames.entries()) {
				const img = new Image();
				img.src = `data:image/jpeg;base64,${data}`;
				await img.decode();
				canvas.width = img.width;
				canvas.height = img.height;
				g.drawImage(img, 0, 0);
				const k = img.width / (1440 * scale);
				const x0 = Math.round((box.x + 24) * scale * k);
				const x1 = Math.round((box.x + box.width - 24) * scale * k);
				const y0 = Math.round(box.y * scale * k);
				const y1 = Math.round((box.y + box.height) * scale * k);
				const { data: px } = g.getImageData(x0, y0, x1 - x0, y1 - y0);
				const w = x1 - x0;
				let run = 0;
				let longest = 0;
				for (let y = 0; y < y1 - y0; y++) {
					let plain = true;
					for (let x = 0; x < w; x += 2) {
						const i = (y * w + x) * 4;
						if (px[i] < 240 || px[i + 1] < 240 || px[i + 2] < 240) {
							plain = false;
							break;
						}
					}
					run = plain ? run + 1 : 0;
					longest = Math.max(longest, run);
				}
				const css = longest / (scale * k);
				worst = Math.max(worst, css);
				if (css > 80) found.push(n);
			}
			return { found, worst: Math.round(worst) };
		},
		{ frames, box, scale }
	);
	await page.close();
	for (const n of result.found.slice(0, 3)) writeFileSync(join(out, `${label}-blank-${n}.jpg`), Buffer.from(frames[n], 'base64'));
	return { blank: result.found.length, worst: result.worst, frames: frames.length };
}

async function screened(browser, page, drive) {
	const cdp = await page.context().newCDPSession(page);
	const frames = [];
	cdp.on('Page.screencastFrame', ({ data, sessionId }) => {
		frames.push(data);
		cdp.send('Page.screencastFrameAck', { sessionId }).catch(() => {});
	});
	await cdp.send('Page.startScreencast', { format: 'jpeg', quality: 70 });
	await drive();
	await page.waitForTimeout(200);
	await cdp.send('Page.stopScreencast');
	await cdp.detach();
	return frames;
}

async function open(browser, base, variant, count = posts) {
	const context = await browser.newContext({ viewport: { width: 1440, height: 900 }, deviceScaleFactor: 2, locale: 'en-US', timezoneId: 'America/Los_Angeles' });
	await context.route(/^https:\/\/(cdn|video)\.bsky\.app\//, (route) => {
		const url = route.request().url();
		const file = url.includes('/avatar') ? 'avatar.jpg' : `r${/r(\d+)(@jpeg|\/thumbnail)/.exec(url)?.[1] ?? 0}.jpg`;
		route.fulfill({ status: 200, contentType: 'image/jpeg', body: readFileSync(join(out, 'img', file)) });
	});
	const page = await context.newPage();
	page.on('pageerror', (error) => say(`${variant.name}: ${error.message}`));
	page.on('console', (message) => message.type() === 'error' && say(`${variant.name}: ${message.text()}`));
	if (cpu !== 1) await (await context.newCDPSession(page)).send('Emulation.setCPUThrottlingRate', { rate: cpu });
	await page.goto(`${base}/${variant.name}/index.html?posts=${count}`);
	await page.waitForFunction(() => window.bench && bench.items() > 0);
	await page.waitForTimeout(1500);
	const box = await page.evaluate(() => bench.box());
	await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
	return { context, page, box };
}

async function measure(browser, base, variant, run) {
	const label = (s) => `${variant.name}-${s}-${run}`;
	const rows = {};
	{
		const { context, page } = await open(browser, base, variant);
		rows.steady = await traced(browser, page, label('steady'), () => wheel(page, STEADY));
		rows.flicks = await traced(browser, page, label('flicks'), () => wheel(page, FLICKS));
		rows.end = await traced(browser, page, label('end'), async () => {
			for (let i = 0; i < 40 && !(await page.evaluate(() => bench.atEnd())); i++) await wheel(page, FLICK);
		});
		await page.waitForTimeout(1000);
		const cdp = await context.newCDPSession(page);
		await cdp.send('HeapProfiler.collectGarbage');
		await cdp.send('Performance.enable');
		const { metrics } = await cdp.send('Performance.getMetrics');
		const metric = (name) => metrics.find((m) => m.name === name)?.value ?? 0;
		rows.end.heap = metric('JSHeapUsedSize') / 2 ** 20;
		rows.end.nodes = metric('Nodes');
		rows.end.drawn = await page.evaluate(() => bench.drawn());
		rows.end.reachedEnd = await page.evaluate(() => bench.atEnd());
		// Back to the middle, then the column drawn again.
		const half = await page.evaluate(() => bench.scrollTop() / 2);
		let up = 0;
		const back = [];
		while (up < half) {
			const d = Math.min(400, half - up);
			back.push(-d);
			up += d;
		}
		await wheel(page, back);
		await page.waitForTimeout(1000);
		rows.remount = await page.evaluate(() => bench.remount(600));
		await context.close();
	}
	{
		const { context, page, box } = await open(browser, base, variant);
		const steady = await screened(browser, page, () => wheel(page, STEADY));
		const flicks = await screened(browser, page, () => wheel(page, FLICKS));
		rows.screen = { steady: await blankFrames(browser, steady, box, 2, label('steady')), flicks: await blankFrames(browser, flicks, box, 2, label('flicks')) };
		await context.close();
	}
	{
		// A page at a time, as Load more adds them: the first, then two more, each once scrolled to the end.
		const { context, page } = await open(browser, base, variant, 100);
		for (const n of [1, 2]) {
			for (let i = 0; i < 40 && !(await page.evaluate(() => bench.atEnd())); i++) await wheel(page, FLICK);
			await page.waitForTimeout(500);
			rows[`more${n}`] = await traced(browser, page, label(`more${n}`), async () => {
				await page.evaluate(() => bench.more(100));
				await page.waitForTimeout(1500);
			});
		}
		await context.close();
	}
	return rows;
}

const fmt = (n, digits = 0) => (typeof n === 'number' ? n.toFixed(digits) : String(n));

mkdirSync(out, { recursive: true });
for (const variant of variants) {
	say(`building ${variant.name}`);
	await build(variant);
}
const browser = await chromium.launch({ channel: 'chromium' });
const { server, base } = await serve();
try {
	await pictures(browser);
	const results = [];
	for (let run = 1; run <= runs; run++) {
		for (const variant of variants) {
			say(`measuring ${variant.name}, run ${run}`);
			try {
				results.push({ variant: variant.name, run, rows: await measure(browser, base, variant, run) });
			} catch (error) {
				say(`${variant.name}, run ${run}: ${error.message}`);
			}
			writeFileSync(join(out, 'results.json'), JSON.stringify(results, null, 2));
		}
	}
	console.log();
	console.log('variant      run scenario  long  longest  script  style  layout  layouts  forced  p50   p95   max    idle-in-view');
	for (const { variant, run, rows } of results) {
		for (const s of ['steady', 'flicks', 'end', 'more1', 'more2']) {
			const r = rows[s];
			console.log(
				[
					variant.padEnd(12),
					String(run).padStart(3),
					s.padEnd(8),
					fmt(r.long).padStart(5),
					fmt(r.longest).padStart(8),
					fmt(r.script).padStart(7),
					fmt(r.styleMs).padStart(6),
					fmt(r.layoutMs).padStart(7),
					fmt(r.layouts).padStart(8),
					fmt(r.forced).padStart(7),
					fmt(r.p50, 1).padStart(5),
					fmt(r.p95, 1).padStart(5),
					fmt(r.max, 0).padStart(5),
					`${r.idleSeen}/${r.sampled}`.padStart(15)
				].join(' ')
			);
		}
	}
	console.log();
	console.log('variant      run  heap MB  nodes  drawn  end   remount moved (drawn, settled)  blank frames steady, flicks (worst px)');
	for (const { variant, run, rows } of results) {
		const e = rows.end;
		const m = rows.remount;
		const sc = rows.screen;
		console.log(
			[
				variant.padEnd(12),
				String(run).padStart(3),
				fmt(e.heap, 1).padStart(8),
				fmt(e.nodes).padStart(6),
				fmt(e.drawn).padStart(6),
				String(e.reachedEnd).padStart(5),
				`${fmt(m.drawn, 0)}, ${fmt(m.settled, 0)}`.padStart(32),
				`  ${sc.steady.blank}/${sc.steady.frames} (${sc.steady.worst}), ${sc.flicks.blank}/${sc.flicks.frames} (${sc.flicks.worst})`
			].join(' ')
		);
	}
	writeFileSync(join(out, 'results.json'), JSON.stringify(results, null, 2));
	say(`results in ${join(out, 'results.json')}`);
} finally {
	await browser.close();
	server.close();
}