Files
clean-architecture-frontend…/tests/unit/performance-budgets.test.ts
DongHyeonka 81701add7e feat(perf): lab이 기능 라우트 첫 진입을 재고 500 ms 예산으로 판정한다
문제: lab(FE-GATE-026)은 플랫폼 구성 화면(EXAMPLES_PLATFORM) 하나만 잰다.
이 화면은 기능 코드를 받지 않아 기능을 더해도 수치가 거의 그대로다
(146 → 154 ms). 기능 라우트는 처음 들어갈 때 context 라우트 모듈과 기능
실행부를 받고, 그다음 페이지 청크를 받는 두 번의 요청 묶음을 거친다.
lab 조건에서 EXAMPLES_UI 첫 진입은 중앙값 406 ms(403~410, 리뷰 15-R1)로
대상 화면의 2.7배인데 어느 게이트도 재지 않는다. 다운스트림의 화면은
모두 기능 라우트다.

고른 방법:
- labFeatureRoute(scripts/lib/lab-performance.ts): 설치된 기능이 가진,
  세션 없이 열리는 첫 내비게이션 라우트. 템플릿에서는 EXAMPLES_UI다.
  없으면 null이고 lab은 measured: false로 남기고 판정하지 않는다.
- lab은 run마다 `/`에서 그 라우트의 내비게이션 링크를 눌러 heading까지를
  같은 페이지 시계로 잰다(새 cold context). metrics.featureRouteInteractionMs
  (중앙값), context.sampling.featureRoute(표본, owner)에 남긴다. 기존 대상
  라우트와 그 예산(200 ms)은 그대로라 회귀 비교가 이어진다.
- 예산 config/performance/budgets.json lab.featureRouteInteractionMs = 500 ms.
  측정 406 ms(8a44226, i5-10400F, lab 조건)를 통과시키고, 지금 둘째 요청
  묶음 크기(약 100 ms, 클릭 뒤 98~204 ms)의 왕복이 하나 더 붙으면 넘는다.
  lab 자가 검사 fixture에 feature-route-over-threshold를 더해 이 예산이
  실패할 수 있음을 매번 확인한다.
- 단위 테스트: 라우트 선택 규칙, 설치된 레지스트리에서의 선택, 커밋된
  예산이 406 ms는 통과·516 ms는 실패·null은 판정 제외.
버린 대안:
- 두 요청 묶음을 하나로 줄이기(진입한 URL의 페이지 청크를 context 라우트
  모듈과 같이 받기): 페이지 청크 이름이 context 라우트 모듈 안의 lazy()에만
  있어, 첫 묶음에서 받으려면 초기 청크의 라우트 선언이 페이지 로더를 가져야
  한다. 선언 모양·생성기·초기 JS를 바꾸는 일이라 이 커밋에서 하지 않았다
  (결정 대기, performance-evidence.md에 적음).
- 처음에는 기록만 하기: 기록만 하면 늘어도 아무도 모른다. 측정값이 있으니
  예산을 같이 둔다.
- 라우트별 예산 표: 다운스트림이 채워야 하는 표가 는다.

위험·한계: 예산 여유가 94 ms(23%)라 lab 호스트가 지금보다 20% 이상 느리면
기능 라우트 판정이 흔들릴 수 있다(0929/11-04 lab 부하 가드는 여전히 없음).
FE-GATE-026 시간이 run마다 cold navigation 하나(약 2~3초) 늘어 5회면
+10~15초다. 생성 기능의 목록 라우트는 session-required라 lab 대상이 되지
않는다. 기능 라우트의 heading은 계약 `title`과 같은 글자로 찾는다(대상
라우트와 같은 방식). CHANGELOG의 "기능 라우트 첫 진입은 어느 게이트도
재지 않는다" 제한 문장은 이 커밋에서 고치지 않았다(오케스트레이터 몫).

검증: desktop batch에서 확인.
node scripts/run-vitest.ts run tests/unit/lab-performance.test.ts
tests/unit/performance-budgets.test.ts
corepack pnpm build && corepack pnpm test:performance -- --runs=5
(featureRoute EXAMPLES_UI 중앙값이 500 ms 안인지, 표본 기록)

Refs: 1105/15-04
2026-11-06 01:23:48 +09:00

157 lines
5.1 KiB
TypeScript

