// Export baseline of the TypeScript PDF engine (R4.1e, plan O12, §B.4): // each run launches a fresh browser, seeds the tab's draft with a reference // report, opens /editor, clicks « Exporter en PDF » and waits for the // download. Records time, PDF size, every PNG the browser rasteriser // encoded, the browser's summed RSS (before, peak, after), the Chromium JS // heap, the dialogs, and stopped_by_ceiling when the watchdog killed it. // // PLAYWRIGHT_NODE_MODULES=... node scripts/browser/export-baseline.mjs \ // [--engines chromium,firefox,webkit] [--out ] import { readFileSync } from 'node:fs' import { join } from 'node:path' import { pathToFileURL } from 'node:url' import { delay, firstLine, tagged, timeoutAfter, untilStopped, } from './lib/async.mjs' import { runHarnessCli } from './lib/harness.mjs' import { FIXTURES_DIR } from './lib/paths.mjs' import { probedPage, readProbe } from './lib/page-probes.mjs' import { closeEngine, launchEngine } from './lib/playwright.mjs' import { sum } from './lib/stats.mjs' const MINUTE = 60_000 export const EXPORT_REPORTS = [ { id: 'demo', file: 'report-v3.demo.json', runs: 3, timeoutMs: 5 * MINUTE }, { id: 'L', file: 'r4/reports/L.json', runs: 3, timeoutMs: 30 * MINUTE }, { id: 'CAP', file: 'r4/reports/CAP.json', runs: 1, timeoutMs: 45 * MINUTE }, ] const EXPORT_LABEL = 'Exporter en PDF' // fr.ts editor.export const CANVAS = '.svg-canvas [data-anno-id]' const LOAD_TIMEOUT_MS = 2 * MINUTE const GC_SETTLE_MS = 1000 const IDLE_SETTLE_MS = 5000 const PROGRESS_POLL_MS = 2000 /** The draft exactly as persistence.ts writes it: JSON.stringify(report). */ function draftOf(report) { const text = readFileSync(join(FIXTURES_DIR, report.file), 'utf8') return JSON.stringify(JSON.parse(text)) } async function chromiumCdp(session, context, page) { if (session.engine !== 'chromium') return null const cdp = await context.newCDPSession(page) await cdp.send('Performance.enable') return cdp } async function openEditor(session, baseUrl, report) { const context = await session.browser.newContext({ acceptDownloads: true, locale: 'fr-FR', }) const seed = { draft: draftOf(report), locale: 'fr', skipDisclaimer: true } const { page, events } = await probedPage(context, seed) const cdp = await chromiumCdp(session, context, page) await page.goto(`${baseUrl}/editor`) await page.waitForSelector(CANVAS, { timeout: LOAD_TIMEOUT_MS }) const button = page.getByRole('button', { name: EXPORT_LABEL }) await button.and(page.locator(':enabled')).waitFor() return { context, page, events, cdp, button } } async function jsHeap(cdp) { const { metrics } = await cdp.send('Performance.getMetrics') const value = (name) => metrics.find((m) => m.name === name)?.value ?? null return { used: value('JSHeapUsedSize'), total: value('JSHeapTotalSize') } } /** Settled memory: forced GC in Chromium (CDP), 5 s idle elsewhere. */ async function settledMemory(session, cdp) { if (cdp) await cdp.send('HeapProfiler.collectGarbage') await delay(cdp ? GC_SETTLE_MS : IDLE_SETTLE_MS) const rss = session.watchdog.snapshot() const heap = cdp ? await jsHeap(cdp) : null return { rssBytes: rss.currentBytes, rssContentBytes: rss.currentContentBytes, rssMaxProcessBytes: rss.currentMaxProcessBytes, jsHeapUsedBytes: heap?.used ?? null, jsHeapTotalBytes: heap?.total ?? null, } } /** * Polls how many PNG blobs the page has encoded, so a run the watchdog kills * still says how far the export got. One read at a time (the page's main * thread may be busy for seconds); stops at the first failed read. */ function trackRasters(tab) { let reading = false const read = () => { if (reading) return reading = true tab.page .evaluate(() => window.__harness.png.length) .then((count) => (tab.rastersSoFar = count)) .catch(() => clearInterval(timer)) .finally(() => (reading = false)) } const timer = setInterval(read, PROGRESS_POLL_MS) return () => clearInterval(timer) } async function clickAndDownload(tab, timeoutMs) { const download = tab.page.waitForEvent('download', { timeout: 0 }) download.catch(() => undefined) const clicked = tab.button.click().then(() => download) const stopTracking = trackRasters(tab) const end = Promise.race([ tagged('downloaded', clicked), timeoutAfter(timeoutMs), ]) end.finally(stopTracking) return end } async function downloadBytes(download) { const stream = await download.createReadStream() let bytes = 0 for await (const chunk of stream) bytes += chunk.length return bytes } /** PNG blobs; the first is the partner SVG logo (branding precedes pages). */ function rastersOf(probe) { const sizes = probe.png.map((p) => p.bytes ?? 0) const views = sizes.slice(1) return { count: sizes.length, totalBytes: sum(sizes), logoBytes: sizes[0] ?? null, views: { count: views.length, totalBytes: sum(views), minBytes: views.length ? Math.min(...views) : null, maxBytes: views.length ? Math.max(...views) : null, }, nullBlobs: probe.png.filter((p) => p.bytes === null).length, dataUrlFallbacks: probe.dataUrls, dimensions: [...new Set(probe.png.map((p) => `${p.width}x${p.height}`))], } } /** * Where the export's time went, from the page clock: before the first raster (checks, * branding), the raster phase (first `toBlob` start to the last `toBlob` end, layout and * image writing interleaved), the tail (last raster to the PDF blob: the remaining * pages, the attachment, the save or `finish()`), plus the Rust driver's own split when * the build publishes it (`faunosteo:export` User Timing: core calls, rasteriser awaits). */ function phasesOf(probe) { const starts = probe.png.map((p) => p.startedAt).filter((t) => t != null) const ends = probe.png.map((p) => p.at).filter((t) => t != null) const first = starts.length ? Math.min(...starts) : null const last = ends.length ? Math.max(...ends) : null return { headMs: elapsed(probe.clickAt, first), rasterPhaseMs: elapsed(first, last), tailMs: elapsed(last, probe.pdfBlobAt), rasterSumMs: sum( probe.png.map((p) => (p.at != null && p.startedAt != null ? p.at - p.startedAt : 0)), ), driver: probe.exportTiming, } } const elapsed = (from, to) => (from === null || to === null ? null : to - from) function peakOf(snapshot) { return { rssPeakBytes: snapshot.peakBytes, rssPeakContentBytes: snapshot.peakContentBytes, rssPeakMaxProcessBytes: snapshot.peakMaxProcessBytes, } } function eventsOf(tab) { const { dialogs, pageErrors, consoleErrors } = tab.events return { dialogs: dialogs.map((d) => d.message), pageErrors, consoleErrors } } /** The part after the download: size, alerts, settled memory, probes. */ async function afterDownload(session, tab, end) { const pdfBytes = await downloadBytes(end.value) await tab.button.and(tab.page.locator(':enabled')).waitFor() await delay(500) const peak = peakOf(session.watchdog.snapshot()) const after = await settledMemory(session, tab.cdp) const probe = await tab.page.evaluate(readProbe) return { pdfBytes, suggestedFilename: end.value.suggestedFilename(), exportMs: elapsed(probe.clickAt, probe.pdfBlobAt), rasters: rastersOf(probe), phases: phasesOf(probe), cspViolations: probe.csp, ...peak, after, } } /** A run that did not download: how far it got and what it held. */ function unfinished(session, tab, end, base) { const snapshot = session.watchdog.snapshot() return { outcome: end.kind, error: end.error ?? null, ...base, ...peakOf(snapshot), stoppedAtBytes: snapshot.stopped ? snapshot.stoppedAtBytes : null, rastersBeforeEnd: tab.rastersSoFar ?? null, ...eventsOf(tab), } } async function measureExport(session, baseUrl, report) { const tab = await openEditor(session, baseUrl, report) const before = await settledMemory(session, tab.cdp) await session.watchdog.resetPeak() const start = performance.now() const end = await untilStopped( session, clickAndDownload(tab, report.timeoutMs), ) const base = { before, clickToEndMs: end.at - start } if (end.kind !== 'downloaded') return unfinished(session, tab, end, base) const rest = await afterDownload(session, tab, end) return { outcome: 'downloaded', ...base, ...rest, ...eventsOf(tab) } } async function exportRun(pw, { engine, baseUrl, report, index }) { const session = await launchEngine(pw, engine) const head = { engine, engineVersion: session.version, report: report.id } try { const measured = await measureExport(session, baseUrl, report) return { ...head, run: index + 1, ...measured } } catch (error) { const stopped = session.watchdog.snapshot().stopped const outcome = stopped ? 'stopped_by_ceiling' : 'error' return { ...head, run: index + 1, outcome, error: firstLine(error) } } finally { await closeEngine(session) } } function runPlan(engines, baseUrl, wanted) { const reports = EXPORT_REPORTS.filter((r) => !wanted || wanted.includes(r.id)) return engines.flatMap((engine) => reports.flatMap((report) => Array.from({ length: report.runs }, (_, index) => ({ engine, baseUrl, report, index, })), ), ) } export async function runExportBaseline(context) { const { pw, baseUrl, engines, filters, log } = context const runs = [] for (const plan of runPlan(engines, baseUrl, filters.reports)) { const run = await exportRun(pw, plan) log(`export ${run.engine} ${run.report} #${run.run}: ${run.outcome}`) if (run.outcome === 'watchdog_failed') throw new Error('watchdog failed') runs.push(run) } const reports = EXPORT_REPORTS.map(({ id, file, runs: n }) => ({ id, file: `apps/app/tests/fixtures/${file}`, runs: n, })) return { locale: 'fr', reports, runs } } if (import.meta.url === pathToFileURL(process.argv[1]).href) { await runHarnessCli({ export: runExportBaseline }) }