import { spawnSync } from "node:child_process"; import { cp, mkdir, mkdtemp, readdir, 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 { classifyViteJavascript } from "../lib/classify-vite-bundle.ts"; import { linkFixtureNodeModules } from "../lib/fixture-node-modules.ts"; import { parseTscExtendedDiagnostics, type TscDiagnostics } from "./collect-tsc-diagnostics.ts"; /** * 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. * * For each N the script runs the product generator, `tsc --extendedDiagnostics` * over tsconfig.app.json and `vite build`, and records compiler counts, wall * times and initial/lazy JavaScript gzip bytes. Deterministic counts repeat * exactly; times depend on the host, so the CPU and load average are recorded * next to them. A typecheck that fails (for example TS2590, a union too complex * to represent) is recorded as a result with its first error. * * node scripts/scale/measure-feature-shape-growth.ts [--sizes=0,25,100] [--repeats=2] [--write] * * `--write` stores the result as config/scale/feature-shape-growth.json. */ export const FEATURE_SHAPE_BASELINE_PATH = "config/scale/feature-shape-growth.json"; /** 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; const BUILD_INPUTS = [ "src", "public", "index.html", "package.json", "vite.config.ts", "tsconfig.base.json", "tsconfig.app.json", "scripts/lib/vite-module-inventory.ts", ] as const; // Platform-owned names that only happen to contain "reference". const PLATFORM_TOKENS = [ "REFERENCE_EXTERNAL_BEARER", "REFERENCE_FEATURE_V1", "supportReference", ] as const; /** 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(); let renamed = text; 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"))); } } async function createFixture(features: number): Promise { const root = await mkdtemp(path.join(tmpdir(), `feature-shape-${features}-`)); for (const input of BUILD_INPUTS) { await mkdir(path.dirname(path.join(root, input)), { recursive: true }); await cp(path.resolve(input), path.join(root, input), { recursive: true }); } 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), ); } return root; } function run( command: string, args: readonly string[], cwd: string, options: Readonly<{ allowFailure?: boolean }> = {}, ) { const started = performance.now(); const result = spawnSync(command, args, { cwd, encoding: "utf8", maxBuffer: 64 * 1024 * 1024, }); const ms = Math.round(performance.now() - started); if (result.status !== 0 && !(options.allowFailure && result.stdout.includes("Instantiations:"))) { throw new Error( `${path.basename(command)} ${args.join(" ")} failed in ${cwd}:\n${(result.stdout + result.stderr).slice(0, 4_000)}`, ); } return { ms, passed: result.status === 0, stdout: result.stdout }; } const load = () => loadavg().map((value) => Math.round(value * 100) / 100); async function javascriptGzipBytes(root: string, files: readonly string[]) { let bytes = 0; for (const file of files) bytes += gzipSync(await readFile(path.join(root, "dist", file))).byteLength; return bytes; } async function measureOnce(root: string, features: number) { const loadBefore = load(); run(process.execPath, [path.resolve("scripts/generate-product-feature-index.ts"), `--root=${root}`], root); // The composed registries throw at module evaluation on a duplicate route, // operation, schema, namespace or message key: prove the copies compose. const contracts = (await import( `${pathToFileURL(path.join(root, "src/features/installed-feature-contracts.ts")).href}?features=${features}` )) as typeof import("../../src/features/installed-feature-contracts.ts"); const routes = Object.keys(contracts.ROUTE_REGISTRY).length; const tsc = run( path.resolve("node_modules/.bin/tsc"), ["--project", path.join(root, "tsconfig.app.json"), "--noEmit", "--extendedDiagnostics"], root, // A type error is a result here (the typecheck stops scaling), not a crash. { allowFailure: true }, ); await rm(path.join(root, "dist"), { recursive: true, force: true }); const build = run(path.resolve("node_modules/.bin/vite"), ["build", "--logLevel", "error"], root); const manifest = JSON.parse(await readFile(path.join(root, "dist/.vite/manifest.json"), "utf8")); const javascript = classifyViteJavascript(manifest); const diagnostics: TscDiagnostics = parseTscExtendedDiagnostics(tsc.stdout); return { addedFeatures: features, routes, tsc: { passed: tsc.passed, firstError: tsc.stdout.split("\n").find((line) => line.includes("error TS")) ?? null, files: diagnostics.files, types: diagnostics.types, symbols: diagnostics.symbols, instantiations: diagnostics.instantiations, memoryKb: diagnostics.memoryKb ?? null, wallMs: tsc.ms, }, build: { wallMs: build.ms, initialJavascriptFiles: javascript.initialFiles.length, initialJavascriptGzipBytes: await javascriptGzipBytes(root, javascript.initialFiles), lazyJavascriptFiles: javascript.lazyFiles.length, lazyJavascriptGzipBytes: await javascriptGzipBytes(root, javascript.lazyFiles), }, loadBefore, loadAfter: load(), }; } function version(bin: string): string { const result = spawnSync(path.resolve("node_modules/.bin", bin), ["--version"], { encoding: "utf8" }); return (result.stdout ?? "").trim(); } async function main(): Promise { const argument = (name: string) => process.argv.find((entry) => entry.startsWith(`--${name}=`))?.slice(name.length + 3); const sizes = (argument("sizes") ?? "0,25,100").split(",").map(Number); const repeats = Number(argument("repeats") ?? "2"); if (!sizes.every((size) => Number.isSafeInteger(size) && size >= 0) || !(repeats >= 1)) { throw new TypeError("--sizes must be nonnegative integers and --repeats at least 1"); } const runs = []; for (const features of sizes) { const root = await createFixture(features); try { for (let repeat = 1; repeat <= repeats; repeat += 1) { const measured = await measureOnce(root, features); runs.push({ repeat, ...measured }); process.stdout.write( `features +${features} (repeat ${repeat}): routes=${measured.routes} ` + `instantiations=${measured.tsc.instantiations} tsc=${measured.tsc.wallMs}ms ` + `build=${measured.build.wallMs}ms initial=${measured.build.initialJavascriptGzipBytes}B ` + `lazy=${measured.build.lazyJavascriptGzipBytes}B load=${measured.loadBefore.join("/")}` + `${measured.tsc.passed ? "" : ` typecheck FAILED: ${measured.tsc.firstError}`}\n`, ); // Compiler results repeat exactly; a failed typecheck costs minutes and // gigabytes again for no new information. if (!measured.tsc.passed) break; } } finally { await rm(root, { recursive: true, force: true }); } } const cpu = cpus(); const result = { schemaVersion: 1, measuredAt: new Date().toISOString(), command: `node scripts/scale/measure-feature-shape-growth.ts --sizes=${sizes.join(",")} --repeats=${repeats}`, environment: { node: process.versions.node, platform: process.platform, arch: process.arch, cpuModel: cpu[0]?.model.trim() ?? "unknown", logicalCpus: cpu.length, availableParallelism: availableParallelism(), typescript: version("tsc"), vite: version("vite"), }, sourceFeature: SOURCE_FEATURE, runs, }; const output = process.argv.includes("--write") ? FEATURE_SHAPE_BASELINE_PATH : "artifacts/scale/feature-shape-growth.json"; await mkdir(path.dirname(output), { recursive: true }); await writeFile(output, `${JSON.stringify(result, null, 2)}\n`); process.stdout.write(`feature-shape growth: ${output}\n`); } if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) { await main(); }