test(tests): R4.1e browser harness and TS baselines (Chromium, Firefox, WebKit)

Playwright harness under scripts/browser: 127.0.0.1 server with the DEPLOY
CSP, export and import baselines of the TypeScript engine under a 4 GiB RSS
watchdog (not a guaranteed memory limit), O9 browser columns in
inflate.browsers.json (26 rows agree with Node in all three engines).
Results of 2026-10-18 committed; the O9 Node test no longer expects browser
columns in inflate.json.
This commit is contained in:
bchanot committed 2026-10-18 15:05:28 +02:00
1 parent 4bdfda59ba
commit 4439fd2420
23 files changed
+20903 -18

No files matched your search

@@ -0,0 +1,414 @@
{
"oracle": "O9 browser columns",
"source": "crates/core/tests/oracles/r4/inflate.json",
"harness": "scripts/browser/inflate-verdicts.mjs",
"function": "inflateFlate(bytes, limit) of packages/pdf/src/attachment/inflate.ts, run in each engine on the file bytes",
"page": "Home of the build served by serve.mjs (DEPLOY.md CSP)",
"watchdog": {
"ceilingBytes": 4294967296,
"sampleIntervalMs": 50,
"measure": "summed VmRSS (/proc/<pid>/status) of the browser process tree; content = renderer / Web Content / WPEWebProcess processes only",
"onCeiling": "SIGKILL of the tree, run recorded as stopped_by_ceiling",
"caveat": "The 4 GiB RSS watchdog is not a guaranteed memory limit: it samples every 50 ms, so a faster allocation can overshoot it before the kill; summed VmRSS counts shared pages once per process; a process outside the browser's tree is not seen."
},
"engines": {
"node": {
"version": "v24.21.0",
"zlib": "1.3.2.1-motley-8002e91"
},
"chromium": {
"version": "156.0.8078.4"
},
"firefox": {
"version": "157.0"
},
"webkit": {
"version": "27.2"
}
},
"rows": [
{
"file": "valid-noise-64kib.bin",
"case": "valid zlib stream, default cap",
"limit": 16777216,
"verdicts": {
"node": {
"length": 65536,
"sha256": "64265a3bb340bdab805c8bf11283bfd195100761700ce69064f2823dbd079819"
},
"chromium": {
"length": 65536,
"sha256": "64265a3bb340bdab805c8bf11283bfd195100761700ce69064f2823dbd079819"
},
"firefox": {
"length": 65536,
"sha256": "64265a3bb340bdab805c8bf11283bfd195100761700ce69064f2823dbd079819"
},
"webkit": {
"length": 65536,
"sha256": "64265a3bb340bdab805c8bf11283bfd195100761700ce69064f2823dbd079819"
}
}
},
{
"file": "valid-noise-64kib.bin",
"case": "output exactly at the limit",
"limit": 65536,
"verdicts": {
"node": {
"length": 65536,
"sha256": "64265a3bb340bdab805c8bf11283bfd195100761700ce69064f2823dbd079819"
},
"chromium": {
"length": 65536,
"sha256": "64265a3bb340bdab805c8bf11283bfd195100761700ce69064f2823dbd079819"
},
"firefox": {
"length": 65536,
"sha256": "64265a3bb340bdab805c8bf11283bfd195100761700ce69064f2823dbd079819"
},
"webkit": {
"length": 65536,
"sha256": "64265a3bb340bdab805c8bf11283bfd195100761700ce69064f2823dbd079819"
}
}
},
{
"file": "valid-noise-64kib.bin",
"case": "output one byte over the limit",
"limit": 65535,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "valid-json.bin",
"case": "valid zlib stream, small JSON",
"limit": 16777216,
"verdicts": {
"node": {
"length": 37,
"sha256": "ec4dd986156adf0e3c47f3d80718a2f56302b837fcea53dff3bf0f231058d396"
},
"chromium": {
"length": 37,
"sha256": "ec4dd986156adf0e3c47f3d80718a2f56302b837fcea53dff3bf0f231058d396"
},
"firefox": {
"length": 37,
"sha256": "ec4dd986156adf0e3c47f3d80718a2f56302b837fcea53dff3bf0f231058d396"
},
"webkit": {
"length": 37,
"sha256": "ec4dd986156adf0e3c47f3d80718a2f56302b837fcea53dff3bf0f231058d396"
}
}
},
{
"file": "empty-input.bin",
"case": "empty input (0 bytes)",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "empty-zlib-stream.bin",
"case": "empty zlib stream (no output)",
"limit": 16777216,
"verdicts": {
"node": {
"length": 0,
"sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
},
"chromium": {
"length": 0,
"sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
},
"firefox": {
"length": 0,
"sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
},
"webkit": {
"length": 0,
"sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
}
}
},
{
"file": "truncated.bin",
"case": "truncated stream (first 2 KiB of a 4 KiB noise stream)",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "truncated-header-only.bin",
"case": "the 2-byte zlib header alone",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "missing-adler.bin",
"case": "complete deflate data, Adler-32 missing",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "partial-adler.bin",
"case": "complete deflate data, 2 of the 4 Adler-32 bytes",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "bad-adler.bin",
"case": "bad Adler-32 (last byte flipped)",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "trailing-bytes.bin",
"case": "trailing bytes after the stream end",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "trailing-zero-byte.bin",
"case": "one 0x00 byte after the stream end",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "two-streams.bin",
"case": "two complete zlib streams back to back",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "raw-deflate.bin",
"case": "raw deflate without zlib header",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "gzip.bin",
"case": "gzip header and trailer",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "preset-dictionary.bin",
"case": "preset dictionary flag (FDICT) set",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "bad-header-check.bin",
"case": "zlib header whose FCHECK fails",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "small-window.bin",
"case": "window size below 32 KiB (CINFO 1, windowBits 9)",
"limit": 16777216,
"verdicts": {
"node": {
"length": 37,
"sha256": "ec4dd986156adf0e3c47f3d80718a2f56302b837fcea53dff3bf0f231058d396"
},
"chromium": {
"length": 37,
"sha256": "ec4dd986156adf0e3c47f3d80718a2f56302b837fcea53dff3bf0f231058d396"
},
"firefox": {
"length": 37,
"sha256": "ec4dd986156adf0e3c47f3d80718a2f56302b837fcea53dff3bf0f231058d396"
},
"webkit": {
"length": 37,
"sha256": "ec4dd986156adf0e3c47f3d80718a2f56302b837fcea53dff3bf0f231058d396"
}
}
},
{
"file": "stored-blocks.bin",
"case": "stored blocks: two non-final ones (one empty), then a final one",
"limit": 16777216,
"verdicts": {
"node": {
"length": 8,
"sha256": "9c56cc51b374c3ba189210d5b6d4bf57790d351c96c47c02190ecf1e430635ab"
},
"chromium": {
"length": 8,
"sha256": "9c56cc51b374c3ba189210d5b6d4bf57790d351c96c47c02190ecf1e430635ab"
},
"firefox": {
"length": 8,
"sha256": "9c56cc51b374c3ba189210d5b6d4bf57790d351c96c47c02190ecf1e430635ab"
},
"webkit": {
"length": 8,
"sha256": "9c56cc51b374c3ba189210d5b6d4bf57790d351c96c47c02190ecf1e430635ab"
}
}
},
{
"file": "stored-bad-nlen.bin",
"case": "stored block whose NLEN is not the complement of LEN",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "multiple-blocks.bin",
"case": "several compressed blocks separated by full flushes",
"limit": 16777216,
"verdicts": {
"node": {
"length": 334,
"sha256": "f5d7c051797254d52e80088055482e0d69e9ca1d4a6ff1223aa7b5b16a813770"
},
"chromium": {
"length": 334,
"sha256": "f5d7c051797254d52e80088055482e0d69e9ca1d4a6ff1223aa7b5b16a813770"
},
"firefox": {
"length": 334,
"sha256": "f5d7c051797254d52e80088055482e0d69e9ca1d4a6ff1223aa7b5b16a813770"
},
"webkit": {
"length": 334,
"sha256": "f5d7c051797254d52e80088055482e0d69e9ca1d4a6ff1223aa7b5b16a813770"
}
}
},
{
"file": "corrupt-body.bin",
"case": "corrupt body (every byte after the ninth flipped)",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "not-deflate.bin",
"case": "not deflate at all (JSON text)",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
},
{
"file": "output-exactly-16mib.bin",
"case": "output of exactly 16 MiB (the default cap)",
"limit": 16777216,
"verdicts": {
"node": {
"length": 16777216,
"sha256": "080acf35a507ac9849cfcba47dc2ad83e01b75663a516279c8b9d243b719643e"
},
"chromium": {
"length": 16777216,
"sha256": "080acf35a507ac9849cfcba47dc2ad83e01b75663a516279c8b9d243b719643e"
},
"firefox": {
"length": 16777216,
"sha256": "080acf35a507ac9849cfcba47dc2ad83e01b75663a516279c8b9d243b719643e"
},
"webkit": {
"length": 16777216,
"sha256": "080acf35a507ac9849cfcba47dc2ad83e01b75663a516279c8b9d243b719643e"
}
}
},
{
"file": "bomb-16mib-plus-1.bin",
"case": "output of 16 MiB + 1 (one byte over the default cap)",
"limit": 16777216,
"verdicts": {
"node": null,
"chromium": null,
"firefox": null,
"webkit": null
}
}
],
"disagreements": []
}
+5 -18
View File
@@ -4,13 +4,11 @@
* fed in 16 KiB awaited slices, null on any failure or past the limit) on
* the zlib framing cases, inputs under `apps/app/tests/fixtures/pdf/r4/
* inflate/`, rows in `crates/core/tests/oracles/r4/inflate.json`. Each row
* holds one verdict per engine: this test owns `verdicts.node` and
* `engines.node`; the browser harness adds its engines' columns to the same
* rows from the same files, and the check here ignores them.
* `MAKE_R4_ORACLES=1` writes the inputs and the Node column; it refuses to
* overwrite a file that already holds browser columns.
* holds the Node verdict; the browser harness (`scripts/browser/
* inflate-verdicts.mjs`) runs the same function on the same files in each
* engine and writes its columns next to Node's in `inflate.browsers.json`.
* `MAKE_R4_ORACLES=1` writes the inputs and this file.
*/
import { existsSync } from 'node:fs'
import { resolve } from 'node:path'
import {
constants,
@@ -19,16 +17,14 @@ import {
deflateSync,
gzipSync,
} from 'node:zlib'
import { describe, expect, it } from 'vitest'
import { describe, it } from 'vitest'
import { MAX_ATTACHMENT_BYTES } from '../src'
import { ascii, concat, deflate, KiB, noise } from './helpers/r4-reader-craft'
import {
expectOnlyFiles,
inflateVerdict,
MAKE,
R4_INPUTS_DIR,
R4_ORACLES_DIR,
readJsonFile,
sha256,
storeFile,
storeJson,
@@ -325,17 +321,8 @@ function nodeColumn(committed: unknown): unknown {
}
}
function refuseToDropBrowserColumns(): void {
if (!existsSync(OUT)) return
const engines = Object.keys(readJsonFile<InflateOracle>(OUT).engines)
expect(engines, 'browser columns would be lost: remove them first').toEqual([
'node',
])
}
describe('R4 inflate oracles O9 (TypeScript freeze, Node)', () => {
it('matches the frozen inputs and Node verdicts', async () => {
if (MAKE) refuseToDropBrowserColumns()
const oracle = await build()
storeJson(OUT, oracle, nodeColumn)
expectOnlyFiles(
+160
View File
@@ -0,0 +1,160 @@
# Browser harness (R4.1e)
Measures the application in real browser engines (Chromium, Firefox, WebKit)
through Playwright: the TypeScript baselines of R4 (export and import time
and memory, plan `.claude/tasks/plans/2026-10-17-rust-r4-pdf-core.md` O12,
§B.3, §B.4, §B.8) and the browser columns of the O9 inflate oracle. Later
slices reuse the server for the CSP and worker proofs.
> **Watchdog caveat.** Every browser runs under a 4 GiB RSS watchdog that is
> **not a guaranteed memory limit**. It samples every 50 ms, so a faster
> allocation can overshoot the ceiling before the kill; summed VmRSS counts
> shared pages once per process; a process outside the browser's tree is not
> seen. A run it stops is recorded as `stopped_by_ceiling`, which is a
> result, not a harness failure.
## Prerequisites
- Linux (the watchdog reads `/proc`), Node 24.
- A production build that embeds the reference sheet. The scripts check it
and refuse to run without it; they never build by themselves:
```bash
SHEET_BUNDLE=draft corepack pnpm --filter @faunosteo/app build
```
- Playwright outside the repository (no project dependency): set
`PLAYWRIGHT_NODE_MODULES` to a `node_modules` that holds `playwright-core`,
with its browsers installed in `~/.cache/ms-playwright`. On the
development machine:
```bash
export PLAYWRIGHT_NODE_MODULES=/home/bchanot/.local/lib/node_modules/gsd-pi/node_modules
```
(playwright-core 1.64.0: Chromium headless shell 156, Firefox 157,
WebKit 27.2, all headless.)
## Commands
All from the repository root.
| Command | What it does |
| -------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| `node scripts/browser/serve.mjs [--port 4173] [--csp with\|without]` | serves `apps/app/dist` on `127.0.0.1` until Ctrl-C |
| `node scripts/browser/ts-baseline.mjs` | export then import baseline, one file `results/<date>-ts-baseline.json`, tables on stdout |
| `node scripts/browser/export-baseline.mjs` | export baseline only (`results/<date>-export.json`) |
| `node scripts/browser/import-baseline.mjs` | import baseline only (`results/<date>-import.json`) |
| `node scripts/browser/inflate-verdicts.mjs` | O9 browser columns → `crates/core/tests/oracles/r4/inflate.browsers.json` |
| `node scripts/browser/summarize.mjs <results.json>` | prints the markdown tables of a results file |
Options of the baseline scripts: `--engines chromium,firefox,webkit` (default
all three), `--out <file>`, `--reports demo,L,CAP` (export runs to keep),
`--groups base,r3,step6,tolerance,read` (import groups to keep). Progress goes
to stderr, the tables to stdout.
## The server (`serve.mjs`)
- `apps/app/dist` with the SPA fallback of `docker/nginx.conf`: an existing
file is served, a missing `/assets/` file is a 404, any other path gets
`index.html`. Binds `127.0.0.1` only; the LAN DEV origin is unaffected.
- `.wasm` as `application/wasm` (uncompressed; production serves the
`.wasm.gz` sibling with `gzip_static`).
- The `Content-Security-Policy` of DEPLOY.md (the nginx `add_header` line,
read at start) on every response, with `'wasm-unsafe-eval'` added to
`script-src` as DEPLOY.md requires for R2+ images; `--csp without` serves
the header without the keyword.
- `/env.js` is the text `docker/entrypoint.sh` writes when no `APP_*`
variable is set: the local tool, no Supabase (the build's stub `env.js`
is not served).
## What each script measures
Every run records the engine version; every results file records the host
(CPU, RAM, MemAvailable at start and end), the build (mtime, chunk sizes,
HEAD), the served CSP and the watchdog facts with its caveat.
Memory comes from the watchdog's samples of the browser's process tree:
summed VmRSS of all its processes, the content processes alone (Chromium
renderers, Firefox Web Content, WebKit `WPEWebProcess`), and the largest
single process.
### Export (`export-baseline.mjs`)
Reports: demo (`report-v3.demo.json`, 3 runs), L and CAP
(`tests/fixtures/r4/reports/`, 3 runs and 1 run), locale fr, each run in a
fresh browser and context. Before navigation the tab gets `osteo:draft`
exactly as `persistence.ts` writes it (`JSON.stringify(report)`),
`osteo:disclaimer-shown` (EditorRoute's first-run flag) and `osteo:locale`
`fr`. The harness opens `/editor`, waits for the canvas and clicks
« Exporter en PDF ».
- `exportMs`: page clock, from the click to `URL.createObjectURL` of the PDF
Blob (download.ts); `clickToEndMs`: Node clock, click to the download
event.
- `pdfBytes`, the suggested file name.
- `rasters`: every PNG Blob `HTMLCanvasElement.prototype.toBlob` produced
(wrapped, calls through): count, sizes; the first is the partner SVG logo,
the others are the views; `dataUrlFallbacks` counts `toDataURL` calls.
- RSS before (settled), peak (click to the end of the export), after
(settled). Settled = after a forced GC (CDP `HeapProfiler.collectGarbage`)
and 1 s in Chromium, after 5 s idle in Firefox and WebKit. The PDF Blob is
still referenced by its object URL for 30 s after the download
(download.ts), so "after" includes it.
- Chromium JS heap (CDP `Performance.getMetrics`), before and after.
- Every dialog (accepted), page errors, console errors, CSP violations.
- `outcome`: `downloaded`, `stopped_by_ceiling`, `timeout` (5 min demo,
30 min L, 45 min CAP), `error`. A run that did not download keeps its
peak, `stoppedAtBytes` (summed RSS when the watchdog fired) and
`rastersBeforeEnd` (PNG blobs encoded so far, read every 2 s).
### Import (`import-baseline.mjs`)
One browser per engine (relaunched after a stop), a fresh context per file.
The harness opens `/`, waits for « Choisir un fichier » to be enabled and
sets the file on the import input. The run ends at the first of: navigation
to `/editor`, an alert, the inline import error (`timeout` after 120 s).
Files: `report-v2.full.pdf`, `report-v2.minimal.pdf`, `report-v3.bare.pdf`,
every PDF under `pdf/r3/` (recursively), `pdf/step6/`, `pdf/r4/tolerance/`,
and the hostile inputs of `pdf/r4/read/` as `crates/core/tests/oracles/r4/
read.json` lists them (inputs listed there but absent are reported as
missing). A `<slug>.gen.json` description is expanded (checked against its
length and digest) to a temporary `<slug>.pdf`, removed after its run.
- `durationMs`: Node clock, from setting the file to the first end;
`pageMs`: page clock, from the input's `change` event to the first alert
or navigation.
- `outcome` (`editor`, `error`, `dialog`, `none`, `timeout`,
`stopped_by_ceiling`), the alert texts, the inline error text, the final
path.
- RSS (summed and content) before the file is set and peak until the first
end.
- Chromium only: main-thread long tasks during the import
(`PerformanceObserver('longtask')`): count, longest, total. Firefox and
WebKit do not expose the entry type (`null`).
### O9 inflate verdicts (`inflate-verdicts.mjs`)
Reads `crates/core/tests/oracles/r4/inflate.json` and its inputs under
`apps/app/tests/fixtures/pdf/r4/inflate/` (each checked against the recorded
length and sha256), runs in each engine, on the application's Home page, a
copy of `inflateFlate` from `packages/pdf/src/attachment/inflate.ts`
(`DecompressionStream('deflate')`, 16 KiB awaited slices, concurrent drain,
null past the row's limit or on any error), and writes
`inflate.browsers.json`: per row, Node's verdict copied from `inflate.json`
and each engine's (`null` or `{ length, sha256 }`), plus the rows where an
engine disagrees with Node.
## Layout
```
scripts/browser/
├─ serve.mjs, ts-baseline.mjs, export-baseline.mjs, import-baseline.mjs
├─ inflate-verdicts.mjs, summarize.mjs
├─ lib/ playwright (load, launch under the watchdog, close), watchdog + its
│ sampler thread, proc (/proc reads), page-probes (init scripts),
│ harness (CLI, results file), summary (tables), app-build, host,
│ inputs, stats, async, paths
└─ results/ raw results files
```
+259
View File
@@ -0,0 +1,259 @@
// 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 <file>]
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}`))],
}
}
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),
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 })
}
+272
View File
@@ -0,0 +1,272 @@
// Import baseline of the TypeScript reader (R4.1e, plan O12, §B.3): for each
// file, a fresh context opens Home and sets the file on the import input;
// the run ends at the first of: navigation to /editor, an alert, the inline
// import error. Records the duration, the alert and error texts, the final
// URL, the browser's summed RSS (before, peak) and, in Chromium, the main
// thread's long tasks. One browser per engine, relaunched after a stop.
//
// PLAYWRIGHT_NODE_MODULES=... node scripts/browser/import-baseline.mjs \
// [--engines chromium,firefox,webkit] [--out <file>]
import {
existsSync,
mkdtempSync,
readFileSync,
rmdirSync,
unlinkSync,
writeFileSync,
} from 'node:fs'
import { tmpdir } from 'node:os'
import { basename, join } from 'node:path'
import { pathToFileURL } from 'node:url'
import {
delay,
firstLine,
isStop,
tagged,
timeoutAfter,
untilStopped,
} from './lib/async.mjs'
import { runHarnessCli } from './lib/harness.mjs'
import { expandGenerator, filesUnder, isGenerator } from './lib/inputs.mjs'
import { FIXTURES_DIR, ORACLES_R4_DIR, relativeToRepo } 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 IMPORT_TIMEOUT_MS = 120_000
const SETTLE_MS = 5000
const PICK_LABEL = 'Choisir un fichier' // fr.ts home.import.cta
const IMPORT_ERROR = 'input[type="file"] ~ p.text-red-700'
const READ_DIR = join(FIXTURES_DIR, 'pdf/r4/read')
const isPdf = (path) => path.endsWith('.pdf')
const BASE_FILES = [
'report-v2.full.pdf',
'report-v2.minimal.pdf',
'report-v3.bare.pdf',
]
function pdfsIn(group, dir, recursive = false) {
const paths = filesUnder(join(FIXTURES_DIR, dir), recursive).filter(isPdf)
return paths.map((path) => ({ group, path }))
}
function fixedGroups() {
return [
...BASE_FILES.map((f) => ({ group: 'base', path: join(FIXTURES_DIR, f) })),
...pdfsIn('r3', 'pdf/r3', true),
...pdfsIn('step6', 'pdf/step6'),
...pdfsIn('tolerance', 'pdf/r4/tolerance'),
]
}
/** The hostile set as read.json (O7) lists it; absent listed files reported. */
function hostileGroup() {
const oraclePath = join(ORACLES_R4_DIR, 'read.json')
if (!existsSync(READ_DIR)) {
return { inputs: [], missing: ['pdf/r4/read/'], listedBy: null }
}
const listed = existsSync(oraclePath)
? JSON.parse(readFileSync(oraclePath, 'utf8')).inputs
: filesUnder(READ_DIR).map((path) => ({ file: basename(path), kind: null }))
const present = listed.filter((i) => existsSync(join(READ_DIR, i.file)))
const missing = listed.filter((i) => !present.includes(i)).map((i) => i.file)
const inputs = present.map((i) => ({
group: 'read',
kind: i.kind,
path: join(READ_DIR, i.file),
}))
return {
inputs,
missing,
listedBy: existsSync(oraclePath) ? relativeToRepo(oraclePath) : 'directory',
}
}
/** A generator description becomes a temporary file, removed after the run. */
function materialise(input, scratch) {
if (!isGenerator(input.path)) {
return { path: input.path, cleanup: () => undefined }
}
const path = join(scratch, `${basename(input.path, '.gen.json')}.pdf`)
writeFileSync(path, expandGenerator(input.path))
return { path, cleanup: () => unlinkSync(path) }
}
async function openHome(session, baseUrl) {
const context = await session.browser.newContext({ locale: 'fr-FR' })
const seed = { draft: null, locale: 'fr', skipDisclaimer: false }
const { page, events } = await probedPage(context, seed)
await page.goto(`${baseUrl}/`)
const pick = page.getByRole('button', { name: PICK_LABEL })
await pick.and(page.locator(':enabled')).waitFor()
return { context, page, events }
}
/** The first end of an import; the waits start before the file is set. */
function firstEnd(tab, path) {
const { page, events } = tab
const ends = Promise.race([
tagged('editor', page.waitForURL('**/editor', { timeout: 0 })),
tagged('dialog', events.firstDialog),
tagged('error_text', page.locator(IMPORT_ERROR).waitFor({ timeout: 0 })),
timeoutAfter(IMPORT_TIMEOUT_MS),
])
const set = page.setInputFiles('input[type="file"]', path)
return tagged('set', set).then((r) => (r.kind === 'error' ? r : ends))
}
/** After an alert the app may still navigate or show its error. */
async function settle(tab, first) {
if (first.kind !== 'dialog') return delay(300)
const { page } = tab
await Promise.race([
tagged('editor', page.waitForURL('**/editor', { timeout: SETTLE_MS })),
tagged(
'error_text',
page.locator(IMPORT_ERROR).waitFor({ timeout: SETTLE_MS }),
),
])
}
function longTasksOf(probe, since) {
if (!probe.longTaskSupported) return null
const tasks = probe.longTasks.filter((t) => t.start >= since).map((t) => t.ms)
return {
count: tasks.length,
maxMs: tasks.length ? Math.max(...tasks) : 0,
totalMs: sum(tasks),
}
}
/** Page clock: the input's change event to the first alert or navigation. */
function pageMsOf(probe) {
const ends = [probe.alertAt, probe.navAt].filter((t) => t !== null)
if (probe.changeAt === null || ends.length === 0) return null
return Math.min(...ends) - probe.changeAt
}
async function finalState(tab, since) {
const { page, events } = tab
const errorText = await page
.locator(IMPORT_ERROR)
.textContent({ timeout: 300 })
.catch(() => null)
const probe = await page.evaluate(readProbe)
return {
finalPath: new URL(page.url()).pathname,
errorText,
dialogs: events.dialogs.map((d) => d.message),
pageMs: pageMsOf(probe),
longTasks: longTasksOf(probe, since),
cspViolations: probe.csp,
pageErrors: events.pageErrors,
}
}
function resultOf(final) {
if (final.finalPath === '/editor') return 'editor'
if (final.errorText) return 'error'
return final.dialogs.length ? 'dialog' : 'none'
}
/** Before the file was set, and the peak until the import's first end. */
function rssOf(before, after) {
return {
rssBeforeBytes: before.currentBytes,
rssBeforeContentBytes: before.currentContentBytes,
rssPeakBytes: after.peakBytes,
rssPeakContentBytes: after.peakContentBytes,
}
}
async function measureImport(session, tab, path) {
const since = await tab.page.evaluate(() => performance.now())
const before = session.watchdog.snapshot()
await session.watchdog.resetPeak()
const start = performance.now()
const first = await untilStopped(session, firstEnd(tab, path))
const rss = rssOf(before, session.watchdog.snapshot())
const head = { firstEnd: first.kind, durationMs: first.at - start, ...rss }
if (isStop(first) || first.kind === 'timeout' || first.kind === 'error') {
return { outcome: first.kind, error: first.error ?? null, ...head }
}
await settle(tab, first)
const final = await finalState(tab, since)
return { outcome: resultOf(final), ...head, ...final }
}
async function importRun(session, baseUrl, input, scratch) {
const file = materialise(input, scratch)
const head = {
group: input.group,
kind: input.kind ?? null,
file: relativeToRepo(input.path),
}
let tab = null
try {
tab = await openHome(session, baseUrl)
return { ...head, ...(await measureImport(session, tab, file.path)) }
} catch (error) {
const outcome = session.watchdog.snapshot().stopped
? 'stopped_by_ceiling'
: 'error'
return { ...head, outcome, error: firstLine(error) }
} finally {
await tab?.context.close().catch(() => undefined)
file.cleanup()
}
}
async function importEngine(pw, { engine, baseUrl, inputs, scratch, log }) {
let session = await launchEngine(pw, engine)
const runs = []
try {
for (const input of inputs) {
const run = await importRun(session, baseUrl, input, scratch)
runs.push({ engine, engineVersion: session.version, ...run })
const ms = Math.round(run.durationMs ?? 0)
log(`import ${engine} ${run.file}: ${run.outcome} ${ms} ms`)
if (run.outcome === 'watchdog_failed') throw new Error('watchdog failed')
if (run.outcome !== 'stopped_by_ceiling') continue
await closeEngine(session)
session = await launchEngine(pw, engine)
}
} finally {
await closeEngine(session)
}
return runs
}
export async function runImportBaseline(context) {
const { pw, baseUrl, engines, filters, log } = context
const hostile = hostileGroup()
const wanted = (i) => !filters.groups || filters.groups.includes(i.group)
const inputs = [...fixedGroups(), ...hostile.inputs].filter(wanted)
const scratch = mkdtempSync(join(tmpdir(), 'faunosteo-import-'))
const runs = []
try {
for (const engine of engines) {
runs.push(
...(await importEngine(pw, { engine, baseUrl, inputs, scratch, log })),
)
}
} finally {
rmdirSync(scratch)
}
return {
timeoutMs: IMPORT_TIMEOUT_MS,
files: inputs.length,
hostile: {
listedBy: hostile.listedBy,
present: hostile.inputs.length,
missing: hostile.missing,
},
runs,
}
}
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
await runHarnessCli({ import: runImportBaseline })
}
+216
View File
@@ -0,0 +1,216 @@
// O9 browser verdicts (R4.1e, plan §B.1.2 "Inflation", §B.5.1): runs, in
// each engine, the algorithm of packages/pdf/src/attachment/inflate.ts
// `inflateFlate` (copied into the page below) on every row of
// crates/core/tests/oracles/r4/inflate.json, with the row's limit, on the
// input bytes of apps/app/tests/fixtures/pdf/r4/inflate/. Writes
// crates/core/tests/oracles/r4/inflate.browsers.json (per row: Node's
// verdict from inflate.json and each engine's, null or { length, sha256 })
// and lists every row where an engine disagrees with Node.
//
// PLAYWRIGHT_NODE_MODULES=... node scripts/browser/inflate-verdicts.mjs \
// [--engines chromium,firefox,webkit]
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { parseArgs } from 'node:util'
import { untilStopped } from './lib/async.mjs'
import { checkBuild } from './lib/app-build.mjs'
import { sha256 } from './lib/inputs.mjs'
import { ORACLES_R4_DIR, REPO_ROOT, relativeToRepo } from './lib/paths.mjs'
import {
ENGINES,
closeEngine,
launchEngine,
loadPlaywright,
} from './lib/playwright.mjs'
import { watchdogFacts } from './lib/watchdog.mjs'
import { startServer } from './serve.mjs'
const ORACLE = join(ORACLES_R4_DIR, 'inflate.json')
const OUT = join(ORACLES_R4_DIR, 'inflate.browsers.json')
/**
* inflate.ts inflateFlate, verbatim in behaviour: DecompressionStream
* ('deflate'), 16 KiB slices each awaited, the drain concurrent with the
* feed, null past `limit` (both sides torn down) or on any stream error.
* Self-contained: Playwright serialises it into the page.
*/
async function inflateInPage({ base64, limit }) {
const WRITE_SLICE_BYTES = 16 * 1024
const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0))
function concat(chunks, total) {
const out = new Uint8Array(total)
let offset = 0
for (const chunk of chunks) {
out.set(chunk, offset)
offset += chunk.byteLength
}
return out
}
async function feedSlices(writer, source) {
for (let at = 0; at < source.byteLength; at += WRITE_SLICE_BYTES) {
await writer.write(source.subarray(at, at + WRITE_SLICE_BYTES))
}
await writer.close()
}
async function drainUnder(reader) {
const chunks = []
let total = 0
for (;;) {
const { done, value } = await reader.read()
if (done) return concat(chunks, total)
total += value.byteLength
if (total > limit) return null
chunks.push(value)
}
}
async function inflateFlate() {
if (typeof DecompressionStream === 'undefined') return null
const inflater = new DecompressionStream('deflate')
const writer = inflater.writable.getWriter()
const reader = inflater.readable.getReader()
const fed = feedSlices(writer, bytes)
fed.catch(() => undefined)
try {
const out = await drainUnder(reader)
if (out === null) {
await Promise.allSettled([reader.cancel(), writer.abort()])
}
return out
} catch {
return null
}
}
const out = await inflateFlate()
if (out === null) return null
const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', out))
const hex = [...digest].map((b) => b.toString(16).padStart(2, '0')).join('')
return { length: out.byteLength, sha256: hex }
}
/** inflate.json and its inputs, each checked against the recorded digest. */
function loadOracle() {
if (!existsSync(ORACLE)) {
throw new Error(
`${relativeToRepo(ORACLE)} missing: run the O9 generator first`,
)
}
const oracle = JSON.parse(readFileSync(ORACLE, 'utf8'))
const dir = join(REPO_ROOT, oracle.inputsDir)
const inputs = new Map()
for (const input of oracle.inputs) {
const bytes = readFileSync(join(dir, input.file))
if (bytes.length !== input.length || sha256(bytes) !== input.sha256) {
throw new Error(`${input.file}: bytes differ from inflate.json`)
}
inputs.set(input.file, bytes.toString('base64'))
}
return { oracle, inputs }
}
async function engineVerdicts(pw, engine, baseUrl, { oracle, inputs }) {
const session = await launchEngine(pw, engine)
try {
const page = await session.browser.newPage()
await page.goto(`${baseUrl}/`)
const verdicts = []
for (const row of oracle.rows) {
const arg = { base64: inputs.get(row.file), limit: row.limit }
const end = await untilStopped(
session,
page
.evaluate(inflateInPage, arg)
.then((value) => ({ kind: 'ok', value })),
)
if (end.kind !== 'ok')
throw new Error(`${engine} ${row.file}: ${end.kind}`)
verdicts.push(end.value)
}
return { version: session.version, verdicts }
} finally {
await closeEngine(session)
}
}
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b)
function disagreements(rows, engines) {
return rows.flatMap((row) =>
engines
.filter((engine) => !same(row.verdicts[engine], row.verdicts.node))
.map((engine) => ({
file: row.file,
case: row.case,
limit: row.limit,
engine,
node: row.verdicts.node,
browser: row.verdicts[engine],
})),
)
}
function resultOf(oracle, engines, columns) {
const rows = oracle.rows.map((row, i) => ({
file: row.file,
case: row.case,
limit: row.limit,
verdicts: {
node: row.verdicts.node,
...Object.fromEntries(engines.map((e) => [e, columns[e].verdicts[i]])),
},
}))
const versions = engines.map((e) => [e, { version: columns[e].version }])
return {
oracle: 'O9 browser columns',
source: relativeToRepo(ORACLE),
harness: 'scripts/browser/inflate-verdicts.mjs',
function: oracle.function,
page: 'Home of the build served by serve.mjs (DEPLOY.md CSP)',
watchdog: watchdogFacts(),
engines: { node: oracle.engines.node, ...Object.fromEntries(versions) },
rows,
disagreements: disagreements(rows, engines),
}
}
async function allColumns(engines, loaded) {
const { pw } = loadPlaywright()
const server = await startServer()
const columns = {}
try {
for (const engine of engines) {
columns[engine] = await engineVerdicts(pw, engine, server.url, loaded)
}
} finally {
server.close()
}
return columns
}
function report(result) {
const engines = Object.keys(result.engines).filter((e) => e !== 'node')
console.log(
`${relativeToRepo(OUT)}: ${result.rows.length} rows, ${engines.join(', ')}`,
)
for (const d of result.disagreements) {
const node = JSON.stringify(d.node)
const browser = JSON.stringify(d.browser)
console.log(`DIFF ${d.engine} ${d.file} ${d.limit}: ${node} / ${browser}`)
}
if (result.disagreements.length === 0)
console.log('every engine agrees with Node')
}
async function main() {
const { values } = parseArgs({
options: { engines: { type: 'string', default: ENGINES.join(',') } },
})
const engines = values.engines.split(',').filter(Boolean)
const loaded = loadOracle()
checkBuild()
const columns = await allColumns(engines, loaded)
const result = resultOf(loaded.oracle, engines, columns)
writeFileSync(OUT, `${JSON.stringify(result, null, 2)}\n`)
report(result)
}
await main()
+49
View File
@@ -0,0 +1,49 @@
import { execFileSync } from 'node:child_process'
import { existsSync, readdirSync, statSync } from 'node:fs'
import { join } from 'node:path'
import { DIST_DIR, REPO_ROOT } from './paths.mjs'
/**
* The harness measures a production build that embeds the reference sheet.
* It never builds by itself: the build command is printed instead, so a run
* always names the build it measured (mtime, chunk sizes, HEAD).
*/
export const BUILD_COMMAND =
'SHEET_BUNDLE=draft corepack pnpm --filter @faunosteo/app build'
// A build without the sheet (SHEET_BUNDLE=none) leaves a 40-byte stub chunk.
const MIN_SHEET_CHUNK_BYTES = 10_000
function assetSize(pattern) {
const assets = join(DIST_DIR, 'assets')
const name = readdirSync(assets).find((file) => pattern.test(file))
return name ? { name, bytes: statSync(join(assets, name)).size } : null
}
function headCommit() {
try {
const args = ['-C', REPO_ROOT, 'rev-parse', '--short', 'HEAD']
return execFileSync('git', args, { encoding: 'utf8' }).trim()
} catch {
return null
}
}
export function checkBuild() {
const index = join(DIST_DIR, 'index.html')
if (!existsSync(index)) {
throw new Error(`no build in apps/app/dist: run ${BUILD_COMMAND}`)
}
const sheet = assetSize(/^_virtual_faunosteo-sheet-.*\.js$/)
if (!sheet || sheet.bytes < MIN_SHEET_CHUNK_BYTES) {
throw new Error(`the build carries no sheet: run ${BUILD_COMMAND}`)
}
return {
distDir: 'apps/app/dist',
builtAt: statSync(index).mtime.toISOString(),
sheetChunk: sheet,
entryChunk: assetSize(/^index-.*\.js$/),
wasm: assetSize(/\.wasm$/),
headCommit: headCommit(),
}
}
+41
View File
@@ -0,0 +1,41 @@
/** Promise helpers for races whose losers must never surface as unhandled. */
export const delay = (ms) => new Promise((done) => setTimeout(done, ms))
export const firstLine = (error) =>
String(error?.message ?? error).split('\n')[0]
/** `{ kind, value, at }` once `promise` resolves; rejected: kind 'error'. */
export function tagged(kind, promise) {
return promise.then(
(value) => ({ kind, value, at: performance.now() }),
(error) => ({
kind: 'error',
error: firstLine(error),
at: performance.now(),
}),
)
}
/** A race arm that never keeps the process alive on its own. */
export function timeoutAfter(ms) {
return new Promise((done) => {
setTimeout(
() => done({ kind: 'timeout', at: performance.now() }),
ms,
).unref()
})
}
/** Races `promise` with the watchdog: a killed browser ends the wait. */
export function untilStopped(session, promise) {
const stopped = session.watchdog.stopped.then((s) => ({
kind: s.type === 'ceiling' ? 'stopped_by_ceiling' : 'watchdog_failed',
error: s.error ?? null,
at: performance.now(),
}))
return Promise.race([promise, stopped])
}
export const isStop = (end) =>
end.kind === 'stopped_by_ceiling' || end.kind === 'watchdog_failed'
+107
View File
@@ -0,0 +1,107 @@
import { mkdirSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { parseArgs } from 'node:util'
import { checkBuild } from './app-build.mjs'
import { hostFacts, memAvailableBytes } from './host.mjs'
import { RESULTS_DIR } from './paths.mjs'
import { ENGINES, loadPlaywright } from './playwright.mjs'
import { markdownSummary } from './summary.mjs'
import { watchdogFacts } from './watchdog.mjs'
import { startServer } from '../serve.mjs'
/**
* Shared CLI of the baseline scripts: checks the build, serves it, runs the
* given sections (`{ export: fn, import: fn }`) engine after engine, writes
* one results file and prints the summary tables.
*/
const today = () => new Date().toISOString().slice(0, 10)
const log = (line) => console.error(`[${new Date().toISOString()}] ${line}`)
const listOf = (text) => (text ? text.split(',').filter(Boolean) : null)
/** --reports and --groups narrow a run (null = all): reruns, smoke tests. */
function options(defaultName) {
const { values } = parseArgs({
options: {
engines: { type: 'string', default: ENGINES.join(',') },
out: { type: 'string', default: join(RESULTS_DIR, defaultName) },
reports: { type: 'string' },
groups: { type: 'string' },
},
})
const engines = listOf(values.engines)
const unknown = engines.filter((e) => !ENGINES.includes(e))
if (unknown.length) throw new Error(`unknown engines: ${unknown.join(', ')}`)
const filters = {
reports: listOf(values.reports),
groups: listOf(values.groups),
}
return { engines, out: values.out, filters }
}
/** Engine versions as the runs recorded them. */
function enginesOf(sections) {
const versions = {}
for (const section of Object.values(sections)) {
for (const run of section.runs ?? []) {
if (run.engineVersion) versions[run.engine] = run.engineVersion
}
}
return versions
}
async function runSections(sections, context) {
const results = {}
for (const [name, run] of Object.entries(sections)) {
log(`section ${name}: ${context.engines.join(', ')}`)
results[name] = await run({ ...context, log })
}
return results
}
/** What a results file states about the conditions of its runs. */
function conditionsOf(build, playwrightCore, server) {
return {
harness: 'scripts/browser',
startedAt: new Date().toISOString(),
watchdog: watchdogFacts(),
host: hostFacts(),
build,
server: { bind: '127.0.0.1', csp: server.csp, envJs: 'local tool' },
playwrightCore,
}
}
function writeResult(out, result) {
mkdirSync(dirname(out), { recursive: true })
writeFileSync(out, `${JSON.stringify(result, null, 2)}\n`)
log(`results written to ${out}`)
console.log(markdownSummary(result))
}
export async function runHarnessCli(sections, defaultName) {
const sectionNames = Object.keys(sections).join('-')
const { engines, out, filters } = options(
defaultName ?? `${today()}-${sectionNames}.json`,
)
const build = checkBuild()
const { pw, version } = loadPlaywright()
const server = await startServer()
try {
const conditions = conditionsOf(build, version, server)
const context = { pw, baseUrl: server.url, engines, filters }
const results = await runSections(sections, context)
conditions.host.memAvailableBytesAtEnd = memAvailableBytes()
const endedAt = new Date().toISOString()
const engineVersions = enginesOf(results)
writeResult(out, {
...conditions,
endedAt,
engines: engineVersions,
...results,
})
} finally {
server.close()
}
}
+27
View File
@@ -0,0 +1,27 @@
import { readFileSync } from 'node:fs'
import { arch, cpus, release, totalmem } from 'node:os'
/** The host facts a reader of the results needs to compare devices. */
export function memAvailableBytes() {
const info = readFileSync('/proc/meminfo', 'utf8')
const match = /^MemAvailable:\s+(\d+) kB$/m.exec(info)
return match ? Number(match[1]) * 1024 : null
}
function cpuModel() {
const info = readFileSync('/proc/cpuinfo', 'utf8')
return /^model name\s*:\s*(.+)$/m.exec(info)?.[1] ?? cpus()[0]?.model
}
export function hostFacts() {
return {
cpuModel: cpuModel(),
logicalCpus: cpus().length,
ramBytes: totalmem(),
memAvailableBytesAtStart: memAvailableBytes(),
kernel: release(),
arch: arch(),
node: process.version,
}
}
+52
View File
@@ -0,0 +1,52 @@
import { createHash } from 'node:crypto'
import { readFileSync, readdirSync, statSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
/**
* Fixture files, and the R4 oracle inputs committed as generator
* descriptions (`<slug>.gen.json`, generator `concat/1`, written by
* packages/pdf/tests/helpers/r4-reader-io.ts for inputs over 2 MiB).
*/
export const sha256 = (bytes) =>
createHash('sha256').update(bytes).digest('hex')
/** Files under `dir`, sorted, recursively when asked. */
export function filesUnder(dir, recursive = false) {
const files = []
for (const name of readdirSync(dir).sort()) {
const path = join(dir, name)
if (statSync(path).isDirectory()) {
if (recursive) files.push(...filesUnder(path, true))
} else {
files.push(path)
}
}
return files
}
export const isGenerator = (path) => path.endsWith('.gen.json')
function partBytes(part, dir) {
if ('latin1' in part) return Buffer.from(part.latin1, 'latin1')
if ('fill' in part) return Buffer.alloc(part.count, part.fill)
const source = readFileSync(resolve(dir, part.slice))
return source.subarray(part.start, part.end)
}
/** The bytes a description stands for, checked: length and digest. */
export function expandGenerator(path) {
const description = JSON.parse(readFileSync(path, 'utf8'))
if (description.generator !== 'concat/1') {
throw new Error(`${path}: unknown generator ${description.generator}`)
}
const parts = description.parts.map((p) => partBytes(p, dirname(path)))
const bytes = Buffer.concat(parts)
if (bytes.length !== description.length) {
throw new Error(`${path}: expands to ${bytes.length} bytes`)
}
if (sha256(bytes) !== description.sha256) {
throw new Error(`${path}: digest differs from the description`)
}
return bytes
}
+144
View File
@@ -0,0 +1,144 @@
/**
* Init scripts (run in the page before its own scripts). They record what
* the measures need and call through to every function they wrap, so the
* application behaves as without them. Each function is serialised by
* Playwright: it must not reference anything outside its own body.
*/
/** Seeds the tab as the app leaves it (persistence.ts, EditorRoute). */
export function seedTab({ draft, locale, skipDisclaimer }) {
if (location.origin === 'null') return
if (draft !== null && sessionStorage.getItem('osteo:draft') === null) {
sessionStorage.setItem('osteo:draft', draft)
}
if (skipDisclaimer) sessionStorage.setItem('osteo:disclaimer-shown', '1')
localStorage.setItem('osteo:locale', locale)
}
export function installProbes() {
const probe = {
png: [],
dataUrls: 0,
clickAt: null,
pdfBlobAt: null,
changeAt: null,
navAt: null,
alertAt: null,
longTaskSupported: false,
longTasks: [],
csp: [],
}
window.__harness = probe
const now = () => performance.now()
// Every PNG the browser rasteriser encodes goes through toBlob.
function wrapCanvas() {
const proto = HTMLCanvasElement.prototype
const toBlob = proto.toBlob
proto.toBlob = function (callback, ...rest) {
const size = { width: this.width, height: this.height }
const record = (blob) => {
probe.png.push({ ...size, bytes: blob ? blob.size : null })
callback(blob)
}
return toBlob.call(this, record, ...rest)
}
const toDataURL = proto.toDataURL
proto.toDataURL = function (...args) {
probe.dataUrls += 1
return toDataURL.apply(this, args)
}
}
// download.ts hands the finished PDF to URL.createObjectURL.
function wrapObjectUrl() {
const create = URL.createObjectURL
URL.createObjectURL = function (object) {
if (object instanceof Blob && object.type === 'application/pdf') {
probe.pdfBlobAt = now()
}
return create.call(URL, object)
}
}
// HomeRoute ends an import with an alert, a navigation, or both.
function wrapNavigationAndAlert() {
const push = history.pushState
history.pushState = function (...args) {
if (probe.navAt === null) probe.navAt = now()
return push.apply(this, args)
}
const alert = window.alert
window.alert = function (message) {
if (probe.alertAt === null) probe.alertAt = now()
return alert.call(window, message)
}
}
function observeLongTasks() {
const types = PerformanceObserver.supportedEntryTypes ?? []
if (!types.includes('longtask')) return
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
probe.longTasks.push({ start: entry.startTime, ms: entry.duration })
}
})
observer.observe({ type: 'longtask' })
probe.longTaskSupported = true
}
function listenToEvents() {
// The first click only: download.ts clicks its own anchor afterwards.
window.addEventListener(
'click',
() => {
if (probe.clickAt === null) probe.clickAt = now()
},
true,
)
window.addEventListener(
'change',
(e) => {
if (e.target?.type === 'file' && probe.changeAt === null) {
probe.changeAt = now()
}
},
true,
)
document.addEventListener('securitypolicyviolation', (e) =>
probe.csp.push(`${e.effectiveDirective} ${e.blockedURI}`),
)
}
wrapCanvas()
wrapObjectUrl()
wrapNavigationAndAlert()
observeLongTasks()
listenToEvents()
}
/** Reads the probe back (a structured-clone copy). */
export const readProbe = () => window.__harness
/**
* A page of a fresh context with the probes and the tab seed, dialogs
* accepted and recorded (the app's alerts block its script until then).
*/
export async function probedPage(context, seed) {
await context.addInitScript(seedTab, seed)
await context.addInitScript(installProbes)
const page = await context.newPage()
const events = { dialogs: [], pageErrors: [], consoleErrors: [] }
let firstDialog
events.firstDialog = new Promise((done) => (firstDialog = done))
page.on('dialog', (dialog) => {
events.dialogs.push({ type: dialog.type(), message: dialog.message() })
firstDialog(dialog.message())
dialog.accept().catch(() => undefined)
})
page.on('pageerror', (e) => events.pageErrors.push(String(e.message)))
page.on('console', (m) => {
if (m.type() === 'error') events.consoleErrors.push(m.text().slice(0, 300))
})
return { page, events }
}
+10
View File
@@ -0,0 +1,10 @@
import { resolve } from 'node:path'
export const HARNESS_DIR = resolve(import.meta.dirname, '..')
export const REPO_ROOT = resolve(HARNESS_DIR, '../..')
export const DIST_DIR = resolve(REPO_ROOT, 'apps/app/dist')
export const FIXTURES_DIR = resolve(REPO_ROOT, 'apps/app/tests/fixtures')
export const ORACLES_R4_DIR = resolve(REPO_ROOT, 'crates/core/tests/oracles/r4')
export const RESULTS_DIR = resolve(HARNESS_DIR, 'results')
export const relativeToRepo = (path) => path.slice(REPO_ROOT.length + 1)
+74
View File
@@ -0,0 +1,74 @@
import { createRequire } from 'node:module'
import { join } from 'node:path'
import { delay } from './async.mjs'
import { childrenOf, killAll, treeOf } from './proc.mjs'
import { startWatchdog } from './watchdog.mjs'
/**
* Playwright stays outside the repository: PLAYWRIGHT_NODE_MODULES names a
* node_modules that holds playwright-core (no project dependency), as for
* docker/csp-proof/app-image.sh. Browsers come from its own cache
* (~/.cache/ms-playwright).
*/
export const ENGINES = ['chromium', 'firefox', 'webkit']
const CLOSE_TIMEOUT_MS = 15_000
function modulesDir() {
const dir = process.env.PLAYWRIGHT_NODE_MODULES
if (!dir) {
throw new Error(
'set PLAYWRIGHT_NODE_MODULES to a node_modules holding playwright-core',
)
}
return dir
}
export function loadPlaywright() {
const require = createRequire(import.meta.url)
const dir = modulesDir()
const pw = require(join(dir, 'playwright-core'))
const { version } = require(join(dir, 'playwright-core/package.json'))
return { pw, version }
}
/** The one child process `launch` started: the root of the browser's tree. */
function newChild(before) {
const fresh = childrenOf(process.pid).filter((pid) => !before.has(pid))
if (fresh.length !== 1) {
throw new Error(`expected one new browser process, found ${fresh.length}`)
}
return fresh[0]
}
/**
* Launches one headless browser under its watchdog. Fails closed: a browser
* whose process tree cannot be identified is closed, never run unwatched.
*/
export async function launchEngine(pw, engine) {
const before = new Set(childrenOf(process.pid))
const browser = await pw[engine].launch({ headless: true })
let rootPid
try {
rootPid = newChild(before)
} catch (error) {
await browser.close()
throw error
}
const watchdog = startWatchdog({ rootPid })
await watchdog.ready
return { engine, browser, rootPid, version: browser.version(), watchdog }
}
/** Closes the browser; one that hangs is killed, its own tree only. */
export async function closeEngine(session) {
const closed = session.browser.close().then(
() => true,
() => true,
)
const timedOut = delay(CLOSE_TIMEOUT_MS).then(() => false)
if (!(await Promise.race([closed, timedOut]))) {
killAll(treeOf(session.rootPid))
}
await session.watchdog.stop()
}
+92
View File
@@ -0,0 +1,92 @@
import { readFileSync, readdirSync } from 'node:fs'
/** Process facts from /proc (Linux only): the watchdog's only source. */
function readOrEmpty(path) {
try {
return readFileSync(path, 'utf8')
} catch {
return ''
}
}
function listOrEmpty(path) {
try {
return readdirSync(path)
} catch {
return []
}
}
const numbersOf = (text) => text.split(/\s+/).filter(Boolean).map(Number)
/** Direct children of `pid`, over all its threads; [] once it is gone. */
export function childrenOf(pid) {
return listOrEmpty(`/proc/${pid}/task`).flatMap((tid) =>
numbersOf(readOrEmpty(`/proc/${pid}/task/${tid}/children`)),
)
}
/** `root` and every process below it, breadth first. */
export function treeOf(root) {
const tree = [root]
for (let i = 0; i < tree.length; i += 1) {
for (const child of childrenOf(tree[i])) {
if (!tree.includes(child)) tree.push(child)
}
}
return tree
}
/** VmRSS in bytes; 0 for a process that is gone or a zombie. */
export function rssBytesOf(pid) {
const status = readOrEmpty(`/proc/${pid}/status`)
const match = /^VmRSS:\s+(\d+) kB$/m.exec(status)
return match ? Number(match[1]) * 1024 : 0
}
/**
* A process that renders web content: Chromium's renderers, Firefox's
* "Web Content" and "Isolated Web Content" (comm is cut at 15 bytes),
* WebKit's web process. Read at every sample: names change after exec.
*/
export function isContentProcess(pid) {
const comm = readOrEmpty(`/proc/${pid}/comm`).trim()
if (comm.startsWith('WPEWebProcess') || comm.startsWith('WebKitWebProc')) {
return true
}
if (comm.startsWith('Web Content') || comm.startsWith('Isolated Web')) {
return true
}
// Chromium rewrites its argv as one space-separated title.
const cmdline = readOrEmpty(`/proc/${pid}/cmdline`)
return /(^|[\0 ])--type=renderer([\0 ]|$)/.test(cmdline)
}
/** Summed, content-process and largest single VmRSS of the tree. */
export function treeRss(root) {
const pids = treeOf(root)
const sizes = pids.map(rssBytesOf)
const content = pids.filter((pid, i) => sizes[i] > 0 && isContentProcess(pid))
const sum = (list) => list.reduce((total, size) => total + size, 0)
return {
pids,
totalBytes: sum(sizes),
contentBytes: sum(content.map((pid) => sizes[pids.indexOf(pid)])),
maxProcessBytes: Math.max(0, ...sizes),
}
}
/**
* SIGKILL to each pid, the first one (the tree's root) first so it cannot
* respawn a child. Callers pass only a tree they launched.
*/
export function killAll(pids) {
for (const pid of pids) {
try {
process.kill(pid, 'SIGKILL')
} catch {
// Already gone.
}
}
}
+21
View File
@@ -0,0 +1,21 @@
/** Small descriptive statistics for the summary tables. */
const sorted = (values) => [...values].sort((a, b) => a - b)
export function median(values) {
const list = sorted(values)
if (list.length === 0) return null
const mid = Math.floor(list.length / 2)
return list.length % 2 ? list[mid] : (list[mid - 1] + list[mid]) / 2
}
/** Median and range of the finite numbers of `values` (nulls skipped). */
export function spread(values) {
const list = sorted(values.filter((v) => Number.isFinite(v)))
if (list.length === 0) return null
const min = list[0]
const max = list[list.length - 1]
return { n: list.length, median: median(list), min, max }
}
export const sum = (values) => values.reduce((total, v) => total + v, 0)
+266
View File
@@ -0,0 +1,266 @@
import { spread } from './stats.mjs'
/**
* Markdown tables from a results file (the shape lib/harness.mjs writes),
* ready for docs/development/RUST.md §10.
*/
const MB = 1024 * 1024
const orDash = (format) => (value) =>
value === null || value === undefined ? '–' : format(value)
const mb = orDash((bytes) => (bytes / MB).toFixed(1))
const kb = orDash((bytes) => (bytes / 1024).toFixed(0))
const ms = orDash((value) => value.toFixed(0))
const s = orDash((value) => (value / 1000).toFixed(1))
/** "median (min–max)" or the single value. */
function range(values, format) {
const stats = spread(values)
if (!stats) return '–'
if (stats.n === 1) return format(stats.median)
return `${format(stats.median)} (${format(stats.min)}–${format(stats.max)})`
}
function table(header, rows) {
const line = (cells) => `| ${cells.join(' | ')} |`
return [line(header), line(header.map(() => '---')), ...rows.map(line)].join(
'\n',
)
}
function groupBy(items, keyOf) {
const groups = new Map()
for (const item of items) {
const key = keyOf(item)
groups.set(key, [...(groups.get(key) ?? []), item])
}
return groups
}
function outcomes(runs) {
const counts = groupBy(runs, (r) => r.outcome)
return [...counts]
.map(([outcome, list]) => `${outcome} ${list.length}`)
.join(', ')
}
const pick = (runs, path) =>
runs.map((r) => path.split('.').reduce((v, k) => v?.[k], r))
/** Where the watchdog stopped a run: summed RSS and PNG blobs encoded. */
function stopsOf(runs) {
const stopped = runs.filter((r) => r.outcome === 'stopped_by_ceiling')
const at = (r) =>
`${mb(r.stoppedAtBytes)} MB after ${r.rastersBeforeEnd} PNGs`
return stopped.map(at).join('; ') || '–'
}
function exportTimeRow([key, runs]) {
const [engine, report] = key.split(' ')
const done = runs.filter((r) => r.outcome === 'downloaded')
return [
engine,
report,
outcomes(runs),
range(pick(done, 'exportMs'), s),
range(pick(done, 'pdfBytes'), mb),
range(pick(done, 'rasters.count'), String),
range(pick(done, 'rasters.views.totalBytes'), mb),
range(pick(done, 'rasters.views.minBytes'), kb),
range(pick(done, 'rasters.views.maxBytes'), kb),
stopsOf(runs),
]
}
function exportMemoryRow([key, runs]) {
const [engine, report] = key.split(' ')
const done = runs.filter((r) => r.outcome === 'downloaded')
return [
engine,
report,
range(pick(runs, 'before.rssBytes'), mb),
range(pick(runs, 'rssPeakBytes'), mb),
range(pick(done, 'after.rssBytes'), mb),
range(pick(runs, 'before.rssContentBytes'), mb),
range(pick(runs, 'rssPeakContentBytes'), mb),
range(pick(done, 'after.rssContentBytes'), mb),
range(pick(runs, 'before.jsHeapUsedBytes'), mb),
range(pick(done, 'after.jsHeapUsedBytes'), mb),
]
}
export function exportTables(section) {
const groups = [...groupBy(section.runs, (r) => `${r.engine} ${r.report}`)]
const time = [
'Engine',
'Report',
'Outcome',
'Export s',
'PDF MB',
'PNG blobs',
'View PNG sum MB',
'View PNG min KB',
'View PNG max KB',
'Stopped at',
]
const memory = [
'Engine',
'Report',
'RSS before MB',
'RSS peak MB',
'RSS after MB',
'Content before MB',
'Content peak MB',
'Content after MB',
'JS heap before MB',
'JS heap after MB',
]
return [
table(time, groups.map(exportTimeRow)),
table(memory, groups.map(exportMemoryRow)),
].join('\n\n')
}
function importGroupRow([key, runs]) {
const [engine, group] = key.split(' ')
const growth = runs.map(
(r) => (r.rssPeakBytes ?? 0) - (r.rssBeforeBytes ?? 0),
)
const content = runs.map(
(r) => (r.rssPeakContentBytes ?? 0) - (r.rssBeforeContentBytes ?? 0),
)
return [
engine,
group,
String(runs.length),
outcomes(runs),
range(pick(runs, 'durationMs'), ms),
ms(Math.max(...pick(runs, 'durationMs').filter(Number.isFinite))),
mb(Math.max(...growth)),
mb(Math.max(...content)),
range(pick(runs, 'longTasks.maxMs'), ms),
range(pick(runs, 'longTasks.totalMs'), ms),
]
}
export function importGroupTable(section) {
const header = [
'Engine',
'Group',
'Files',
'Outcome',
'Duration ms',
'Max ms',
'Max RSS growth MB',
'Max content growth MB',
'Long task max ms',
'Long tasks total ms',
]
const groups = groupBy(section.runs, (r) => `${r.engine} ${r.group}`)
return table(header, [...groups].map(importGroupRow))
}
const short = (text) => (text ?? '').replace(/\s+/g, ' ').slice(0, 70)
const messageOf = (run) =>
short(run.errorText ?? run.dialogs?.[0] ?? run.error ?? '')
/** Reference files: one row per file, one cell per engine. */
export function importFileTable(section, groups, engines) {
const runs = section.runs.filter((r) => groups.includes(r.group))
const byFile = groupBy(runs, (r) => r.file)
const cell = (list, engine) => {
const run = list.find((r) => r.engine === engine)
return run ? `${run.outcome} ${ms(run.durationMs)} ms` : '–'
}
const rows = [...byFile].map(([file, list]) => [
file.replace('apps/app/tests/fixtures/', ''),
...engines.map((engine) => cell(list, engine)),
messageOf(list[0]),
])
return table(['File', ...engines, 'Message (first engine)'], rows)
}
/** Files whose outcome or message differs between engines. */
export function importDisagreements(section) {
const byFile = groupBy(section.runs, (r) => r.file)
const rows = []
for (const [file, list] of byFile) {
const verdicts = list.map((r) => `${r.outcome}: ${messageOf(r)}`)
if (new Set(verdicts).size < 2) continue
rows.push([
file.replace('apps/app/tests/fixtures/', ''),
...list.map((r, i) => `${r.engine} ${verdicts[i]}`),
])
}
if (rows.length === 0) return 'No file differs between engines.'
return rows.map((r) => `- ${r[0]}: ${r.slice(1).join(' / ')}`).join('\n')
}
/** The slowest and the most memory-hungry imports. */
export function importTop(section, count = 10) {
const growth = (r) => (r.rssPeakBytes ?? 0) - (r.rssBeforeBytes ?? 0)
const row = (r) => [
r.engine,
r.file.replace('apps/app/tests/fixtures/', ''),
r.outcome,
ms(r.durationMs),
mb(growth(r)),
short(messageOf(r)),
]
const header = [
'Engine',
'File',
'Outcome',
'Duration ms',
'RSS growth MB',
'Message',
]
const slow = [...section.runs]
.sort((a, b) => (b.durationMs ?? 0) - (a.durationMs ?? 0))
.slice(0, count)
const heavy = [...section.runs]
.sort((a, b) => growth(b) - growth(a))
.slice(0, count)
return [
'Slowest:',
table(header, slow.map(row)),
'Largest RSS growth:',
table(header, heavy.map(row)),
].join('\n\n')
}
function hostLine(host) {
const ram = `${mb(host.ramBytes)} MB RAM`
return `Host: ${host.cpuModel}, ${host.logicalCpus} threads, ${ram}.`
}
function enginesLine(result, engines) {
const list = engines.map((e) => `${e} ${result.engines[e]}`).join(', ')
return `Engines: ${list} (playwright-core ${result.playwrightCore}).`
}
export function markdownSummary(result) {
const engines = Object.keys(result.engines)
const parts = [
hostLine(result.host),
enginesLine(result, engines),
`Watchdog: ${result.watchdog.caveat}`,
]
if (result.export)
parts.push('Export (TS engine, fr):', exportTables(result.export))
if (result.import) {
parts.push('Import by group:', importGroupTable(result.import))
parts.push(
'Reference files:',
importFileTable(result.import, ['base', 'r3', 'step6'], engines),
)
parts.push('Engines that disagree:', importDisagreements(result.import))
parts.push(importTop(result.import))
if (result.import.hostile.missing.length) {
parts.push(
`Hostile inputs missing: ${result.import.hostile.missing.join(', ')}`,
)
}
}
return parts.join('\n\n')
}
+51
View File
@@ -0,0 +1,51 @@
import { parentPort, workerData } from 'node:worker_threads'
import { killAll, treeOf, treeRss } from './proc.mjs'
import { SAMPLE_INTERVAL_MS, SLOT } from './watchdog.mjs'
/** Sampler thread of lib/watchdog.mjs; writes the shared slots only. */
const { rootPid, ceilingBytes, shared } = workerData
const stats = new Int32Array(shared)
const toKib = (bytes) => Math.round(bytes / 1024)
const PEAKS = [
[SLOT.currentKib, SLOT.peakKib],
[SLOT.currentMaxKib, SLOT.peakMaxKib],
[SLOT.currentContentKib, SLOT.peakContentKib],
]
/** Current values, then each peak (restarted when the main thread asked). */
function record(values) {
for (const [slot, value] of values) Atomics.store(stats, slot, value)
const reset = Atomics.load(stats, SLOT.resetRequested) === 1
for (const [current, peak] of PEAKS) {
const value = Atomics.load(stats, current)
if (reset || value > Atomics.load(stats, peak)) {
Atomics.store(stats, peak, value)
}
}
if (reset) Atomics.store(stats, SLOT.resetRequested, 0)
Atomics.add(stats, SLOT.samples, 1)
}
/** A fresh walk at kill time: children forked since the sample are included. */
function stopBrowser(timer, totalBytes) {
clearInterval(timer)
killAll(treeOf(rootPid))
Atomics.store(stats, SLOT.stoppedAtKib, toKib(totalBytes))
Atomics.store(stats, SLOT.stopped, 1)
parentPort.postMessage({ type: 'ceiling', totalBytes })
}
function sample(timer) {
const rss = treeRss(rootPid)
record([
[SLOT.currentKib, toKib(rss.totalBytes)],
[SLOT.currentMaxKib, toKib(rss.maxProcessBytes)],
[SLOT.currentContentKib, toKib(rss.contentBytes)],
])
if (rss.totalBytes > ceilingBytes) stopBrowser(timer, rss.totalBytes)
}
const timer = setInterval(() => sample(timer), SAMPLE_INTERVAL_MS)
sample(timer)
+125
View File
@@ -0,0 +1,125 @@
import { Worker } from 'node:worker_threads'
import { delay } from './async.mjs'
import { killAll, treeOf } from './proc.mjs'
/**
* Memory ceiling of one launched browser. A worker thread samples the summed
* VmRSS of every process below the browser's root pid every 50 ms and, above
* the ceiling, SIGKILLs that tree (the shared host must never reach the
* kernel OOM killer). The sampler lives in its own thread so a busy main
* event loop cannot delay it.
*/
export const CEILING_BYTES = 4 * 1024 ** 3
export const SAMPLE_INTERVAL_MS = 50
export const WATCHDOG_CAVEAT =
'The 4 GiB RSS watchdog is not a guaranteed memory limit: it samples ' +
'every 50 ms, so a faster allocation can overshoot it before the kill; ' +
'summed VmRSS counts shared pages once per process; a process outside ' +
"the browser's tree is not seen."
/** Shared Int32 slots, sizes in KiB. */
export const SLOT = {
currentKib: 0,
currentMaxKib: 1,
peakKib: 2,
peakMaxKib: 3,
samples: 4,
stopped: 5,
resetRequested: 6,
stoppedAtKib: 7,
currentContentKib: 8,
peakContentKib: 9,
}
const SLOT_COUNT = 10
const RESET_WAIT_MS = 1000
const POLL_MS = 5
export const watchdogFacts = () => ({
ceilingBytes: CEILING_BYTES,
sampleIntervalMs: SAMPLE_INTERVAL_MS,
measure:
'summed VmRSS (/proc/<pid>/status) of the browser process tree; ' +
'content = renderer / Web Content / WPEWebProcess processes only',
onCeiling: 'SIGKILL of the tree, run recorded as stopped_by_ceiling',
caveat: WATCHDOG_CAVEAT,
})
const kib = (stats, slot) => Atomics.load(stats, slot) * 1024
function snapshotOf(stats) {
return {
currentBytes: kib(stats, SLOT.currentKib),
currentMaxProcessBytes: kib(stats, SLOT.currentMaxKib),
peakBytes: kib(stats, SLOT.peakKib),
peakMaxProcessBytes: kib(stats, SLOT.peakMaxKib),
currentContentBytes: kib(stats, SLOT.currentContentKib),
peakContentBytes: kib(stats, SLOT.peakContentKib),
samples: Atomics.load(stats, SLOT.samples),
stopped: Atomics.load(stats, SLOT.stopped) === 1,
stoppedAtBytes: kib(stats, SLOT.stoppedAtKib),
}
}
async function waitUntil(test, timeoutMs, what) {
const end = performance.now() + timeoutMs
while (!test()) {
if (performance.now() > end) throw new Error(`watchdog: ${what}`)
await delay(POLL_MS)
}
}
/** Starts a peak window at the next sample (the sampler owns the write). */
async function resetPeak(stats) {
Atomics.store(stats, SLOT.resetRequested, 1)
const done = () => Atomics.load(stats, SLOT.resetRequested) === 0
await waitUntil(done, RESET_WAIT_MS, 'peak reset not acknowledged')
}
/**
* `stopped` resolves when the sampler killed the tree, or when the sampler
* itself died: the tree is then killed here, so no browser runs unwatched.
*/
function stoppedPromise(worker, rootPid, stats, isStopping) {
return new Promise((resolve) => {
let settled = false
const settle = (message) => {
settled = true
resolve(message)
}
const failed = (error) => {
if (settled || isStopping()) return
if (Atomics.load(stats, SLOT.stopped) === 1) {
return settle({
type: 'ceiling',
totalBytes: kib(stats, SLOT.stoppedAtKib),
})
}
killAll(treeOf(rootPid))
settle({ type: 'watchdog_failed', error: String(error) })
}
worker.on('message', (m) => m.type === 'ceiling' && settle(m))
worker.on('error', failed)
worker.on('exit', (code) => failed(`sampler exited (${code})`))
})
}
export function startWatchdog({ rootPid, ceilingBytes = CEILING_BYTES }) {
const shared = new SharedArrayBuffer(SLOT_COUNT * 4)
const stats = new Int32Array(shared)
const worker = new Worker(new URL('./watchdog-worker.mjs', import.meta.url), {
workerData: { rootPid, ceilingBytes, shared },
})
let stopping = false
const sampled = () => Atomics.load(stats, SLOT.samples) > 0
return {
ready: waitUntil(sampled, RESET_WAIT_MS, 'no first sample'),
stopped: stoppedPromise(worker, rootPid, stats, () => stopping),
snapshot: () => snapshotOf(stats),
resetPeak: () => resetPeak(stats),
stop: () => {
stopping = true
return worker.terminate()
},
}
}
File diff suppressed because it is too large. Load diff
+159
View File
@@ -0,0 +1,159 @@
// Static server of the production build for the browser harness (R4.1e):
// apps/app/dist with the SPA fallback of docker/nginx.conf, `.wasm` as
// application/wasm, the production CSP of DEPLOY.md on every response, and
// the env.js docker/entrypoint.sh writes when no APP_* variable is set (the
// local tool, no Supabase). Binds 127.0.0.1 only.
//
// node scripts/browser/serve.mjs [--port 4173] [--csp with|without]
//
// --csp with (default): script-src carries 'wasm-unsafe-eval' (DEPLOY.md
// note for R2+ images); without: the header exactly without the keyword,
// for the CSP proofs of later slices.
import { readFileSync } from 'node:fs'
import { readFile, stat } from 'node:fs/promises'
import { createServer } from 'node:http'
import { extname, join, resolve, sep } from 'node:path'
import { pathToFileURL } from 'node:url'
import { parseArgs } from 'node:util'
import { DIST_DIR, REPO_ROOT } from './lib/paths.mjs'
const HOST = '127.0.0.1'
const WASM_KEYWORD = "'wasm-unsafe-eval'"
const MIME = {
'.html': 'text/html; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.mjs': 'text/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.json': 'application/json',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.ico': 'image/x-icon',
'.wasm': 'application/wasm',
'.woff2': 'font/woff2',
'.txt': 'text/plain; charset=utf-8',
}
// Byte for byte what docker/entrypoint.sh writes with no APP_* variable set.
export const LOCAL_TOOL_ENV_JS = `window.__APP_CONFIG__ = {
siteName: "Faunosteo",
brandName: "Faunosteo",
footerName: "Faunosteo",
tagline: "Outils pour ostéopathes animaliers",
supabaseUrl: "",
supabaseAnonKey: "",
authProviders: ""
};
`
/** The nginx header line of DEPLOY.md, the one app-image.sh reads too. */
export function productionCsp() {
const deploy = readFileSync(join(REPO_ROOT, 'DEPLOY.md'), 'utf8')
const line = /^add_header Content-Security-Policy "(.*)" always;$/m
const match = line.exec(deploy)
if (!match) throw new Error('DEPLOY.md: no Content-Security-Policy line')
return match[1]
}
export function cspVariant(policy, variant) {
const without = policy.replace(` ${WASM_KEYWORD}`, '')
if (variant === 'without') return without
if (!without.includes("script-src 'self'")) {
throw new Error("DEPLOY.md CSP: no script-src 'self'")
}
return without.replace(
"script-src 'self'",
`script-src 'self' ${WASM_KEYWORD}`,
)
}
/** The request path, or null when it cannot be decoded safely. */
function requestPath(url) {
try {
const path = decodeURIComponent(new URL(url, 'http://local').pathname)
return path.includes('\0') ? null : path
} catch {
return null
}
}
/** A regular file under `root` for `path`, never outside it. */
async function fileUnder(root, path) {
const full = resolve(root, `.${path}`)
if (!full.startsWith(root + sep)) return null
try {
return (await stat(full)).isFile() ? full : null
} catch {
return null
}
}
function send(res, { status, body = '', type, csp, noStore = false, head }) {
const headers = { 'Content-Security-Policy': csp }
if (type) headers['Content-Type'] = type
if (noStore) headers['Cache-Control'] = 'no-store, no-cache, must-revalidate'
headers['Content-Length'] = Buffer.byteLength(body)
res.writeHead(status, headers)
res.end(head ? undefined : body)
}
/** nginx.conf: env.js generated, /assets/ files or 404, else the SPA. */
async function route(root, path) {
if (path === '/env.js') {
return { body: LOCAL_TOOL_ENV_JS, type: MIME['.js'], noStore: true }
}
const file = await fileUnder(root, path)
if (file) {
const type = MIME[extname(file)] ?? 'application/octet-stream'
return { body: await readFile(file), type }
}
if (path.startsWith('/assets/')) return { status: 404 }
return { body: await readFile(join(root, 'index.html')), type: MIME['.html'] }
}
async function respond(req, res, { root, csp }) {
const head = req.method === 'HEAD'
if (req.method !== 'GET' && !head) return send(res, { status: 405, csp })
const path = requestPath(req.url)
if (path === null) return send(res, { status: 400, csp })
try {
const answer = await route(root, path)
send(res, { status: 200, ...answer, csp, head })
} catch {
send(res, { status: 500, csp })
}
}
/** Starts the server; `port` 0 picks a free one. */
export function startServer({ root = DIST_DIR, port = 0, csp = 'with' } = {}) {
const policy = cspVariant(productionCsp(), csp)
const server = createServer((req, res) =>
respond(req, res, { root, csp: policy }),
)
return new Promise((done, fail) => {
server.once('error', fail)
server.listen(port, HOST, () => {
const url = `http://${HOST}:${server.address().port}`
done({ url, csp: policy, close: () => server.close() })
})
})
}
async function main() {
const { values } = parseArgs({
options: {
port: { type: 'string', default: '4173' },
csp: { type: 'string', default: 'with' },
},
})
if (!['with', 'without'].includes(values.csp)) {
throw new Error('--csp is "with" or "without"')
}
const server = await startServer({
port: Number(values.port),
csp: values.csp,
})
console.log(`serving apps/app/dist on ${server.url}`)
console.log(`Content-Security-Policy: ${server.csp}`)
}
if (import.meta.url === pathToFileURL(process.argv[1]).href) await main()
+8
View File
@@ -0,0 +1,8 @@
// Prints the markdown tables of a results file the baseline scripts wrote:
// node scripts/browser/summarize.mjs scripts/browser/results/<file>.json
import { readFileSync } from 'node:fs'
import { markdownSummary } from './lib/summary.mjs'
const file = process.argv[2]
if (!file) throw new Error('usage: summarize.mjs <results.json>')
console.log(markdownSummary(JSON.parse(readFileSync(file, 'utf8'))))
+14
View File
@@ -0,0 +1,14 @@
// The full R4.1e TypeScript baseline: the export runs, then the import runs,
// in one results file (default results/<date>-ts-baseline.json).
//
// PLAYWRIGHT_NODE_MODULES=... node scripts/browser/ts-baseline.mjs \
// [--engines chromium,firefox,webkit] [--out <file>]
import { runExportBaseline } from './export-baseline.mjs'
import { runImportBaseline } from './import-baseline.mjs'
import { runHarnessCli } from './lib/harness.mjs'
const name = `${new Date().toISOString().slice(0, 10)}-ts-baseline.json`
await runHarnessCli(
{ export: runExportBaseline, import: runImportBaseline },
name,
)