Budget gate: Next's idle link prefetches counted apart, third parties recorded then aborted, spec filter safe for any checkout path; zero counted queries fail
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
beef517c23
commit
3b32e659c5
@@ -13,7 +13,10 @@
|
||||
* inp (the longest Event Timing entry of one scripted interaction: a click on the page's first visible button),
|
||||
* scriptMs (CDP Performance.ScriptDuration: parse, compile, run and hydrate), requests, bytes per type
|
||||
* (transfer size, compressed), renderBlocking (resource timing renderBlockingStatus), thirdParty (requests to
|
||||
* another origin before any consent is given).
|
||||
* another origin before any consent is given). Next's link prefetches (`?_rsc=`, made when the browser is idle) are
|
||||
* counted apart (`prefetches`): they are not part of the page's load and come at a different moment each run.
|
||||
* Option `blockThirdParty` (the gate): requests to another origin are recorded, then aborted — a slow external host
|
||||
* cannot hold the page's load event, and the numbers are the site's own.
|
||||
*/
|
||||
import { chromium } from '@playwright/test'
|
||||
|
||||
@@ -45,7 +48,7 @@ const OBSERVERS = `(() => {
|
||||
* @typedef {{ bytes: { css: number, font: number, html: number, image: number, js: number, other: number }, cls: number,
|
||||
* fcp: null | number, inp: null | number, interaction: null | string, lcp: null | number,
|
||||
* lcpElement: null | { fetchpriority: null | string, loading: null | string, src: null | string, tag: null | string },
|
||||
* load: null | number, profile: string, renderBlocking: string[], requests: number, runs?: number, scriptMs: null | number,
|
||||
* load: null | number, prefetches: number, profile: string, renderBlocking: string[], requests: number, runs?: number, scriptMs: null | number,
|
||||
* status: null | number, tbt: null | number, thirdParty: string[], ttfb: null | number, url: string }} PageMeasurement
|
||||
*/
|
||||
|
||||
@@ -62,7 +65,7 @@ const typeOf = (resourceType, mime = '') => {
|
||||
* @param {import('@playwright/test').Browser} browser
|
||||
* @param {string} url
|
||||
* @param {'mobile'|'desktop'} profileName
|
||||
* @param {{ interact?: boolean, settleMs?: number }} [options]
|
||||
* @param {{ blockThirdParty?: boolean, interact?: boolean, settleMs?: number }} [options]
|
||||
* @returns {Promise<PageMeasurement>}
|
||||
*/
|
||||
export async function measurePage(browser, url, profileName, options = {}) {
|
||||
@@ -78,13 +81,19 @@ export async function measurePage(browser, url, profileName, options = {}) {
|
||||
await page.addInitScript(OBSERVERS)
|
||||
|
||||
const origin = new URL(url).origin
|
||||
if (options.blockThirdParty) {await context.route((u) => u.origin !== origin, (route) => route.abort())}
|
||||
const requests = new Map()
|
||||
cdp.on('Network.requestWillBeSent', (e) => { if (!e.request.url.startsWith('data:')) {requests.set(e.requestId, { bytes: 0, type: typeOf(e.type), url: e.request.url })} })
|
||||
let prefetches = 0
|
||||
cdp.on('Network.requestWillBeSent', (e) => {
|
||||
if (e.request.url.startsWith('data:')) {return}
|
||||
if (/[?&]_rsc=/.test(e.request.url)) {prefetches++; return}
|
||||
requests.set(e.requestId, { bytes: 0, type: typeOf(e.type), url: e.request.url })
|
||||
})
|
||||
cdp.on('Network.responseReceived', (e) => { const r = requests.get(e.requestId); if (r) {r.type = typeOf(e.type, e.response.mimeType); r.status = e.response.status} })
|
||||
cdp.on('Network.loadingFinished', (e) => { const r = requests.get(e.requestId); if (r) {r.bytes = e.encodedDataLength} })
|
||||
|
||||
const response = await page.goto(url, { waitUntil: 'load' })
|
||||
await page.waitForLoadState('networkidle').catch(() => {})
|
||||
const response = await page.goto(url, { timeout: 60_000, waitUntil: 'load' })
|
||||
await page.waitForLoadState('networkidle', { timeout: 15_000 }).catch(() => {})
|
||||
await page.waitForTimeout(options.settleMs ?? 1000)
|
||||
|
||||
let interaction = null
|
||||
@@ -124,6 +133,7 @@ export async function measurePage(browser, url, profileName, options = {}) {
|
||||
lcp: round(vitals.lcp?.at),
|
||||
lcpElement: vitals.lcp ? { fetchpriority: vitals.lcp.fetchpriority, loading: vitals.lcp.loading, src: vitals.lcp.src ? vitals.lcp.src.replace(origin, '') : null, tag: vitals.lcp.tag } : null,
|
||||
load: round(nav.load),
|
||||
prefetches,
|
||||
profile: profileName,
|
||||
renderBlocking: nav.blocking.map((u) => u.replace(origin, '')),
|
||||
requests: list.length,
|
||||
@@ -149,7 +159,7 @@ export const median = (values) => {
|
||||
* @param {string} url
|
||||
* @param {'mobile'|'desktop'} profileName
|
||||
* @param {number} [runs]
|
||||
* @param {{ interact?: boolean, settleMs?: number }} [options]
|
||||
* @param {{ blockThirdParty?: boolean, interact?: boolean, settleMs?: number }} [options]
|
||||
* @returns {Promise<PageMeasurement>}
|
||||
*/
|
||||
export async function measureMedian(browser, url, profileName, runs = 3, options = {}) {
|
||||
|
||||
@@ -14,7 +14,8 @@ import { measureMedian } from './perf-lab.mjs'
|
||||
* and a desktop, first visit. The numbers are this site's own, measured on 2026-12-01 (docs/dev/perf/*-after.json)
|
||||
* with headroom for lab noise: a change that makes a page heavier or slower fails here, in `pnpm e2e:built`, with both
|
||||
* bundlers. Loosen a number only with a reason in the comment next to it.
|
||||
* PERF_RUNS (default 1) runs per page; the gate takes the median.
|
||||
* PERF_RUNS (default 1) runs per page; the gate takes the median. Third-party requests are recorded (none may come
|
||||
* before consent) and aborted, so an external host's speed cannot fail or hang the gate.
|
||||
*/
|
||||
const runs = Number(process.env.PERF_RUNS ?? 1)
|
||||
|
||||
@@ -56,7 +57,7 @@ test.describe('performance budgets', () => {
|
||||
for (const profile of ['mobile', 'desktop'] as const) {
|
||||
test(`${page.name} (${profile}) stays within its budget`, async ({ baseURL }) => {
|
||||
const budget = { ...budgets[profile], ...page.budget?.[profile] }
|
||||
const measured = await measureMedian(browser, `${baseURL}${page.path}`, profile, runs)
|
||||
const measured = await measureMedian(browser, `${baseURL}${page.path}`, profile, runs, { blockThirdParty: true })
|
||||
test.info().annotations.push({ description: JSON.stringify({ bytes: measured.bytes, cls: measured.cls, inp: measured.inp, lcp: measured.lcp, requests: measured.requests, tbt: measured.tbt }), type: 'measured' })
|
||||
expect(overBudget(measured, budget), `${page.path} ${profile}${page.why ? ` (${page.why})` : ''}`).toEqual([])
|
||||
// the LCP image is never lazy (web.dev/articles/lcp-lazy-loading)
|
||||
@@ -99,6 +100,7 @@ test.describe('queries per render', () => {
|
||||
expect(res.status).toBe(200)
|
||||
await db.command({ profile: 0 })
|
||||
counts[p] = await db.collection('system.profile').countDocuments({ ns: { $not: /system\.profile$/ }, op: 'query' })
|
||||
expect(counts[p], `${p}: no query counted — the profiler did not record this database`).toBeGreaterThan(0)
|
||||
expect(counts[p], `${p}: ${counts[p]} queries (budget ${limit}, plugin default ${SERVER_BUDGET.queriesPerRender})`).toBeLessThanOrEqual(limit)
|
||||
}
|
||||
test.info().annotations.push({ description: JSON.stringify(counts), type: 'queries' })
|
||||
|
||||
Reference in New Issue
Block a user