/** * 11-07. The pieces of the lab performance gate (`scripts/test-performance.ts`) * that decide what is measured and whether the numbers can be trusted. */ export const DEFAULT_LAB_PORT = 4173; export const DEFAULT_LAB_RUNS = 5; /** * `vite preview` arguments. Without `--strictPort` vite moved to the next free * port when the requested one was taken, and the gate measured whatever server * already listened there. */ export function previewArguments(port: number): readonly string[] { return [ "pnpm", "preview", "--host", "127.0.0.1", "--port", String(port), "--strictPort", ]; } /** * 1105/15-05. Playwright starts Chromium with `--hide-scrollbars`, so the lab * measured layout shift in a browser without the classic scrollbar most * Windows and Linux desktops draw. A first render that made one appear * moved the header 15 px and the lab reported CLS 0. The lab runs with * scrollbars, as those users see the page. */ export function labBrowserLaunchOptions(): { ignoreDefaultArgs: string[] } { return { ignoreDefaultArgs: ["--hide-scrollbars"] }; } export type LabRoute = Readonly<{ routeId: string; path: string; access: "public" | "session-required"; title: string; navigationLabel: string | null; navigationOrder: number | null; }>; /** * 1105/15-04. The feature route the lab times next to its platform target: * the first one in the navigation that an installed feature owns and a * visitor without a session can open. The platform target loads no feature * code, so its number stayed flat while a feature route's first entry (the * context route module, then the page chunk) took 2.7 times as long and no * gate measured it. `null` when no installed feature has such a route; the * lab then records that it measured none. */ export function labFeatureRoute( routes: readonly LabRoute[], owners: Readonly>, ): LabRoute | null { const candidates = routes.filter( (route) => owners[route.routeId] !== undefined && route.access === "public" && route.navigationLabel !== null && route.navigationOrder !== null, ); candidates.sort((left, right) => left.navigationOrder! - right.navigationOrder!); return candidates[0] ?? null; } export type SampleSummary = Readonly<{ median: number; min: number; max: number; samples: readonly number[]; }>; export function summarizeSamples(samples: readonly number[]): SampleSummary { if (samples.length === 0) throw new RangeError("no samples to summarize"); const sorted = [...samples].sort((left, right) => left - right); const middle = Math.floor(sorted.length / 2); const median = sorted.length % 2 === 1 ? sorted[middle]! : (sorted[middle - 1]! + sorted[middle]!) / 2; return Object.freeze({ median, min: sorted[0]!, max: sorted.at(-1)!, samples: Object.freeze([...samples]), }); } /** Fails when anything already answers where the preview server will listen. */ export async function assertPortUnused( baseUrl: string, fetchImpl: typeof fetch = fetch, ): Promise { let answered = false; try { await fetchImpl(baseUrl, { signal: AbortSignal.timeout(1_000) }); answered = true; } catch { // Nothing is listening: the expected state. } if (answered) { throw new Error( `${baseUrl} already answers before the preview server starts; another server holds the port.`, ); } } /** * Compares the release the server actually serves with the local build that * is being measured, so the numbers belong to that build. */ export async function assertServedRelease( baseUrl: string, expected: Readonly<{ buildId: string; releaseId: string }>, fetchImpl: typeof fetch = fetch, ): Promise { const response = await fetchImpl(new URL("/release-manifest.json", baseUrl), { cache: "no-store", }); const served = response.ok ? ((await response.json()) as Partial>) : {}; if (served.buildId !== expected.buildId || served.releaseId !== expected.releaseId) { throw new Error( `${baseUrl} serves build ${String(served.buildId)}/${String(served.releaseId)}, ` + `not the measured ${expected.buildId}/${expected.releaseId}.`, ); } }