export type BundleMeasurements = Readonly<{ initialJsGzipBytes: number; initialJsFiles: number; lazyChunks: readonly Readonly<{ path: string; gzipBytes: number }>[]; }>; /** * 1103/15-05. `initialJsFiles` bounds the number of initial JavaScript files * (entry included). The entry preloads them in parallel, which costs no round * trip over HTTP/2 and queues them six at a time over HTTP/1.1 * (docs/operations/performance-evidence.md, "초기 청크 수와 HTTP/1.1"). */ export type BundleThresholds = Readonly<{ initialJsGzipBytes: number; initialJsFiles: number; lazyChunkGzipBytes: number; }>; export function evaluateBundleBudget( measurements: BundleMeasurements, thresholds: BundleThresholds, ) { const initialPassed = measurements.initialJsGzipBytes <= thresholds.initialJsGzipBytes; const initialFilesPassed = measurements.initialJsFiles <= thresholds.initialJsFiles; const lazyResults = measurements.lazyChunks.map((chunk) => ({ ...chunk, threshold: thresholds.lazyChunkGzipBytes, passed: chunk.gzipBytes <= thresholds.lazyChunkGzipBytes, })); return Object.freeze({ initialPassed, initialFilesPassed, lazyResults: Object.freeze(lazyResults), passed: initialPassed && initialFilesPassed && lazyResults.every((chunk) => chunk.passed), }); } /** * 1103/15-03. One line per exceeded limit with the measured value, the limit * and the excess, so a failed check:bundle says what to cut without opening * artifacts/performance/bundle.json. */ export function describeBundleBudgetFailures( measurements: BundleMeasurements, thresholds: BundleThresholds, ): string[] { const over = (label: string, measured: number, limit: number, unit = " gzip bytes") => `${label} ${measured}${unit} > budget ${limit} (over by ${measured - limit})`; const lines: string[] = []; if (measurements.initialJsGzipBytes > thresholds.initialJsGzipBytes) { lines.push(over("initial JavaScript", measurements.initialJsGzipBytes, thresholds.initialJsGzipBytes)); } if (measurements.initialJsFiles > thresholds.initialJsFiles) { lines.push(over("initial JavaScript files", measurements.initialJsFiles, thresholds.initialJsFiles, "")); } for (const chunk of measurements.lazyChunks) { if (chunk.gzipBytes > thresholds.lazyChunkGzipBytes) { lines.push(over(`lazy chunk ${chunk.path}`, chunk.gzipBytes, thresholds.lazyChunkGzipBytes)); } } return lines; } /** * 1105/15-01. `targetDeepLinkCls` is the CLS of a direct visit to the lab's * target route, judged against the same `cls` threshold. The click run * cannot see a shift the target makes while it fills in: one within 500 ms * of the click is input-driven by definition. */ export type LabMetrics = Readonly<{ lcpMs: number; cls: number; namedInteractionMs: number; targetDeepLinkCls: number; /** * 1105/15-04. Click to heading on the first public navigation route an * installed feature owns (`labFeatureRoute`). `null` when there is none: * nothing to judge. */ featureRouteInteractionMs: number | null; }>; export type LabThresholds = Readonly<{ lcpMs: number; cls: number; namedInteractionMs: number; featureRouteInteractionMs: number; }>; export function evaluateLabBudget( report: Readonly<{ context?: Readonly>; metrics: LabMetrics; }>, thresholds: LabThresholds, ) { const requiredContext = [ "runner", "browser", "viewport", "network", "cpu", "cache", "build", ]; const missingContext = requiredContext.filter( (field) => report.context?.[field] === undefined, ); const results = { lcp: report.metrics.lcpMs <= thresholds.lcpMs, cls: report.metrics.cls <= thresholds.cls, namedInteraction: report.metrics.namedInteractionMs <= thresholds.namedInteractionMs, targetDeepLinkCls: report.metrics.targetDeepLinkCls <= thresholds.cls, featureRouteInteraction: report.metrics.featureRouteInteractionMs === null || report.metrics.featureRouteInteractionMs <= thresholds.featureRouteInteractionMs, }; return Object.freeze({ missingContext: Object.freeze(missingContext), results: Object.freeze(results), passed: missingContext.length === 0 && Object.values(results).every(Boolean), }); } export function percentile75(values: readonly number[]): number | null { if (values.length === 0) return null; const sorted = [...values].sort((left, right) => left - right); return sorted[Math.ceil(sorted.length * 0.75) - 1]; } export type FieldMetrics = Readonly<{ p75LcpMs: number | null; p75Cls: number | null; p75InpMs: number | null; }>; export type FieldThresholds = Readonly<{ p75LcpMs: number; p75Cls: number; p75InpMs: number; minimumEligibleSamples: number | null; }>; export type FieldBudgetResult = Readonly<{ status: "PASS" | "FAIL_THRESHOLD" | "FAIL_UNVERIFIED"; passed: boolean; }>; export function evaluateFieldBudget( report: Readonly<{ metrics: FieldMetrics; eligibleSamples: number }>, thresholds: FieldThresholds, ): FieldBudgetResult { if ( thresholds.minimumEligibleSamples === null || report.eligibleSamples < thresholds.minimumEligibleSamples || Object.values(report.metrics).some((value) => value === null) ) { return Object.freeze({ status: "FAIL_UNVERIFIED", passed: false, }); } const metrics = report.metrics as Readonly<{ p75LcpMs: number; p75Cls: number; p75InpMs: number; }>; const passed = metrics.p75LcpMs <= thresholds.p75LcpMs && metrics.p75Cls <= thresholds.p75Cls && metrics.p75InpMs <= thresholds.p75InpMs; return Object.freeze({ status: passed ? "PASS" : "FAIL_THRESHOLD", passed, }); }