test(scale): 규모 측정을 참조 기능 대신 생성 기능 사본으로 잰다

규모 측정(S4 3.1절 S-6)은 참조 기능을 복사해 이름을 바꿔 잰다.

create:feature의 planFeature로 사본마다 --with-create --keyed 기능
파일을 만든다(자기 계약 패키지와 topic, context당 200개). 초기 JS
기록에 firstFeatureGzipBytes를 더하고, context 비용은 둘째 기능을 새
context에 둘 때와 같은 context에 둘 때의 차이로 잰다. 옛 정의는 기능이
없는 템플릿에서 첫 기능의 공용 코드까지 context 비용으로 세어 5,788B로
상한 512B를 넘었다. 몇 바이트짜리 비용에는 기록 허용치 하한 16B를
둔다(빌드 ID에 따라 gzip이 1~2B 흔들린다). 이 트리(샘플이 아직 있는
상태)의 기록: 204,266B, 27파일, 첫 기능 797B, 기능 521B, context 12B.

위험: 성장 기준선(0·25·100개)은 샘플을 지운 트리에서 뒤 커밋이 다시
잰다. 이 커밋의 기준선 파일은 아직 참조 사본으로 잰 값이다.

검증(desktop): feature-shape-growth 등 4파일 57 passed, test:unit 통과.
This commit is contained in:
DongHyeonka
2026-11-08 14:07:53 +09:00
parent 794a51a37d
commit b2042de9be
7 changed files with 208 additions and 170 deletions
+137 -105
View File
@@ -1,10 +1,12 @@
import { spawnSync } from "node:child_process";
import { cp, mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises";
import { cp, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { availableParallelism, cpus, loadavg, tmpdir } from "node:os";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { gzipSync } from "node:zlib";
import { PLATFORM_ROUTE_REGISTRY } from "../../src/contracts/routes.ts";
import { planFeature, type InstalledFacts } from "../create-feature.ts";
import { classifyViteJavascript } from "../lib/classify-vite-bundle.ts";
import { linkFixtureNodeModules } from "../lib/fixture-node-modules.ts";
import { VITE_BUILD_INPUTS } from "../lib/vite-build-inputs.ts";
@@ -12,11 +14,13 @@ import { parseTscExtendedDiagnostics, type TscDiagnostics } from "./collect-tsc-
/**
* 11-04 (D-18). Cost of one app as its feature count grows, measured with
* features shaped like the real one: `src/features/reference-feature` copied N
* times with every feature-owned identifier renamed (feature id, route ids and
* paths, API operations, schemas, query namespace, invalidation topic,
* messages). The copies live in contexts of their own (200 features each, the
* context ceiling) so the reference context is unchanged.
* features shaped like a real one: N copies of the feature `create:feature`
* writes with `--with-create --keyed` (a list, a detail and a create with its
* form), each planned by the command's own planner (`planFeature`) with its
* own ids and its own pinned contract package. The copies live in contexts of
* their own, 200 features each (the context ceiling the baseline records).
* 2026-11-07: the copies were the template's reference sample, renamed; the
* template ships no sample now.
*
* For each N the script runs the product generator, `tsc --extendedDiagnostics`
* over tsconfig.app.json and `vite build`, and records compiler counts, wall
@@ -36,70 +40,102 @@ export const FEATURE_SHAPE_BASELINE_PATH = "config/scale/feature-shape-growth.js
/** What the declared ceilings rest on, derived by hand from the baseline. */
export const FEATURE_SHAPE_CEILINGS_PATH = "config/scale/feature-shape-ceilings.json";
const SOURCE_FEATURE = "src/features/reference-feature";
const FEATURES_PER_CONTEXT = 200;
// Platform-owned names that only happen to contain "reference".
const PLATFORM_TOKENS = [
"REFERENCE_FEATURE_V1",
"supportReference",
] as const;
/** The command each copy is, as the baseline records it. */
export const SHAPE_FEATURE_COMMAND = "create:feature <id> --with-create --keyed";
export const FEATURES_PER_CONTEXT = 200;
/** Renames every feature-owned identifier of the reference feature for copy `id`. */
export function renameReferenceFeature(
text: string,
id: string,
context: Readonly<{ id: string; routeRoot: string }>,
): string {
const upper = id.toUpperCase();
// 1101/07-04. Assembly refuses the reference fixture source in any other
// feature, so a copy pins an external package and declares it in its
// contracts, as a product feature would.
const contractPackage = [
"Object.freeze({",
` packageId: "@feature-shape/${id}",`,
' version: "1.0.0",',
' digest: `sha256:${"0".repeat(64)}`,',
" runtimeProtocolVersion: 1 as const,",
' sourceRevision: "0000000",',
" })",
].join("\n");
let renamed = text
.replace(
/source: Object\.freeze\(\{\s*kind: "TEMPLATE_FIXTURE" as const,\s*fixtureId: "REFERENCE_FEATURE_V1" as const,\s*revision: 1 as const,\s*\}\)/u,
`source: Object.freeze({\n kind: "EXTERNAL_PACKAGE" as const,\n package: ${contractPackage},\n })`,
)
.replace(
"contractPackages: Object.freeze([]),",
`contractPackages: Object.freeze([${contractPackage}]),`,
/** Copy `index`: its feature id and the context it lands in. */
export function shapeFeature(index: number, featuresPerContext = FEATURES_PER_CONTEXT) {
const contextNumber = Math.floor(index / featuresPerContext);
return Object.freeze({
id: `s${String(index).padStart(3, "0")}-feature`,
context: Object.freeze({ id: `shape-context-${contextNumber}`, routeRoot: `shape-${contextNumber}` }),
});
}
function contextModule(context: Readonly<{ id: string; routeRoot: string }>): string {
return (
`export const CONTEXT_MODULE_ID = "${context.id}";\n` +
`export const CONTEXT_APP_ID = "template-app";\n` +
`export const CONTEXT_ROUTE_PATH = "${context.routeRoot}/*";\n` +
`export const CONTEXT_MAX_FEATURES = ${FEATURES_PER_CONTEXT};\n`
);
}
/**
* The source files of `features` copies: the context declarations and what
* `create:feature` writes under src/features (its tests and accessibility
* records are not part of a build). Each copy pins `@feature-shape/<id>`, as
* a product feature pins its published package, and gets a topic of its own.
*/
export function planShapeFeatures(
features: number,
featuresPerContext = FEATURES_PER_CONTEXT,
): ReadonlyMap<string, string> {
const files = new Map<string, string>();
const contexts: { id: string; routePath: string }[] = [];
const routes: InstalledFacts["routes"][number][] = Object.values(PLATFORM_ROUTE_REGISTRY).map(
(route) => ({
routeId: route.routeId,
path: route.path,
chunkId: route.chunkId,
navigationOrder: route.navigationOrder,
}),
);
const featureIds: string[] = [];
const topics: string[] = [];
for (let index = 0; index < features; index += 1) {
const { id, context } = shapeFeature(index, featuresPerContext);
if (index % featuresPerContext === 0) {
contexts.push({ id: context.id, routePath: `${context.routeRoot}/*` });
files.set(`src/contexts/${context.id}/context-module.ts`, contextModule(context));
}
const navigationOrder =
Math.max(0, ...routes.flatMap((route) => (route.navigationOrder === null ? [] : [route.navigationOrder]))) + 10;
const plan = planFeature(
{
id,
context: context.id,
write: "KEYED",
contractPackage: {
packageId: `@feature-shape/${id}`,
version: "1.0.0",
digest: `sha256:${"0".repeat(64)}`,
sourceRevision: "0000000",
},
},
{
appIds: ["template-app"],
featureIds,
contexts,
contextDirectories: contexts.map((declared) => declared.id),
routes,
topics,
messageKeys: [],
},
() => String(index).padStart(26, "0"),
);
PLATFORM_TOKENS.forEach((token, index) => {
renamed = renamed.replaceAll(token, `\u0000${index}\u0000`);
});
renamed = renamed
.replaceAll("/examples/reference-resources", `/${context.routeRoot}/${id}-resources`)
.replaceAll("qinv.01k10f7m3w9p6r2c8v5n4x", `qinv.shape.${id}`)
.replaceAll("NoRequest", `${upper}NoRequest`)
.replaceAll("reference-context", context.id)
.replaceAll("REFERENCE", upper)
.replaceAll("Reference", upper)
.replaceAll("reference", id);
PLATFORM_TOKENS.forEach((token, index) => {
renamed = renamed.replaceAll(`\u0000${index}\u0000`, token);
});
return renamed;
}
async function copyRenamed(from: string, to: string, rename: (text: string) => string) {
await mkdir(to, { recursive: true });
for (const entry of await readdir(from, { withFileTypes: true })) {
const source = path.join(from, entry.name);
const target = path.join(to, rename(entry.name));
if (entry.isDirectory()) await copyRenamed(source, target, rename);
else await writeFile(target, rename(await readFile(source, "utf8")));
for (const [file, content] of plan.files) {
if (file.startsWith("src/")) files.set(file, content);
}
featureIds.push(id);
topics.push(plan.topic);
for (const route of plan.routes) {
routes.push({
routeId: route.routeId,
path: route.path,
chunkId: `${route.routeId}-chunk`,
navigationOrder: route.routeId === plan.routeId ? navigationOrder : null,
});
}
}
return files;
}
async function createFixture(features: number): Promise<string> {
async function createFixture(
features: number,
featuresPerContext = FEATURES_PER_CONTEXT,
): Promise<string> {
const root = await mkdtemp(path.join(tmpdir(), `feature-shape-${features}-`));
for (const input of VITE_BUILD_INPUTS) {
await mkdir(path.dirname(path.join(root, input)), { recursive: true });
@@ -107,26 +143,9 @@ async function createFixture(features: number): Promise<string> {
}
await rm(path.join(root, "src/.generated"), { recursive: true, force: true });
await linkFixtureNodeModules(root);
for (let index = 0; index < features; index += 1) {
const contextNumber = Math.floor(index / FEATURES_PER_CONTEXT);
const context = { id: `shape-context-${contextNumber}`, routeRoot: `shape-${contextNumber}` };
if (index % FEATURES_PER_CONTEXT === 0) {
const contextFile = path.join(root, "src/contexts", context.id, "context-module.ts");
await mkdir(path.dirname(contextFile), { recursive: true });
await writeFile(
contextFile,
`export const CONTEXT_MODULE_ID = "${context.id}";\n` +
`export const CONTEXT_APP_ID = "template-app";\n` +
`export const CONTEXT_ROUTE_PATH = "${context.routeRoot}/*";\n` +
`export const CONTEXT_MAX_FEATURES = ${FEATURES_PER_CONTEXT};\n`,
);
}
const id = `s${String(index).padStart(3, "0")}`;
await copyRenamed(
path.resolve(SOURCE_FEATURE),
path.join(root, "src/features", `${id}-feature`),
(text) => renameReferenceFeature(text, id, context),
);
for (const [file, content] of planShapeFeatures(features, featuresPerContext)) {
await mkdir(path.dirname(path.join(root, file)), { recursive: true });
await writeFile(path.join(root, file), content);
}
return root;
}
@@ -166,15 +185,15 @@ async function javascriptGzipBytes(root: string, files: readonly string[]) {
}
/**
* 15-01. Initial JavaScript of the app with `features` reference copies
* added: the generator and `vite build` only, no typecheck. The unit suite
* 15-01. Initial JavaScript of the app with `features` copies added: the generator and `vite build` only, no typecheck. The unit suite
* (merge stage) bounds what one more feature adds to the initial chunk with
* it, so the growth is caught on the PR instead of at the next release.
*/
async function measureInitialJavascript(
features: number,
featuresPerContext = FEATURES_PER_CONTEXT,
): Promise<Readonly<{ gzipBytes: number; files: number }>> {
const root = await createFixture(features);
const root = await createFixture(features, featuresPerContext);
try {
run(process.execPath, [path.resolve("scripts/generate-product-feature-index.ts"), `--root=${root}`], root);
run(path.resolve("node_modules/.bin/vite"), ["build", "--logLevel", "error"], root);
@@ -187,17 +206,20 @@ async function measureInitialJavascript(
}
/**
* 1103/15-02, 15-04. The template's initial JavaScript and what one more
* feature and one more context add to it. The first copy lands in a new
* context (shape-context-0) and the second in the same one, so the second
* copy is a feature alone and the rest of the first is the context.
* `initialJavascript` in config/scale/feature-shape-ceilings.json records
* these numbers; tests/unit/feature-shape-growth.test.ts compares them with
* the build on every unit run.
* 1103/15-02, 15-04. The template's initial JavaScript and what features add
* to it. Two copies in one context against one copy are one more feature
* alone; the same two copies in two contexts against that are one more
* context. 2026-11-07: the template has no feature, so the first copy also
* brings the code every feature shares into the initial chunks; that is
* `firstFeatureGzipBytes`, paid once. `initialJavascript` in
* config/scale/feature-shape-ceilings.json records these numbers;
* tests/unit/feature-shape-growth.test.ts compares them with the build on
* every unit run.
*/
export type InitialJavascriptCosts = Readonly<{
gzipBytes: number;
files: number;
firstFeatureGzipBytes: number;
perFeatureGzipBytes: number;
perContextGzipBytes: number;
}>;
@@ -205,13 +227,14 @@ export type InitialJavascriptCosts = Readonly<{
export async function measureInitialJavascriptCosts(): Promise<InitialJavascriptCosts> {
const none = await measureInitialJavascript(0);
const one = await measureInitialJavascript(1);
const two = await measureInitialJavascript(2);
const perFeatureGzipBytes = two.gzipBytes - one.gzipBytes;
const twoInOneContext = await measureInitialJavascript(2);
const twoInTwoContexts = await measureInitialJavascript(2, 1);
return {
gzipBytes: none.gzipBytes,
files: none.files,
perFeatureGzipBytes,
perContextGzipBytes: one.gzipBytes - none.gzipBytes - perFeatureGzipBytes,
firstFeatureGzipBytes: one.gzipBytes - none.gzipBytes,
perFeatureGzipBytes: twoInOneContext.gzipBytes - one.gzipBytes,
perContextGzipBytes: twoInTwoContexts.gzipBytes - twoInOneContext.gzipBytes,
};
}
@@ -228,10 +251,18 @@ export const INITIAL_JAVASCRIPT_RECORD_TOLERANCE_BYTES = 256;
/**
* Fields of the record that no longer describe the build. The total may be
* INITIAL_JAVASCRIPT_RECORD_TOLERANCE_BYTES off; the per-feature and
* per-context costs are differences of builds with the same id but only a
* few hundred bytes, so 10%; the file count is exact.
* INITIAL_JAVASCRIPT_RECORD_TOLERANCE_BYTES off; the first-feature,
* per-feature and per-context costs are differences of builds with the same
* id, so 10%, and at least COST_RECORD_TOLERANCE_BYTES: a context costs a few
* bytes now, and gzip moves a difference that small by a byte or two when
* the build id changes. The file count is exact.
*/
export const COST_RECORD_TOLERANCE_BYTES = 16;
function costTolerance(bytes: number): number {
return Math.max(Math.abs(bytes) * 0.1, COST_RECORD_TOLERANCE_BYTES);
}
export function initialJavascriptRecordDrift(
measured: InitialJavascriptCosts,
record: InitialJavascriptCosts,
@@ -239,8 +270,9 @@ export function initialJavascriptRecordDrift(
const allowed: Readonly<Record<keyof InitialJavascriptCosts, number>> = {
gzipBytes: INITIAL_JAVASCRIPT_RECORD_TOLERANCE_BYTES,
files: 0,
perFeatureGzipBytes: Math.abs(measured.perFeatureGzipBytes) * 0.1,
perContextGzipBytes: Math.abs(measured.perContextGzipBytes) * 0.1,
firstFeatureGzipBytes: costTolerance(measured.firstFeatureGzipBytes),
perFeatureGzipBytes: costTolerance(measured.perFeatureGzipBytes),
perContextGzipBytes: costTolerance(measured.perContextGzipBytes),
};
return (Object.keys(allowed) as (keyof InitialJavascriptCosts)[])
.filter((field) => Math.abs(record[field] - measured[field]) > allowed[field])
@@ -393,7 +425,7 @@ async function main(): Promise<void> {
vite: version("vite"),
},
source,
sourceFeature: SOURCE_FEATURE,
sourceFeature: SHAPE_FEATURE_COMMAND,
runs,
};
const output = process.argv.includes("--write")