import { readFile } from "node:fs/promises";
import { describe, expect, it } from "vitest";
import {
evaluateBundleBudget,
evaluateFieldBudget,
evaluateLabBudget,
percentile75,
} from "../../scripts/lib/performance-budgets.ts";
describe("performance budgets", () => {
it("rejects initial and lazy JavaScript above their named limits", () => {
const thresholds = {
initialJsGzipBytes: 200,
initialJsFiles: 2,
lazyChunkGzipBytes: 120,
};
expect(
evaluateBundleBudget(
{ initialJsGzipBytes: 201, initialJsFiles: 1, lazyChunks: [] },
thresholds,
).passed,
).toBe(false);
expect(
evaluateBundleBudget(
{
initialJsGzipBytes: 100,
initialJsFiles: 1,
lazyChunks: [{ path: "lazy.js", gzipBytes: 121 }],
},
thresholds,
).passed,
).toBe(false);
expect(
evaluateBundleBudget(
{ initialJsGzipBytes: 100, initialJsFiles: 3, lazyChunks: [] },
thresholds,
),
).toMatchObject({ initialPassed: true, initialFilesPassed: false, passed: false });
});
it("fails lab evidence when context is absent or a metric is over budget", () => {
const thresholds = { lcpMs: 2500, cls: 0.1, namedInteractionMs: 200, featureRouteInteractionMs: 500 };
expect(
evaluateLabBudget(
{ metrics: { lcpMs: 1000, cls: 0, namedInteractionMs: 50, targetDeepLinkCls: 0, featureRouteInteractionMs: null } },
thresholds,
),
).toMatchObject({ passed: false });
expect(
evaluateLabBudget(
{
context: {
runner: {},
browser: {},
viewport: {},
network: {},
cpu: {},
cache: {},
build: {},
},
metrics: { lcpMs: 2501, cls: 0, namedInteractionMs: 50, targetDeepLinkCls: 0, featureRouteInteractionMs: null },
},
thresholds,
).passed,
).toBe(false);
});
/**
* 1105/15-01. The lab entered its target route only by a click, and a
* shift within 500 ms of a click is left out of CLS. The target screen
* pushed its content 21 px after its title and the lab reported 0. The
* direct visit's CLS is judged against the same threshold.
*/
it("fails lab evidence when a direct visit to the target route shifts over the CLS budget", () => {
const thresholds = { lcpMs: 2500, cls: 0.1, namedInteractionMs: 200, featureRouteInteractionMs: 500 };
const context = {
runner: {},
browser: {},
viewport: {},
network: {},
cpu: {},
cache: {},
build: {},
};
const metrics = { lcpMs: 1000, cls: 0, namedInteractionMs: 50, targetDeepLinkCls: 0.0046, featureRouteInteractionMs: 406 };
expect(evaluateLabBudget({ context, metrics }, thresholds).passed).toBe(true);
const shifted = evaluateLabBudget(
{ context, metrics: { ...metrics, targetDeepLinkCls: 0.11 } },
thresholds,
);
expect(shifted.passed).toBe(false);
expect(shifted.results).toMatchObject({ cls: true, targetDeepLinkCls: false });
});
/**
* 1105/15-04. The lab timed only the platform screen, which loads no
* feature code. A feature route's first entry took 406 ms (403-410) under
* the lab conditions at 8a44226, two request rounds (the context route
* module, then the page chunk, the second about 100 ms), and no gate
* measured it. The committed budget passes that entry and fails it with
* one more round of that size.
*/
it("judges the feature route entry against its committed budget", async () => {
const { lab: thresholds } = JSON.parse(
await readFile("config/performance/budgets.json", "utf8"),
) as { lab: Parameters<typeof evaluateLabBudget>[1] };
const context = {
runner: {},
browser: {},
viewport: {},
network: {},
cpu: {},
cache: {},
build: {},
};
const metrics = {
lcpMs: 1000,
cls: 0,
namedInteractionMs: 150,
targetDeepLinkCls: 0,
featureRouteInteractionMs: 406,
};
const judged = (featureRouteInteractionMs: number | null) =>
evaluateLabBudget({ context, metrics: { ...metrics, featureRouteInteractionMs } }, thresholds);
expect(thresholds.featureRouteInteractionMs).toEqual(expect.any(Number));
expect(judged(406).passed).toBe(true);
expect(judged(406 + 110)).toMatchObject({
passed: false,
results: { namedInteraction: true, featureRouteInteraction: false },
});
// No installed feature owns a public navigation route: nothing to judge.
expect(judged(null).passed).toBe(true);
});
it("uses the nearest-rank p75 and fails closed while sample minimum is deferred", () => {
expect(percentile75([4, 1, 3, 2])).toBe(3);
expect(
evaluateFieldBudget(
{
metrics: { p75LcpMs: 1000, p75Cls: 0.01, p75InpMs: 50 },
eligibleSamples: 100,
},
{
p75LcpMs: 2500,
p75Cls: 0.1,
p75InpMs: 200,
minimumEligibleSamples: null,
},
),
).toEqual({ status: "FAIL_UNVERIFIED", passed: false });
});
});