// 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/ 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 start a variant, built from the tree but for the // files given after it; none given: `tree`, the // working tree as it is // --window its web/renderer/src/view/window.ts, or `none`: a // ListWindow that never lets an item go // --list its web/renderer/src/tree/List.svelte // --runs measure each variant n times, interleaved (1) // --posts the posts in the feed (300) // --cpu 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/--.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(); 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=&from= 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/