| 1 | // Runs the web feed's bench (src/feed-bench.ts) in headless Chromium and writes |
| 2 | // the report: the JSON the tables are rendered from and the same tables as |
| 3 | // Markdown — the web twin of the iOS `--autorun feed` + feed-report.json. |
| 4 | // corepack pnpm tsx scripts/feed-report.ts [--passes 5] [--count 60] [--combos iframe:on,shadow:on,iframe:off,shadow:off] |
| 5 |
| // [--no-prewarm] [--theme dark] [--origin http://127.0.0.1:5174] [--out docs/feed-report-web] |
| 6 | // Needs the dev server (corepack pnpm dev [--port 5174]). The frozen canvas prototypes (blitz, litehtml) run only when named |
| 7 | // in --combos and built (pnpm build:blitz-web / build:litehtml-web; docs/decisions.md, "The renderer prototypes are frozen"). |
| 8 | // Headless Chromium exposes measureUserAgentSpecificMemory (the page is cross-origin isolated), so the memory row counts |
| 9 | // workers and frames there; the window is 1280×900 so the scroller is a desktop screen. |
| 10 |
| 11 | import { chromium } from '@playwright/test'; |
| 12 | import { execFileSync } from 'node:child_process'; |
| 13 | import fs from 'node:fs'; |
| 14 | import path from 'node:path'; |
| 15 | import { fileURLToPath } from 'node:url'; |
| 16 |
| 17 | const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 18 | const argv = process.argv.slice(2); |
| 19 | const opt = (name: string, def: string) => { const i = argv.indexOf(name); return i >= 0 && argv[i + 1] ? argv[i + 1] : def; }; |
| 20 | const passes = Number(opt('--passes', '5')); |
| 21 | const count = Number(opt('--count', '60')); |
| 22 | const combos = opt('--combos', 'iframe:on,shadow:on,iframe:off,shadow:off'); |
| 23 | const prewarm = !argv.includes('--no-prewarm'); |
| 24 | const theme = opt('--theme', 'light'); |
| 25 | const origin = opt('--origin', 'http://127.0.0.1:5174'); |
| 26 | const out = opt('--out', 'docs/feed-report-web'); |
| 27 |
| 28 | // --expose-gc: the bench calls window.gc() before its performance.memory readings (no garbage in the memory row). |
| 29 | const browser = await chromium.launch({ args: ['--js-flags=--expose-gc'] }); |
| 30 | const page = await browser.newPage({ viewport: { width: 1280, height: 900 } }); |
| 31 | page.on('console', (m) => { if (m.type() === 'error') console.error('[page]', m.text()); }); |
| 32 | // Resident memory of the whole browser (every process CDP lists: browser, renderers, isolated frames, GPU, utility), in bytes. |
| 33 | // The bench reads it at the baseline and the end of each pass through `__twProcessMemory` (src/feed-bench.ts, measureMemory). |
| 34 | const cdp = await browser.newBrowserCDPSession(); |
| 35 | async function processMemory(): Promise<number | null> { |
| 36 | const { processInfo } = (await cdp.send('SystemInfo.getProcessInfo')) as { processInfo: { type: string; id: number }[] }; |
| 37 | if (!processInfo.length) return null; |
| 38 | const out = execFileSync('ps', ['-o', 'rss=', '-p', processInfo.map((p) => p.id).join(',')], { encoding: 'utf8' }); |
| 39 | return out.trim().split('\n').reduce((a, l) => a + (Number(l.trim()) || 0) * 1024, 0); |
| 40 | } |
| 41 | await page.exposeFunction('__twProcessMemory', () => processMemory()); |
| 42 | const url = `${origin}/?feed=1&corpus=common&hostile=none&count=${count}&bench=open&passes=${passes}&benchCount=${count}&combos=${combos}${prewarm ? '' : '&prewarm=0'}${theme === 'dark' ? '&theme=dark' : ''}`; |
| 43 | await page.goto(url); |
| 44 | await page.waitForFunction(() => typeof (window as any).__tw?.feed?.bench?.run === 'function'); |
| 45 | console.error(`running ${combos} × ${passes} passes, ${count} bubbles …`); |
| 46 | const started = Date.now(); |
| 47 | const progress = setInterval(async () => { |
| 48 | const text = await page.evaluate(() => document.querySelector('.bench-progress')?.textContent ?? '').catch(() => ''); |
| 49 | console.error(` ${Math.round((Date.now() - started) / 1000)} s: ${text}`); |
| 50 | }, 30_000); |
| 51 | await page.evaluate(() => (window as any).__tw.feed.bench.run()); |
| 52 | clearInterval(progress); |
| 53 | const json: string = await page.evaluate(() => (window as any).__tw.feed.bench.json()); |
| 54 | const md: string = await page.evaluate(() => (window as any).__tw.feed.bench.markdown()); |
| 55 | await browser.close(); |
| 56 |
| 57 | fs.writeFileSync(path.resolve(root, out + '.json'), json); |
| 58 | fs.writeFileSync(path.resolve(root, out + '.md'), md); |
| 59 | console.log(md); |
| 60 | console.error(`→ ${out}.json, ${out}.md`); |
| 61 |