chore: sync the frontend template from ebea300 to af3d8e6
Carries 359 template commits, tag v0.2.0 plus one documentation commit: the generator-built feature index with its app, bounded-context and feature declarations, the src/platform kernel and scale tooling, the 2026-09-24 src review fixes, and the 2026-09-25 maturity review round with its CHANGELOG [보안]/[계약] rows. 691 of the 824 template paths are applied, 85 of them merged with product changes; 127 are not carried. Product identity is unchanged: name, version 0.1.0, routes, design and the TechLog domain. Three decisions worth naming. TechLog stays composed directly, as the last two syncs recorded; its directory carries a NOT_INSTALLED marker that says so, and every installed-* composer takes the template's projection with TechLog added on top. The reference sample is no longer installed: the template now installs a feature only through feature-module.ts and context-routes.ts, which load the screens this product deleted, so its three operations, six schemas and one topic leave the registries, recorded as registry change evidence. And AppRouter mounts the platform and TechLog routes only; the template's bounded-context route gateway waits for a generator-installed feature. Required fixes: readSubject on the BFF session owner (without it every HTTP Studio session would close as integration-failed), zod through the facade in nine TechLog files, noUnusedLocals, and public/config.json written out to match the local profile. Derived baselines were recomputed after reproducing both sides' values: CI contract 101 commands / 106 references / 141 artifacts, gate shape c9adcf85; registry approval 2dc6c3a3; the runtime config schema baseline equals the schema at8210d48. Verified here: types, lint, architecture, adapter inventory, CI contract, i18n, design system, diagnostics, browser security, dev release manifest, TechLog contract and build pass; 220 component, 155 integration, 15 contract, 418 TechLog, 43 reference-feature, 18 recipe and 46 release-coherence tests pass, and 2381 of 2383 unit tests. Reproduced at8210d48first and unchanged: check:registries (byte-identical), test:runtime-schema, verify:documentation. Brought by the template and escalated rather than suppressed: check:request-fanout on TechLog's cursor pagination, check:remediation-ledger (fails upstream too), the shared-chunk hash test, and the registry-dependent sample removal and second-feature tests. Playwright suites were not run. Full record, the five open questions and the follow-ups in docs/operations/template-sync-2026-09-29.md. (cherry picked from commit b2170cf265dea45dc9cc05d3c61e627e97f31cdb)
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { benchmark } from "../../scripts/scale/benchmark.ts";
|
||||
import {
|
||||
assertBenchmarkResult,
|
||||
median,
|
||||
} from "../../scripts/scale/benchmark-result.ts";
|
||||
|
||||
function fakeDurations(values: readonly number[]): () => number {
|
||||
let index = 0;
|
||||
return () => values[index++ % values.length] as number;
|
||||
}
|
||||
|
||||
describe("benchmark runner", () => {
|
||||
it("records median rather than a single timing sample", async () => {
|
||||
const result = await benchmark("compose", {
|
||||
samples: 5,
|
||||
run: fakeDurations([10, 11, 100, 12, 9]),
|
||||
});
|
||||
|
||||
expect(result.medianMs).toBe(11);
|
||||
expect(result.samples).toHaveLength(5);
|
||||
});
|
||||
|
||||
it("runs warm-up iterations that are not sampled", async () => {
|
||||
let calls = 0;
|
||||
const result = await benchmark("warm", {
|
||||
samples: 3,
|
||||
warmup: 2,
|
||||
run: () => {
|
||||
calls += 1;
|
||||
return calls;
|
||||
},
|
||||
});
|
||||
expect(calls).toBe(5);
|
||||
expect(result.samples).toEqual([3, 4, 5]);
|
||||
});
|
||||
|
||||
it("produces the machine-readable result schema", async () => {
|
||||
const result = await benchmark("registry-composition", {
|
||||
samples: 3,
|
||||
profile: "S2",
|
||||
cardinality: 10_000,
|
||||
measureHeap: true,
|
||||
metadata: { implementation: "composeUniqueRecord" },
|
||||
run: () => {
|
||||
Array.from({ length: 1_000 }, (_, i) => i);
|
||||
},
|
||||
});
|
||||
expect(result).toMatchObject({
|
||||
name: "registry-composition",
|
||||
profile: "S2",
|
||||
cardinality: 10_000,
|
||||
metadata: { implementation: "composeUniqueRecord" },
|
||||
});
|
||||
expect(result.medianMs).toBeGreaterThanOrEqual(0);
|
||||
expect(typeof result.heapBytes).toBe("number");
|
||||
});
|
||||
|
||||
it("computes even-length medians and rejects inconsistent results", () => {
|
||||
expect(median([4, 1, 3, 2])).toBe(2.5);
|
||||
expect(() =>
|
||||
assertBenchmarkResult({
|
||||
name: "x",
|
||||
profile: "S0",
|
||||
cardinality: 1,
|
||||
samples: [1, 2, 3],
|
||||
medianMs: 3,
|
||||
metadata: {},
|
||||
}),
|
||||
).toThrow(/median/);
|
||||
expect(() =>
|
||||
assertBenchmarkResult({
|
||||
name: "x",
|
||||
profile: "S9" as never,
|
||||
cardinality: 1,
|
||||
samples: [1],
|
||||
medianMs: 1,
|
||||
metadata: {},
|
||||
}),
|
||||
).toThrow(/profile/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,153 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
classifyCounterSeries,
|
||||
classifyMemorySlope,
|
||||
linearSlope,
|
||||
} from "../../scripts/scale/memory-slope.ts";
|
||||
import {
|
||||
evaluateSoak,
|
||||
runBrowserSoak,
|
||||
summarizeSoak,
|
||||
} from "../../scripts/scale/browser-soak.ts";
|
||||
|
||||
describe("soak slope classifier", () => {
|
||||
it("flags a persistent positive heap slope after warmup", () => {
|
||||
expect(
|
||||
classifyMemorySlope([
|
||||
{ minute: 10, heapMb: 100 },
|
||||
{ minute: 20, heapMb: 200 },
|
||||
{ minute: 30, heapMb: 300 },
|
||||
]),
|
||||
).toBe("unbounded-growth");
|
||||
});
|
||||
|
||||
it("treats warm-up growth followed by a plateau as stable", () => {
|
||||
expect(
|
||||
classifyMemorySlope(
|
||||
[
|
||||
{ minute: 0, heapMb: 20 },
|
||||
{ minute: 5, heapMb: 80 },
|
||||
{ minute: 10, heapMb: 100 },
|
||||
{ minute: 20, heapMb: 101 },
|
||||
{ minute: 30, heapMb: 99 },
|
||||
{ minute: 40, heapMb: 100.5 },
|
||||
],
|
||||
{ warmupMinutes: 10 },
|
||||
),
|
||||
).toBe("stable");
|
||||
});
|
||||
|
||||
it("does not flag GC sawtooth noise around a flat baseline", () => {
|
||||
const sawtooth = Array.from({ length: 40 }, (_, minute) => ({
|
||||
minute,
|
||||
heapMb: 100 + (minute % 4) * 3,
|
||||
}));
|
||||
expect(classifyMemorySlope(sawtooth)).toBe("stable");
|
||||
});
|
||||
|
||||
it("needs enough post-warm-up evidence", () => {
|
||||
expect(classifyMemorySlope([{ minute: 1, heapMb: 1 }, { minute: 2, heapMb: 2 }])).toBe(
|
||||
"insufficient-data",
|
||||
);
|
||||
});
|
||||
|
||||
it("classifies logical counters and computes a least-squares slope", () => {
|
||||
expect(linearSlope([{ x: 0, y: 1 }, { x: 1, y: 3 }, { x: 2, y: 5 }])).toBeCloseTo(2);
|
||||
expect(classifyCounterSeries([0, 0, 0, 0, 0])).toBe("stable");
|
||||
expect(classifyCounterSeries([1, 2, 3, 4, 5])).toBe("unbounded-growth");
|
||||
});
|
||||
|
||||
it("summarizes a browser series recorded as { elapsedMs, heapBytes, domNodes, activeTimers }", () => {
|
||||
const mb = 1024 * 1024;
|
||||
const flat = Array.from({ length: 12 }, (_, index) => ({
|
||||
elapsedMs: index * 10_000,
|
||||
heapBytes: 40 * mb + (index % 2) * mb,
|
||||
domNodes: 900,
|
||||
listeners: 120,
|
||||
activeTimers: 3,
|
||||
}));
|
||||
const leaking = flat.map((sample, index) => ({
|
||||
...sample,
|
||||
heapBytes: 40 * mb + index * 8 * mb,
|
||||
}));
|
||||
expect(summarizeSoak(flat, 20_000)).toBe("stable");
|
||||
expect(summarizeSoak(leaking, 20_000)).toBe("unbounded-growth");
|
||||
});
|
||||
});
|
||||
|
||||
describe("soak evaluation", () => {
|
||||
const base = (index: number) => ({
|
||||
elapsedMs: index * 5_000,
|
||||
cycles: index * 100,
|
||||
heapBytes: 40 * 1024 * 1024,
|
||||
domNodes: 900,
|
||||
listeners: 120,
|
||||
activeTimers: 2,
|
||||
inFlightRequests: 0,
|
||||
storageKeys: 1,
|
||||
storageBytes: 100,
|
||||
});
|
||||
const healthySamples = Array.from({ length: 12 }, (_, index) => base(index));
|
||||
const totals = {
|
||||
cycles: 1_200,
|
||||
navigationFailures: 0,
|
||||
referenceVisits: 200,
|
||||
settledReferenceVisits: 40,
|
||||
finalInFlightRequests: 0,
|
||||
apiRequests: 240,
|
||||
telemetryPosts: 5,
|
||||
telemetryEvents: 6,
|
||||
telemetryPostTimes: [1_000, 20_000, 40_000, 70_000, 90_000],
|
||||
};
|
||||
|
||||
it("passes a healthy long session", () => {
|
||||
const { checks } = evaluateSoak(healthySamples, totals, 10_000);
|
||||
expect(checks.filter((check) => !check.passed)).toEqual([]);
|
||||
});
|
||||
|
||||
it("fails a vacuous soak that never exercised the backend or telemetry", () => {
|
||||
const { checks } = evaluateSoak(healthySamples, { ...totals, apiRequests: 0, telemetryEvents: 0 }, 10_000);
|
||||
expect(checks.find((check) => check.name === "backend failure exercised")?.passed).toBe(false);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["timers", healthySamples.map((sample, index) => ({ ...sample, activeTimers: 2 + index * 3 }))],
|
||||
["listeners", healthySamples.map((sample, index) => ({ ...sample, listeners: 120 + index * 20 }))],
|
||||
["storage growth", healthySamples.map((sample, index) => ({ ...sample, storageKeys: 1 + index }))],
|
||||
] as const)("fails a %s leak", (name, samples) => {
|
||||
const { checks } = evaluateSoak(samples, totals, 10_000);
|
||||
expect(checks.find((check) => check.name === name)?.passed).toBe(false);
|
||||
});
|
||||
|
||||
it("fails request amplification and a telemetry storm", () => {
|
||||
const { checks } = evaluateSoak(
|
||||
healthySamples,
|
||||
{
|
||||
...totals,
|
||||
apiRequests: 1_000,
|
||||
telemetryPostTimes: Array.from({ length: 100 }, (_, index) => index * 100),
|
||||
},
|
||||
10_000,
|
||||
);
|
||||
expect(checks.find((check) => check.name === "request amplification")?.passed).toBe(false);
|
||||
expect(checks.find((check) => check.name === "telemetry rate")?.passed).toBe(false);
|
||||
});
|
||||
|
||||
it("fails too short a soak and requests that never settle", () => {
|
||||
const { checks } = evaluateSoak(healthySamples.slice(0, 3), { ...totals, cycles: 10, finalInFlightRequests: 2 }, 10_000);
|
||||
expect(checks.find((check) => check.name === "cycles")?.passed).toBe(false);
|
||||
expect(checks.find((check) => check.name === "in-flight settles")?.passed).toBe(false);
|
||||
});
|
||||
|
||||
it("reports skipped (never passed) without a build", async () => {
|
||||
const result = await runBrowserSoak({
|
||||
distRoot: "does-not-exist",
|
||||
durationMs: 1,
|
||||
intervalMs: 1,
|
||||
warmupMs: 0,
|
||||
routes: ["/"],
|
||||
});
|
||||
expect(result.status).toBe("skipped");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { composeUniqueRecord } from "../../src/platform/cardinality/compose-unique-record.ts";
|
||||
import {
|
||||
COMPOSITION_WORKLOADS,
|
||||
createUniqueRegistryParts,
|
||||
} from "../../scripts/scale/composition-workloads.ts";
|
||||
import { runCompositionScale } from "../../scripts/scale/run-composition-scale.ts";
|
||||
|
||||
describe("registry/composition scale (S0-S2)", () => {
|
||||
it.each([100, 1_000, 10_000])(
|
||||
"composes %i unique entries without duplicates",
|
||||
(count) => {
|
||||
const parts = createUniqueRegistryParts(count);
|
||||
const result = composeUniqueRecord(parts, () => "route");
|
||||
|
||||
expect(Object.keys(result)).toHaveLength(count);
|
||||
},
|
||||
);
|
||||
|
||||
it("rejects a duplicate at the largest profile deterministically", () => {
|
||||
const parts = [...createUniqueRegistryParts(10_000), { "route-42": -1 }];
|
||||
expect(() => composeUniqueRecord(parts, () => "route")).toThrow(
|
||||
"Duplicate route key: route-42",
|
||||
);
|
||||
});
|
||||
|
||||
it("runs every production workload at every profile", () => {
|
||||
for (const workload of COMPOSITION_WORKLOADS) {
|
||||
for (const n of [100, 1_000, 10_000]) {
|
||||
expect(workload.prepare(n)()).toBeDefined();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it(
|
||||
"keeps the S0/S1/S2 composition series linear-or-better with machine-readable results",
|
||||
async () => {
|
||||
const measure = () =>
|
||||
runCompositionScale(["S0", "S1", "S2"], {
|
||||
samples: 5,
|
||||
warmup: 1,
|
||||
// This suite runs beside other suites; see `statistic` in the runner.
|
||||
statistic: "min",
|
||||
});
|
||||
// Review focus: one noisy measurement must not fail a linear path. A
|
||||
// real super-linear path fails every independent re-measurement.
|
||||
let report = await measure();
|
||||
if (!report.passed) report = await measure();
|
||||
|
||||
expect(report.checks.filter((check) => !check.passed)).toEqual([]);
|
||||
expect(report.results).toHaveLength(COMPOSITION_WORKLOADS.length * 3);
|
||||
for (const result of report.results) {
|
||||
expect(result.samples).toHaveLength(5);
|
||||
expect(result.metadata["implementation"]).toBeTypeOf("string");
|
||||
}
|
||||
},
|
||||
60_000,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,139 @@
|
||||
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
/**
|
||||
* Writes a real multi-app repository layout on disk, using the same ownership
|
||||
* declarations the production generator reads:
|
||||
*
|
||||
* src/apps/<app>/app-module.ts
|
||||
* src/contexts/<context>/context-module.ts
|
||||
* src/features/<feature>/{feature-module,context-routes}.ts + presentation/
|
||||
*
|
||||
* App A is fixed. Every other logical feature belongs to other apps, so growing
|
||||
* `globalFeatures` must leave App A's generated output and source graph alone.
|
||||
*/
|
||||
export const APP_A = Object.freeze({ id: "app-a", contexts: 2, featuresPerContext: 50 });
|
||||
const OTHER_CONTEXTS_PER_APP = 10;
|
||||
const OTHER_FEATURES_PER_CONTEXT = 100;
|
||||
|
||||
async function write(file: string, content: string): Promise<void> {
|
||||
await mkdir(path.dirname(file), { recursive: true });
|
||||
await writeFile(file, content);
|
||||
}
|
||||
|
||||
async function writeFeature(root: string, id: string, contextId: string): Promise<void> {
|
||||
const dir = path.join(root, "src/features", id);
|
||||
await write(
|
||||
path.join(dir, "feature-module.ts"),
|
||||
`import { PAGE } from "./presentation/page.ts";\n` +
|
||||
`export const FEATURE_MODULE_ID = "${id}";\nexport const FEATURE_CONTEXT_ID = "${contextId}";\n` +
|
||||
`export const FEATURE_MODULE = Object.freeze({ id: FEATURE_MODULE_ID, page: PAGE });\n`,
|
||||
);
|
||||
await write(
|
||||
path.join(dir, "context-routes.ts"),
|
||||
`import { PAGE } from "./presentation/page.ts";\nexport const FEATURE_CONTEXT_ROUTES = [{ id: "${id}", page: PAGE }];\n`,
|
||||
);
|
||||
await write(path.join(dir, "presentation/page.ts"), `export const PAGE = "${id}-page";\n`);
|
||||
}
|
||||
|
||||
async function writeContext(root: string, id: string, appId: string, maxFeatures: number) {
|
||||
await write(
|
||||
path.join(root, "src/contexts", id, "context-module.ts"),
|
||||
`export const CONTEXT_MODULE_ID = "${id}";\nexport const CONTEXT_APP_ID = "${appId}";\n` +
|
||||
`export const CONTEXT_ROUTE_PATH = "${id}/*";\nexport const CONTEXT_MAX_FEATURES = ${maxFeatures};\n`,
|
||||
);
|
||||
}
|
||||
|
||||
async function writeApp(root: string, id: string, maxContexts: number) {
|
||||
await write(
|
||||
path.join(root, "src/apps", id, "app-module.ts"),
|
||||
`export const APP_MODULE_ID = "${id}";\nexport const APP_MAX_CONTEXTS = ${maxContexts};\n`,
|
||||
);
|
||||
}
|
||||
|
||||
export async function writeFilesystemTopology(
|
||||
root: string,
|
||||
options: Readonly<{ globalFeatures: number }>,
|
||||
): Promise<Readonly<{ apps: number; features: number }>> {
|
||||
await writeApp(root, APP_A.id, 20);
|
||||
let written = 0;
|
||||
for (let context = 0; context < APP_A.contexts; context += 1) {
|
||||
const contextId = `${APP_A.id}-ctx${context}`;
|
||||
await writeContext(root, contextId, APP_A.id, 200);
|
||||
for (let feature = 0; feature < APP_A.featuresPerContext; feature += 1) {
|
||||
await writeFeature(root, `${contextId}-f${feature}`, contextId);
|
||||
written += 1;
|
||||
}
|
||||
}
|
||||
let apps = 1;
|
||||
for (let appIndex = 0; written < options.globalFeatures; appIndex += 1) {
|
||||
const appId = `other-${appIndex}`;
|
||||
await writeApp(root, appId, 20);
|
||||
apps += 1;
|
||||
for (let context = 0; context < OTHER_CONTEXTS_PER_APP && written < options.globalFeatures; context += 1) {
|
||||
const contextId = `${appId}-ctx${context}`;
|
||||
await writeContext(root, contextId, appId, 200);
|
||||
for (let feature = 0; feature < OTHER_FEATURES_PER_CONTEXT && written < options.globalFeatures; feature += 1) {
|
||||
await writeFeature(root, `${contextId}-f${feature}`, contextId);
|
||||
written += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return { apps, features: written };
|
||||
}
|
||||
|
||||
const IMPORT = /(?:import|export)\s[^"'`]*?from\s+["']([^"']+)["']|import\(\s*["']([^"']+)["']\s*\)/gu;
|
||||
|
||||
/**
|
||||
* Static source graph reachable from the generated indexes: every relative
|
||||
* import (static and lazy) followed transitively on disk. This is the module
|
||||
* set the app's bundler and type-checker are given from these entries.
|
||||
*/
|
||||
export async function reachableSourceGraph(
|
||||
root: string,
|
||||
entries: readonly string[],
|
||||
): Promise<readonly string[]> {
|
||||
const seen = new Set<string>();
|
||||
const pending = entries.map((entry) => path.resolve(root, entry));
|
||||
while (pending.length > 0) {
|
||||
const file = pending.pop() as string;
|
||||
if (seen.has(file)) continue;
|
||||
let source: string;
|
||||
try {
|
||||
source = await readFile(file, "utf8");
|
||||
} catch {
|
||||
continue; // outside this synthetic repository (e.g. platform modules)
|
||||
}
|
||||
seen.add(file);
|
||||
for (const match of source.matchAll(IMPORT)) {
|
||||
const specifier = match[1] ?? match[2];
|
||||
if (specifier?.startsWith(".")) pending.push(path.resolve(path.dirname(file), specifier));
|
||||
}
|
||||
}
|
||||
return [...seen].map((file) => path.relative(root, file)).sort();
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the given apps of a synthetic topology as real declarations, so the
|
||||
* filesystem source and the synthetic declaration source can be compared.
|
||||
*/
|
||||
export async function writeSyntheticApps(
|
||||
root: string,
|
||||
apps: readonly Readonly<{
|
||||
id: string;
|
||||
maxContexts: number;
|
||||
contexts: readonly Readonly<{ id: string; routePath: string; maxFeatures: number; featureIds: readonly string[] }>[];
|
||||
}>[],
|
||||
): Promise<void> {
|
||||
for (const app of apps) {
|
||||
await writeApp(root, app.id, app.maxContexts);
|
||||
for (const context of app.contexts) {
|
||||
await write(
|
||||
path.join(root, "src/contexts", context.id, "context-module.ts"),
|
||||
`export const CONTEXT_MODULE_ID = "${context.id}";\nexport const CONTEXT_APP_ID = "${app.id}";\n` +
|
||||
`export const CONTEXT_ROUTE_PATH = "${context.routePath}";\nexport const CONTEXT_MAX_FEATURES = ${context.maxFeatures};\n`,
|
||||
);
|
||||
for (const featureId of context.featureIds) await writeFeature(root, featureId, context.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
Files: 821
|
||||
Lines: 361168
|
||||
Identifiers: 428183
|
||||
Symbols: 779934
|
||||
Types: 277593
|
||||
Instantiations: 698528
|
||||
Memory used: 511482K
|
||||
Memory allocs: 2977087
|
||||
Config time: 0.002s
|
||||
Parse time: 0.145s
|
||||
Bind time: 0.031s
|
||||
Check time: 0.883s
|
||||
Emit time: 0.000s
|
||||
Total time: 1.064s
|
||||
@@ -0,0 +1,172 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { afterAll, describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
generateAppIndexes,
|
||||
selectAppIndexes,
|
||||
} from "../../scripts/generate-product-feature-index.ts";
|
||||
import {
|
||||
APP_A,
|
||||
reachableSourceGraph,
|
||||
writeFilesystemTopology,
|
||||
writeSyntheticApps,
|
||||
} from "./filesystem-topology.ts";
|
||||
import { APP_CONTEXT_BUDGET, CONTEXT_FEATURE_BUDGET, SCALE_PROFILES } from "./scale-profile.ts";
|
||||
import {
|
||||
createSyntheticTopology,
|
||||
declarationSource,
|
||||
selectAppInputs,
|
||||
} from "./synthetic-topology.ts";
|
||||
|
||||
/**
|
||||
* Spec §37. The central 1M architectural proof, on the production path.
|
||||
*
|
||||
* - S0-S2: real on-disk repositories, generated by the production command
|
||||
* (`scripts/generate-product-feature-index.ts --app=`), whose discovery reads
|
||||
* app/context/feature ownership declarations.
|
||||
* - S3/S4: the same production selection (`selectAppIndexes`) over a
|
||||
* declaration source that models 100k/1M logical features without writing
|
||||
* them; an equivalence test pins that source to the filesystem one.
|
||||
*/
|
||||
const GENERATOR = path.resolve("scripts/generate-product-feature-index.ts");
|
||||
const OUTPUTS = [
|
||||
"src/.generated/product-feature-index.ts",
|
||||
"src/.generated/product-context-index.ts",
|
||||
"src/.generated/product-app.ts",
|
||||
] as const;
|
||||
const roots: string[] = [];
|
||||
|
||||
afterAll(async () => {
|
||||
await Promise.all(roots.map((root) => rm(root, { recursive: true, force: true })));
|
||||
});
|
||||
|
||||
async function productionBuildInputs(globalFeatures: number) {
|
||||
const root = await mkdtemp(path.join(tmpdir(), `topology-${globalFeatures}-`));
|
||||
roots.push(root);
|
||||
const written = await writeFilesystemTopology(root, { globalFeatures });
|
||||
const run = spawnSync(process.execPath, [GENERATOR, `--root=${root}`, `--app=${APP_A.id}`], {
|
||||
encoding: "utf8",
|
||||
});
|
||||
if (run.status !== 0) throw new Error(`generator failed: ${run.stderr}`);
|
||||
const generated = await Promise.all(
|
||||
OUTPUTS.map((file) => readFile(path.join(root, file), "utf8")),
|
||||
);
|
||||
const graph = await reachableSourceGraph(root, OUTPUTS);
|
||||
return { written, generated, graph };
|
||||
}
|
||||
|
||||
describe("global-to-local independence on the production generator path", () => {
|
||||
it(
|
||||
"keeps App A's generated indexes and reachable source graph identical from S0 to S2 on disk",
|
||||
async () => {
|
||||
const s0 = await productionBuildInputs(SCALE_PROFILES.S0);
|
||||
const s1 = await productionBuildInputs(SCALE_PROFILES.S1);
|
||||
const s2 = await productionBuildInputs(SCALE_PROFILES.S2);
|
||||
|
||||
expect(s2.written.features).toBe(SCALE_PROFILES.S2);
|
||||
expect(s2.written.apps).toBeGreaterThan(s0.written.apps);
|
||||
expect(s1.generated).toEqual(s0.generated);
|
||||
expect(s2.generated).toEqual(s0.generated);
|
||||
expect(s2.graph).toEqual(s0.graph);
|
||||
const hash = (texts: readonly string[]) =>
|
||||
texts.map((text) => createHash("sha256").update(text).digest("hex"));
|
||||
expect(hash(s2.generated)).toEqual(hash(s0.generated));
|
||||
|
||||
const localFeatures = APP_A.contexts * APP_A.featuresPerContext;
|
||||
// Generated indexes + (feature-module, context-routes, page) per local feature.
|
||||
expect(s2.graph).toHaveLength(OUTPUTS.length + localFeatures * 3);
|
||||
expect(s2.graph.some((file) => file.includes("other-"))).toBe(false);
|
||||
expect(s2.generated[1]?.match(/defineBoundedContextModule\(\{/gu)).toHaveLength(APP_A.contexts);
|
||||
},
|
||||
120_000,
|
||||
);
|
||||
|
||||
it("selects another app from the same repository without App A's features", async () => {
|
||||
const root = await mkdtemp(path.join(tmpdir(), "topology-select-"));
|
||||
roots.push(root);
|
||||
await writeFilesystemTopology(root, { globalFeatures: 1_100 });
|
||||
const other = await generateAppIndexes(root, "other-0");
|
||||
expect(other.app.id).toBe("other-0");
|
||||
expect(other.entries.every((entry) => entry.contextId.startsWith("other-0-"))).toBe(true);
|
||||
await expect(generateAppIndexes(root, undefined)).rejects.toThrow(/Select a deployable app/);
|
||||
await expect(generateAppIndexes(root, "missing")).rejects.toThrow(/Unknown deployable app/);
|
||||
});
|
||||
|
||||
it("produces the same output from the filesystem source and the S3/S4 declaration source", async () => {
|
||||
const topology = createSyntheticTopology({ globalFeatures: SCALE_PROFILES.S0 * 3 });
|
||||
const root = await mkdtemp(path.join(tmpdir(), "topology-equivalence-"));
|
||||
roots.push(root);
|
||||
await writeSyntheticApps(
|
||||
root,
|
||||
["app-0", "app-1", "app-2"].map((id) => ({
|
||||
id,
|
||||
maxContexts: APP_CONTEXT_BUDGET,
|
||||
contexts: topology.app(id).contexts.map((context) => ({
|
||||
...context,
|
||||
maxFeatures: CONTEXT_FEATURE_BUDGET,
|
||||
})),
|
||||
})),
|
||||
);
|
||||
const fromDisk = await generateAppIndexes(root, "app-0");
|
||||
const fromModel = selectAppIndexes(declarationSource(topology), "app-0");
|
||||
expect(fromModel.featureIndex).toBe(fromDisk.featureIndex);
|
||||
expect(fromModel.contextIndex).toBe(fromDisk.contextIndex);
|
||||
expect(fromModel.appIndex).toBe(fromDisk.appIndex);
|
||||
});
|
||||
|
||||
it("keeps one unchanged app's production indexes identical from S0 to S4", () => {
|
||||
const s0 = selectAppInputs(createSyntheticTopology({ globalFeatures: SCALE_PROFILES.S0 }), "app-0");
|
||||
for (const globalFeatures of Object.values(SCALE_PROFILES)) {
|
||||
const current = selectAppInputs(createSyntheticTopology({ globalFeatures }), "app-0");
|
||||
expect(current.contextIds).toEqual(s0.contextIds);
|
||||
expect(current.featureIds).toEqual(s0.featureIds);
|
||||
expect(current.routeRootIds).toEqual(s0.routeRootIds);
|
||||
expect(current.generatedFeatureIndex).toBe(s0.generatedFeatureIndex);
|
||||
expect(current.generatedContextIndex).toBe(s0.generatedContextIndex);
|
||||
expect(current.generatedAppIndex).toBe(s0.generatedAppIndex);
|
||||
expect(current.generatedHashes).toEqual(s0.generatedHashes);
|
||||
expect(current.compileInputPaths).toEqual(s0.compileInputPaths);
|
||||
}
|
||||
// The compile input is App A's own modules only: one feature module and
|
||||
// one lazy route module per local feature, plus the context descriptor.
|
||||
const featureOwned = s0.compileInputPaths.filter((file) => /^src\/features\/[^/]+\//u.test(file));
|
||||
expect(featureOwned).toHaveLength(s0.featureIds.length * 2);
|
||||
expect(featureOwned.every((file) => file.startsWith("src/features/app0-"))).toBe(true);
|
||||
});
|
||||
|
||||
it("reads only the selected app's declarations, even at 1,000,000 logical features", () => {
|
||||
const inputs = selectAppInputs(
|
||||
createSyntheticTopology({ globalFeatures: SCALE_PROFILES.S4 }),
|
||||
"app-0",
|
||||
);
|
||||
expect(inputs.reads.apps).toBe(1);
|
||||
expect(inputs.reads.contexts).toBe(inputs.contextIds.length);
|
||||
expect(inputs.reads.features).toBe(inputs.featureIds.length);
|
||||
expect(inputs.featureIds.length).toBeLessThan(SCALE_PROFILES.S0 + 1);
|
||||
});
|
||||
|
||||
it("boots only direct context roots, never the global feature table", () => {
|
||||
const inputs = selectAppInputs(
|
||||
createSyntheticTopology({ globalFeatures: SCALE_PROFILES.S4 }),
|
||||
"app-0",
|
||||
);
|
||||
expect(inputs.routeRootIds.length).toBe(inputs.contextIds.length);
|
||||
expect(inputs.routeRootIds.length).toBeLessThanOrEqual(inputs.contextBudget);
|
||||
// Feature executables sit behind the lazy context loader, not in boot imports.
|
||||
expect(inputs.generatedContextIndex).toContain("async loadRoutes()");
|
||||
expect(inputs.generatedContextIndex).toMatch(/import\("\.\.\/features\//u);
|
||||
});
|
||||
|
||||
it("keeps an app deep in the S4 topology within the same local shape", () => {
|
||||
const topology = createSyntheticTopology({ globalFeatures: SCALE_PROFILES.S4 });
|
||||
const first = selectAppInputs(topology, "app-0");
|
||||
const last = selectAppInputs(topology, `app-${topology.appCount - 1}`);
|
||||
expect(last.featureIds.length).toBe(first.featureIds.length);
|
||||
expect(last.contextIds.length).toBe(first.contextIds.length);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,91 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
assertLinearOrBetter,
|
||||
GrowthRegressionError,
|
||||
growthExponents,
|
||||
} from "../../src/platform/cardinality/growth-regression.ts";
|
||||
|
||||
describe("assertLinearOrBetter", () => {
|
||||
it("accepts roughly linear growth", () => {
|
||||
expect(() =>
|
||||
assertLinearOrBetter([
|
||||
{ n: 100, cost: 1 },
|
||||
{ n: 1_000, cost: 10 },
|
||||
{ n: 10_000, cost: 100 },
|
||||
]),
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it("rejects quadratic-shaped growth", () => {
|
||||
expect(() =>
|
||||
assertLinearOrBetter([
|
||||
{ n: 100, cost: 1 },
|
||||
{ n: 1_000, cost: 100 },
|
||||
{ n: 10_000, cost: 10_000 },
|
||||
]),
|
||||
).toThrow(/growth/i);
|
||||
});
|
||||
|
||||
it("rejects a quadratic implementation even while absolute times are tiny", () => {
|
||||
expect(() =>
|
||||
assertLinearOrBetter([
|
||||
{ n: 100, cost: 0.0001 },
|
||||
{ n: 1_000, cost: 0.01 },
|
||||
]),
|
||||
).toThrow(GrowthRegressionError);
|
||||
});
|
||||
|
||||
it("accepts sub-linear and n log n growth", () => {
|
||||
expect(() =>
|
||||
assertLinearOrBetter([
|
||||
{ n: 100, cost: 100 * Math.log(100) },
|
||||
{ n: 1_000, cost: 1_000 * Math.log(1_000) },
|
||||
{ n: 10_000, cost: 10_000 * Math.log(10_000) },
|
||||
]),
|
||||
).not.toThrow();
|
||||
expect(() =>
|
||||
assertLinearOrBetter([
|
||||
{ n: 100, cost: 5 },
|
||||
{ n: 10_000, cost: 5 },
|
||||
]),
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it("does not fail a linear series because of noise below the floor", () => {
|
||||
expect(() =>
|
||||
assertLinearOrBetter(
|
||||
[
|
||||
{ n: 100, cost: 0.001 },
|
||||
{ n: 1_000, cost: 0.2 },
|
||||
{ n: 10_000, cost: 2 },
|
||||
],
|
||||
{ noiseFloor: 0.2 },
|
||||
),
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it("rejects malformed series", () => {
|
||||
expect(() => assertLinearOrBetter([{ n: 1, cost: 1 }])).toThrow(TypeError);
|
||||
expect(() =>
|
||||
assertLinearOrBetter([
|
||||
{ n: 1, cost: 1 },
|
||||
{ n: 1, cost: 2 },
|
||||
]),
|
||||
).toThrow(TypeError);
|
||||
expect(() =>
|
||||
assertLinearOrBetter([
|
||||
{ n: 0, cost: 1 },
|
||||
{ n: 10, cost: 2 },
|
||||
]),
|
||||
).toThrow(TypeError);
|
||||
});
|
||||
|
||||
it("reports per-segment exponents", () => {
|
||||
const [exponent] = growthExponents([
|
||||
{ n: 10, cost: 1 },
|
||||
{ n: 100, cost: 100 },
|
||||
]);
|
||||
expect(exponent).toBeCloseTo(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
/**
|
||||
* Scenario suites record their measured counters as machine-readable JSON when
|
||||
* a scale profile run asks for them (`SCALE_ARTIFACT_DIR`, set by
|
||||
* scripts/scale/run-scale-profile.ts). Plain test runs write nothing.
|
||||
*/
|
||||
export async function recordScaleMetrics(
|
||||
name: string,
|
||||
metrics: Readonly<Record<string, unknown>>,
|
||||
): Promise<void> {
|
||||
const directory = process.env["SCALE_ARTIFACT_DIR"];
|
||||
if (!directory) return;
|
||||
await mkdir(directory, { recursive: true });
|
||||
await writeFile(
|
||||
path.join(directory, `${name}.json`),
|
||||
`${JSON.stringify({ schemaVersion: 1, name, recordedAt: new Date().toISOString(), metrics }, null, 2)}\n`,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
approveBaseline,
|
||||
compareToBaseline,
|
||||
metricsDigest,
|
||||
type ScaleBaseline,
|
||||
type ScaleEnvironment,
|
||||
type ScaleMetrics,
|
||||
} from "../../scripts/scale/scale-baseline.ts";
|
||||
import { COMPOSITION_WORKLOADS } from "../../scripts/scale/composition-workloads.ts";
|
||||
import { BASELINE_PATH, LOCAL_TSC_SIZES } from "../../scripts/scale/check-scale-baseline.ts";
|
||||
|
||||
const ENVIRONMENT: ScaleEnvironment = {
|
||||
nodeMajor: 24,
|
||||
platform: "linux",
|
||||
arch: "x64",
|
||||
cpuModel: "Test CPU",
|
||||
cpuCount: 8,
|
||||
typescript: "7.0.2",
|
||||
};
|
||||
|
||||
const METRICS: ScaleMetrics = {
|
||||
timing: [
|
||||
{ workload: "registry-composition", profile: "S1", cardinality: 1_000, medianMs: 0.5, minMs: 0.4 },
|
||||
{ workload: "registry-composition", profile: "S2", cardinality: 10_000, medianMs: 5, minMs: 4 },
|
||||
],
|
||||
compiler: [{ fixture: "app", files: 800, types: 270_000, symbols: 770_000, instantiations: 690_000 }],
|
||||
};
|
||||
|
||||
function baseline(metrics: ScaleMetrics = METRICS, environment = ENVIRONMENT): ScaleBaseline {
|
||||
return approveBaseline(environment, metrics, {
|
||||
owner: "frontend-platform",
|
||||
reason: "test",
|
||||
approvedAt: "2026-09-23T00:00:00.000Z",
|
||||
});
|
||||
}
|
||||
|
||||
function withTiming(minMs: number): ScaleMetrics {
|
||||
return {
|
||||
...METRICS,
|
||||
timing: METRICS.timing.map((entry) =>
|
||||
entry.profile === "S2" ? { ...entry, minMs, medianMs: minMs } : entry,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
function withTypes(types: number): ScaleMetrics {
|
||||
return { ...METRICS, compiler: [{ ...METRICS.compiler[0]!, types }] };
|
||||
}
|
||||
|
||||
describe("relative regression gate against the stored baseline", () => {
|
||||
it("passes an unchanged run on all three axes", () => {
|
||||
const result = compareToBaseline(baseline(), ENVIRONMENT, METRICS);
|
||||
expect(result.status).toBe("passed");
|
||||
expect(result.passed).toBe(true);
|
||||
expect(result.absolute.passed).toBe(true);
|
||||
expect(result.growth.passed).toBe(true);
|
||||
expect(result.compiler.length).toBe(4);
|
||||
expect(result.timing.length).toBe(2);
|
||||
});
|
||||
|
||||
it("fails a compiler-cost regression above 10% even when the growth shape is fine", () => {
|
||||
expect(compareToBaseline(baseline(), ENVIRONMENT, withTypes(290_000)).passed).toBe(true);
|
||||
const regressed = compareToBaseline(baseline(), ENVIRONMENT, withTypes(300_000));
|
||||
expect(regressed.passed).toBe(false);
|
||||
expect(regressed.failures.join("\n")).toMatch(/tsc:app:types 300000 exceeds 297000/);
|
||||
});
|
||||
|
||||
it("fails a timing regression beyond 2.5x on a compatible machine", () => {
|
||||
expect(compareToBaseline(baseline(), ENVIRONMENT, withTiming(9.9)).passed).toBe(true);
|
||||
const regressed = compareToBaseline(baseline(), ENVIRONMENT, withTiming(12));
|
||||
expect(regressed.passed).toBe(false);
|
||||
expect(regressed.failures.join("\n")).toMatch(/time:registry-composition\/S2/);
|
||||
});
|
||||
|
||||
it("never fails timing differences inside the absolute noise floor", () => {
|
||||
const tiny: ScaleMetrics = {
|
||||
...METRICS,
|
||||
timing: [{ workload: "w", profile: "S0", cardinality: 100, medianMs: 0.02, minMs: 0.02 }],
|
||||
};
|
||||
const noisy: ScaleMetrics = { ...tiny, timing: [{ ...tiny.timing[0]!, minMs: 0.9 }] };
|
||||
expect(compareToBaseline(baseline(tiny), ENVIRONMENT, noisy).passed).toBe(true);
|
||||
});
|
||||
|
||||
it("reports timings from a different machine as incomplete, never as passed, but still gates compiler counts", () => {
|
||||
const otherMachine = { ...ENVIRONMENT, cpuModel: "Other CPU" };
|
||||
const slow = compareToBaseline(baseline(), otherMachine, withTiming(12));
|
||||
expect(slow.status).toBe("incomplete");
|
||||
expect(slow.passed).toBe(false);
|
||||
expect(slow.compatibility.timing.ok).toBe(false);
|
||||
expect(slow.timing).toEqual([]);
|
||||
|
||||
const heavier = compareToBaseline(baseline(), otherMachine, withTypes(400_000));
|
||||
expect(heavier.status).toBe("failed");
|
||||
});
|
||||
|
||||
it("enforces machine-agnostic absolute ceilings on every run", () => {
|
||||
const tooSlow = compareToBaseline(baseline(), { ...ENVIRONMENT, cpuModel: "Other CPU" }, withTiming(151));
|
||||
expect(tooSlow.status).toBe("failed");
|
||||
expect(tooSlow.failures.join("\n")).toMatch(/ceiling:time:registry-composition\/S2 151 exceeds 150/);
|
||||
const tooHeavy = compareToBaseline(baseline(), ENVIRONMENT, withTypes(600_001));
|
||||
expect(tooHeavy.absolute.passed).toBe(false);
|
||||
});
|
||||
|
||||
it("fails a super-linear growth shape even when relative and absolute limits hold", () => {
|
||||
const quadratic: ScaleMetrics = {
|
||||
...METRICS,
|
||||
timing: [
|
||||
{ workload: "registry-composition", profile: "S1", cardinality: 1_000, medianMs: 0.6, minMs: 0.6 },
|
||||
{ workload: "registry-composition", profile: "S2", cardinality: 10_000, medianMs: 60, minMs: 60 },
|
||||
],
|
||||
};
|
||||
const result = compareToBaseline(baseline(quadratic), ENVIRONMENT, quadratic);
|
||||
expect(result.growth.passed).toBe(false);
|
||||
expect(result.status).toBe("failed");
|
||||
expect(result.failures.join("\n")).toMatch(/growth:time:registry-composition/);
|
||||
});
|
||||
|
||||
it("requires re-approval when the compiler changes", () => {
|
||||
const result = compareToBaseline(baseline(), { ...ENVIRONMENT, typescript: "7.1.0" }, METRICS);
|
||||
expect(result.passed).toBe(false);
|
||||
expect(result.compatibility.compiler.ok).toBe(false);
|
||||
expect(result.failures.join("\n")).toMatch(/re-approve with update:scale-baseline/);
|
||||
});
|
||||
|
||||
it("rejects a hand-edited baseline whose metrics no longer match the approved digest", () => {
|
||||
const tampered: ScaleBaseline = {
|
||||
...baseline(),
|
||||
metrics: withTypes(900_000),
|
||||
};
|
||||
const result = compareToBaseline(tampered, ENVIRONMENT, METRICS);
|
||||
expect(result.passed).toBe(false);
|
||||
expect(result.compatibility.structural.reasons.join("\n")).toMatch(/digest/);
|
||||
});
|
||||
|
||||
it("rejects a run measuring something the baseline does not cover", () => {
|
||||
const extra: ScaleMetrics = {
|
||||
...METRICS,
|
||||
compiler: [...METRICS.compiler, { fixture: "local-9999", files: 1, types: 1, symbols: 1, instantiations: 1 }],
|
||||
};
|
||||
const result = compareToBaseline(baseline(), ENVIRONMENT, extra);
|
||||
expect(result.passed).toBe(false);
|
||||
expect(result.failures.join("\n")).toMatch(/no baseline for compiler fixture local-9999/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("stored benchmark baseline", () => {
|
||||
it("is approved, digest-consistent and covers every measured workload and fixture", async () => {
|
||||
const stored = JSON.parse(
|
||||
await readFile(path.resolve(BASELINE_PATH), "utf8"),
|
||||
) as ScaleBaseline;
|
||||
|
||||
expect(stored.schemaVersion).toBe(1);
|
||||
expect(stored.approval.owner).toBeTruthy();
|
||||
expect(stored.approval.reason).toBeTruthy();
|
||||
expect(metricsDigest(stored.metrics)).toBe(stored.approval.metricsDigest);
|
||||
|
||||
const timing = new Set(stored.metrics.timing.map((entry) => `${entry.workload}/${entry.profile}`));
|
||||
for (const workload of COMPOSITION_WORKLOADS) {
|
||||
for (const profile of ["S0", "S1", "S2"]) {
|
||||
expect(timing.has(`${workload.name}/${profile}`), `${workload.name}/${profile}`).toBe(true);
|
||||
}
|
||||
}
|
||||
const fixtures = new Set(stored.metrics.compiler.map((entry) => entry.fixture));
|
||||
expect(fixtures).toEqual(
|
||||
new Set(["app", ...LOCAL_TSC_SIZES.map((size) => `local-${size}`)]),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Spec §35. Synthetic scale profiles. S3/S4 are logical cardinalities: they
|
||||
* are modelled as topology, never compiled as one SPA.
|
||||
*/
|
||||
export const SCALE_PROFILES = Object.freeze({
|
||||
S0: 100,
|
||||
S1: 1_000,
|
||||
S2: 10_000,
|
||||
S3: 100_000,
|
||||
S4: 1_000_000,
|
||||
});
|
||||
|
||||
export type ScaleProfileId = keyof typeof SCALE_PROFILES;
|
||||
|
||||
/** Direct-child ceilings, matching the deployable app/context descriptors. */
|
||||
export const APP_CONTEXT_BUDGET = 20;
|
||||
export const CONTEXT_FEATURE_BUDGET = 200;
|
||||
export const APP_LOCAL_FEATURE_BUDGET = APP_CONTEXT_BUDGET * CONTEXT_FEATURE_BUDGET;
|
||||
|
||||
/**
|
||||
* The fixed shape one synthetic app is given. Global growth adds apps; it
|
||||
* never widens an existing app.
|
||||
*/
|
||||
export const SYNTHETIC_APP_SHAPE = Object.freeze({
|
||||
contextsPerApp: 10,
|
||||
featuresPerContext: 10,
|
||||
});
|
||||
@@ -0,0 +1,154 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createQueryClient } from "../../../src/adapters/query-cache/tanstack-query-cache.ts";
|
||||
import { createTelemetryPipeline } from "../../../src/platform/telemetry/telemetry-pipeline.ts";
|
||||
import type { InstalledHttpContract } from "../../../src/contracts/external-contract-runtime.ts";
|
||||
import { TEST_CREATE_HTTP_CONTRACT } from "../../helpers/external-contract-fixture.ts";
|
||||
import {
|
||||
jsonRows,
|
||||
networkFailure,
|
||||
testExecutionContext,
|
||||
testExecutor,
|
||||
type Transport,
|
||||
} from "../../helpers/traffic-test-kit.ts";
|
||||
import { classifyCounterSeries } from "../../../scripts/scale/memory-slope.ts";
|
||||
import { TEST_QUERY } from "../traffic/synthetic-traffic.ts";
|
||||
import { recordScaleMetrics } from "../scale-artifact.ts";
|
||||
|
||||
const TEST_COMMAND = TEST_CREATE_HTTP_CONTRACT as InstalledHttpContract<
|
||||
unknown,
|
||||
unknown,
|
||||
unknown
|
||||
>;
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
/**
|
||||
* A long-lived scope signal that counts its listeners, standing in for the
|
||||
* session-wide server-state scope every execution subscribes to.
|
||||
*/
|
||||
function countingScope() {
|
||||
const controller = new AbortController();
|
||||
let listeners = 0;
|
||||
const signal = controller.signal;
|
||||
const add = signal.addEventListener.bind(signal);
|
||||
const remove = signal.removeEventListener.bind(signal);
|
||||
Object.defineProperty(signal, "addEventListener", {
|
||||
value: (...args: Parameters<AbortSignal["addEventListener"]>) => {
|
||||
listeners += 1;
|
||||
add(...args);
|
||||
},
|
||||
});
|
||||
Object.defineProperty(signal, "removeEventListener", {
|
||||
value: (...args: Parameters<AbortSignal["removeEventListener"]>) => {
|
||||
listeners -= 1;
|
||||
remove(...args);
|
||||
},
|
||||
});
|
||||
let generation = 1;
|
||||
return {
|
||||
scope: () =>
|
||||
Object.freeze({
|
||||
generation,
|
||||
fingerprint: `scope-${generation}`,
|
||||
identities: Object.freeze({}) as never,
|
||||
signal,
|
||||
isCurrent: () => true,
|
||||
}),
|
||||
listeners: () => listeners,
|
||||
nextGeneration() {
|
||||
generation += 1;
|
||||
},
|
||||
generation: () => generation,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Spec §33 / §44. Thousands of login/navigation/query/mutation/logout cycles.
|
||||
* Every long-lived resource must return to (or stabilize at) its post-warm-up
|
||||
* bound rather than grow with the number of cycles.
|
||||
*/
|
||||
describe("long-session logical leak checks", () => {
|
||||
it("returns timers, listeners, leases, buffers and cache entries to a stable bound", async () => {
|
||||
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
||||
const session = countingScope();
|
||||
let cycle = 0;
|
||||
const transport: Transport = () => (cycle % 7 === 0 ? networkFailure() : Promise.resolve(jsonRows(2)));
|
||||
const executor = testExecutor(transport, { maxRetryAttempts: 1 });
|
||||
const telemetry = createTelemetryPipeline({
|
||||
transport: { send: () => {} },
|
||||
schedule: () => {},
|
||||
setTimer: () => 0,
|
||||
clearTimer: () => {},
|
||||
});
|
||||
const queryClient = createQueryClient();
|
||||
|
||||
const series = { timers: [] as number[], listeners: [] as number[], buffered: [] as number[], cache: [] as number[] };
|
||||
const CYCLES = 3_000;
|
||||
for (cycle = 0; cycle < CYCLES; cycle += 1) {
|
||||
// login: a new session scope generation every 250 cycles
|
||||
if (cycle % 250 === 0) session.nextGeneration();
|
||||
const execution = testExecutionContext({
|
||||
maxPhysicalRequests: 3,
|
||||
maxRetries: 1,
|
||||
maxConcurrentRequests: 2,
|
||||
});
|
||||
const context = { routeId: "TEST_ROUTE", scope: session.scope(), execution };
|
||||
// navigation + query through the query cache (bounded key cardinality)
|
||||
const queryKey = ["session", session.generation(), cycle % 25];
|
||||
const query = queryClient.fetchQuery({
|
||||
queryKey,
|
||||
queryFn: () => executor.execute(TEST_QUERY, { limit: 1 }, context),
|
||||
});
|
||||
// mutation
|
||||
const mutation = executor.execute(TEST_COMMAND, { name: `c${cycle}` }, {
|
||||
...context,
|
||||
intent: Object.freeze({
|
||||
intentId: `intent-${cycle}`,
|
||||
operationId: "TEST_CREATE_ENTITY",
|
||||
canonicalInputIdentity: `input-${cycle}`,
|
||||
idempotencyKey: `key-${cycle.toString().padStart(6, "0")}`,
|
||||
createdAtMonotonicMs: cycle,
|
||||
}),
|
||||
});
|
||||
const [queryOutcome, mutationOutcome] = await Promise.all([query, mutation]);
|
||||
if (queryOutcome.kind !== "SUCCESS" || mutationOutcome.kind !== "SUCCESS") {
|
||||
await telemetry.emit({
|
||||
kind: "session.operation.failed",
|
||||
severity: "warning",
|
||||
interactionId: execution.interaction.id,
|
||||
payload: { query: queryOutcome.kind, mutation: mutationOutcome.kind },
|
||||
});
|
||||
}
|
||||
// logout: the interaction ends
|
||||
execution.cancellation.cancel();
|
||||
expect(execution.traffic.concurrency.active).toBe(0);
|
||||
|
||||
if (cycle % 100 === 99) {
|
||||
await telemetry.flush();
|
||||
series.timers.push(vi.getTimerCount());
|
||||
series.listeners.push(session.listeners());
|
||||
series.buffered.push(telemetry.stats().buffered);
|
||||
series.cache.push(queryClient.getQueryCache().getAll().length);
|
||||
}
|
||||
if (cycle % 250 === 249) {
|
||||
// Scope generation changed: the previous generation's entries are garbage.
|
||||
queryClient.removeQueries({ queryKey: ["session", session.generation()] });
|
||||
}
|
||||
}
|
||||
|
||||
await recordScaleMetrics("long-session", { cycles: CYCLES, ...series });
|
||||
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
expect(session.listeners()).toBe(0);
|
||||
expect(classifyCounterSeries(series.timers, { warmupSamples: 3 })).toBe("stable");
|
||||
expect(classifyCounterSeries(series.listeners, { warmupSamples: 3 })).toBe("stable");
|
||||
expect(classifyCounterSeries(series.buffered, { warmupSamples: 3 })).toBe("stable");
|
||||
expect(Math.max(...series.cache)).toBeLessThanOrEqual(25);
|
||||
expect(Math.max(...series.buffered)).toBeLessThanOrEqual(200);
|
||||
queryClient.clear();
|
||||
telemetry.dispose();
|
||||
}, 120_000);
|
||||
});
|
||||
@@ -0,0 +1,128 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import type { InstalledHttpContract } from "../../../src/contracts/external-contract-runtime.ts";
|
||||
import { TEST_CREATE_HTTP_CONTRACT } from "../../helpers/external-contract-fixture.ts";
|
||||
import {
|
||||
jsonRows,
|
||||
scopeSnapshot,
|
||||
testExecutionContext,
|
||||
testExecutor,
|
||||
type Transport,
|
||||
} from "../../helpers/traffic-test-kit.ts";
|
||||
import { TEST_QUERY } from "../traffic/synthetic-traffic.ts";
|
||||
|
||||
const TEST_COMMAND = TEST_CREATE_HTTP_CONTRACT as InstalledHttpContract<
|
||||
unknown,
|
||||
unknown,
|
||||
unknown
|
||||
>;
|
||||
|
||||
/**
|
||||
* Spec §42 (network flap). The network alternates online/offline. An
|
||||
* offline attempt that reached `fetch` is a dispatched attempt whose effect is
|
||||
* unknown; the executor must never replay a keyed command automatically.
|
||||
*/
|
||||
function flappingNetwork() {
|
||||
let online = true;
|
||||
const commandDispatches = new Map<string, number>();
|
||||
const inFlight = new Set<AbortSignal>();
|
||||
const transport: Transport = (_input, init) => {
|
||||
const key = new Headers(init?.headers).get("idempotency-key");
|
||||
if (key) commandDispatches.set(key, (commandDispatches.get(key) ?? 0) + 1);
|
||||
if (!online) return Promise.reject(new TypeError("offline"));
|
||||
if (key) {
|
||||
return Promise.resolve(
|
||||
Response.json({ id: key, name: "created" }, { status: 201 }),
|
||||
);
|
||||
}
|
||||
return Promise.resolve(jsonRows(1));
|
||||
};
|
||||
return {
|
||||
transport,
|
||||
commandDispatches,
|
||||
inFlight,
|
||||
setOnline(value: boolean) {
|
||||
online = value;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function intent(index: number) {
|
||||
return Object.freeze({
|
||||
intentId: `intent-${index}`,
|
||||
operationId: "TEST_CREATE_ENTITY",
|
||||
canonicalInputIdentity: `input-${index}`,
|
||||
idempotencyKey: `key-${index.toString().padStart(6, "0")}`,
|
||||
createdAtMonotonicMs: index,
|
||||
});
|
||||
}
|
||||
|
||||
describe("network flap", () => {
|
||||
it("never replays an unsafe mutation and keeps queues bounded across flaps", async () => {
|
||||
const network = flappingNetwork();
|
||||
const executor = testExecutor(network.transport, { maxRetryAttempts: 2 });
|
||||
let peakQueued = 0;
|
||||
|
||||
for (let cycle = 0; cycle < 400; cycle += 1) {
|
||||
network.setOnline(cycle % 2 === 0);
|
||||
const execution = testExecutionContext({
|
||||
maxPhysicalRequests: 4,
|
||||
maxRetries: 2,
|
||||
maxConcurrentRequests: 1,
|
||||
maxQueuedRequests: 2,
|
||||
});
|
||||
const context = { routeId: "TEST_ROUTE", scope: scopeSnapshot(), execution };
|
||||
const work = Promise.all([
|
||||
executor.execute(TEST_COMMAND, { name: `n${cycle}` }, { ...context, intent: intent(cycle) }),
|
||||
executor.execute(TEST_QUERY, { limit: 1 }, context),
|
||||
executor.execute(TEST_QUERY, { limit: 1 }, context),
|
||||
]);
|
||||
peakQueued = Math.max(peakQueued, execution.traffic.concurrency.queued);
|
||||
await work;
|
||||
expect(execution.traffic.concurrency.active).toBe(0);
|
||||
expect(execution.traffic.concurrency.queued).toBe(0);
|
||||
}
|
||||
|
||||
expect(Math.max(...network.commandDispatches.values())).toBe(1);
|
||||
expect(network.commandDispatches.size).toBe(400);
|
||||
expect(peakQueued).toBeLessThanOrEqual(2);
|
||||
});
|
||||
|
||||
it("completes cancellation of stale interactions when the network drops", async () => {
|
||||
const pendingSignals: AbortSignal[] = [];
|
||||
const transport: Transport = (_input, init) =>
|
||||
new Promise<Response>((_resolve, reject) => {
|
||||
if (init?.signal) pendingSignals.push(init.signal);
|
||||
init?.signal?.addEventListener(
|
||||
"abort",
|
||||
() => reject(new DOMException("aborted", "AbortError")),
|
||||
{ once: true },
|
||||
);
|
||||
});
|
||||
const executor = testExecutor(transport);
|
||||
const interactions = Array.from({ length: 50 }, () =>
|
||||
testExecutionContext({ maxConcurrentRequests: 1, maxQueuedRequests: 2 }),
|
||||
);
|
||||
const running = interactions.flatMap((execution) =>
|
||||
[0, 1, 2].map(() =>
|
||||
executor.execute(TEST_QUERY, { limit: 1 }, {
|
||||
routeId: "TEST_ROUTE",
|
||||
scope: scopeSnapshot(),
|
||||
execution,
|
||||
}),
|
||||
),
|
||||
);
|
||||
await vi.waitFor(() => expect(pendingSignals).toHaveLength(50));
|
||||
|
||||
// "offline" event: the session cancels every interaction started online.
|
||||
for (const execution of interactions) execution.cancellation.cancel();
|
||||
const outcomes = await Promise.all(running);
|
||||
|
||||
expect(outcomes.every((outcome) => outcome.kind === "CANCELLED")).toBe(true);
|
||||
expect(pendingSignals.every((signal) => signal.aborted)).toBe(true);
|
||||
for (const execution of interactions) {
|
||||
expect(execution.traffic.concurrency.active).toBe(0);
|
||||
expect(execution.traffic.concurrency.queued).toBe(0);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
APP_CONTEXT_BUDGET,
|
||||
APP_LOCAL_FEATURE_BUDGET,
|
||||
CONTEXT_FEATURE_BUDGET,
|
||||
SCALE_PROFILES,
|
||||
} from "./scale-profile.ts";
|
||||
import { createSyntheticTopology } from "./synthetic-topology.ts";
|
||||
|
||||
describe("scale profiles and synthetic topology", () => {
|
||||
it("defines the exact S0-S4 profiles", () => {
|
||||
expect(SCALE_PROFILES).toEqual({
|
||||
S0: 100,
|
||||
S1: 1_000,
|
||||
S2: 10_000,
|
||||
S3: 100_000,
|
||||
S4: 1_000_000,
|
||||
});
|
||||
});
|
||||
|
||||
it("grows global topology without growing a selected local app beyond its budgets", () => {
|
||||
const small = createSyntheticTopology({ globalFeatures: 100 });
|
||||
const huge = createSyntheticTopology({ globalFeatures: 1_000_000 });
|
||||
|
||||
expect(huge.apps[0]?.contexts.length).toBeLessThanOrEqual(APP_CONTEXT_BUDGET);
|
||||
expect(huge.apps[0]?.localFeatureCount).toBeLessThanOrEqual(APP_LOCAL_FEATURE_BUDGET);
|
||||
expect(huge.apps[0]?.localFeatureCount).toBe(small.apps[0]?.localFeatureCount);
|
||||
});
|
||||
|
||||
it.each(Object.entries(SCALE_PROFILES))(
|
||||
"%s partitions %i features into budget-respecting apps and contexts",
|
||||
(_profile, globalFeatures) => {
|
||||
const topology = createSyntheticTopology({ globalFeatures });
|
||||
expect(topology.appCount * APP_CONTEXT_BUDGET * CONTEXT_FEATURE_BUDGET).toBeGreaterThanOrEqual(
|
||||
globalFeatures,
|
||||
);
|
||||
for (const index of [0, topology.appCount - 1]) {
|
||||
const app = topology.apps[index];
|
||||
expect(app?.contexts.length).toBeLessThanOrEqual(APP_CONTEXT_BUDGET);
|
||||
for (const context of app?.contexts ?? []) {
|
||||
expect(context.featureIds.length).toBeLessThanOrEqual(CONTEXT_FEATURE_BUDGET);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it("models S4 compactly instead of materializing a million features", () => {
|
||||
const before = process.memoryUsage().heapUsed;
|
||||
const topology = createSyntheticTopology({ globalFeatures: 1_000_000 });
|
||||
const after = process.memoryUsage().heapUsed;
|
||||
|
||||
expect(topology.appCount).toBe(10_000);
|
||||
expect(after - before).toBeLessThan(16 * 1024 * 1024);
|
||||
expect(topology.apps[10_000]).toBeUndefined();
|
||||
expect(() => topology.app("app-10000")).toThrow(RangeError);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,207 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { planTopologyCapacity } from "../../src/platform/cardinality/topology-capacity.ts";
|
||||
import {
|
||||
selectAppIndexes,
|
||||
type ContextDeclaration,
|
||||
type FeatureDeclaration,
|
||||
type TopologyDeclarationSource,
|
||||
} from "../../scripts/generate-product-feature-index.ts";
|
||||
import {
|
||||
APP_CONTEXT_BUDGET,
|
||||
CONTEXT_FEATURE_BUDGET,
|
||||
SYNTHETIC_APP_SHAPE,
|
||||
} from "./scale-profile.ts";
|
||||
|
||||
/**
|
||||
* Spec §37. A compact model of a global product topology.
|
||||
*
|
||||
* Only counts are stored. An app's contexts and feature ids are derived on
|
||||
* demand from its index, so a 1,000,000-feature topology costs a handful of
|
||||
* numbers plus whatever single app a test actually inspects.
|
||||
*/
|
||||
export type SyntheticContext = Readonly<{
|
||||
id: string;
|
||||
routePath: string;
|
||||
featureIds: readonly string[];
|
||||
}>;
|
||||
|
||||
export type SyntheticApp = Readonly<{
|
||||
id: string;
|
||||
localFeatureCount: number;
|
||||
contexts: readonly SyntheticContext[];
|
||||
}>;
|
||||
|
||||
export type SyntheticTopology = Readonly<{
|
||||
globalFeatures: number;
|
||||
appCount: number;
|
||||
contextCount: number;
|
||||
apps: Readonly<{ length: number; [index: number]: SyntheticApp }>;
|
||||
app(id: string): SyntheticApp;
|
||||
}>;
|
||||
|
||||
function contextFor(appIndex: number, contextIndex: number, features: number): SyntheticContext {
|
||||
const id = `app${appIndex}-ctx${contextIndex}`;
|
||||
return Object.freeze({
|
||||
id,
|
||||
routePath: `ctx${contextIndex}/*`,
|
||||
featureIds: Object.freeze(
|
||||
Array.from({ length: features }, (_, featureIndex) => `${id}-f${featureIndex}`),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function createSyntheticTopology(
|
||||
options: Readonly<{ globalFeatures: number }>,
|
||||
): SyntheticTopology {
|
||||
const { contextsPerApp, featuresPerContext } = SYNTHETIC_APP_SHAPE;
|
||||
const plan = planTopologyCapacity({
|
||||
featureCount: options.globalFeatures,
|
||||
maxFeaturesPerContext: featuresPerContext,
|
||||
maxContextsPerApp: contextsPerApp,
|
||||
});
|
||||
const featuresPerApp = contextsPerApp * featuresPerContext;
|
||||
const cache = new Map<number, SyntheticApp>();
|
||||
|
||||
function build(appIndex: number): SyntheticApp {
|
||||
const cached = cache.get(appIndex);
|
||||
if (cached) return cached;
|
||||
const firstFeature = appIndex * featuresPerApp;
|
||||
const localFeatureCount = Math.max(
|
||||
0,
|
||||
Math.min(featuresPerApp, options.globalFeatures - firstFeature),
|
||||
);
|
||||
const contextCount = Math.ceil(localFeatureCount / featuresPerContext);
|
||||
const app = Object.freeze({
|
||||
id: `app-${appIndex}`,
|
||||
localFeatureCount,
|
||||
contexts: Object.freeze(
|
||||
Array.from({ length: contextCount }, (_, contextIndex) =>
|
||||
contextFor(
|
||||
appIndex,
|
||||
contextIndex,
|
||||
Math.min(featuresPerContext, localFeatureCount - contextIndex * featuresPerContext),
|
||||
),
|
||||
),
|
||||
),
|
||||
});
|
||||
// Inspecting an app is local work; the cache never exceeds what the test
|
||||
// itself asked for.
|
||||
cache.set(appIndex, app);
|
||||
return app;
|
||||
}
|
||||
|
||||
const apps = new Proxy(Object.freeze({ length: plan.appCount }), {
|
||||
get(target, property) {
|
||||
if (typeof property === "string" && /^\d+$/u.test(property)) {
|
||||
const index = Number(property);
|
||||
return index < plan.appCount ? build(index) : undefined;
|
||||
}
|
||||
return Reflect.get(target, property);
|
||||
},
|
||||
}) as SyntheticTopology["apps"];
|
||||
|
||||
return Object.freeze({
|
||||
globalFeatures: options.globalFeatures,
|
||||
appCount: plan.appCount,
|
||||
contextCount: plan.contextCount,
|
||||
apps,
|
||||
app(id: string) {
|
||||
const match = /^app-(\d+)$/u.exec(id);
|
||||
const index = match ? Number(match[1]) : Number.NaN;
|
||||
if (!Number.isSafeInteger(index) || index >= plan.appCount) {
|
||||
throw new RangeError(`unknown synthetic app ${id}`);
|
||||
}
|
||||
return build(index);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export type DeclarationReads = { apps: number; contexts: number; features: number };
|
||||
|
||||
/**
|
||||
* The synthetic topology as a production `TopologyDeclarationSource`: the same
|
||||
* ownership-indexed interface the filesystem source implements. It counts how
|
||||
* many declarations the production selection actually reads, which is the
|
||||
* locality evidence at 1,000,000 logical features.
|
||||
*/
|
||||
export function declarationSource(
|
||||
topology: SyntheticTopology,
|
||||
): TopologyDeclarationSource & Readonly<{ reads: DeclarationReads }> {
|
||||
const reads: DeclarationReads = { apps: 0, contexts: 0, features: 0 };
|
||||
const appIndex = (appId: string) => {
|
||||
const match = /^app-(\d+)$/u.exec(appId);
|
||||
const index = match ? Number(match[1]) : Number.NaN;
|
||||
return Number.isSafeInteger(index) && index < topology.appCount ? index : -1;
|
||||
};
|
||||
const contextOwner = (contextId: string) => {
|
||||
const match = /^app(\d+)-ctx(\d+)$/u.exec(contextId);
|
||||
return match ? { app: `app-${match[1]}`, context: Number(match[2]) } : null;
|
||||
};
|
||||
return Object.freeze({
|
||||
reads,
|
||||
appIds() {
|
||||
// Listing app ids is O(apps), never O(features); the generator only
|
||||
// needs it when no app is selected explicitly.
|
||||
return Array.from({ length: topology.appCount }, (_, index) => `app-${index}`);
|
||||
},
|
||||
app(appId: string) {
|
||||
if (appIndex(appId) < 0) return undefined;
|
||||
reads.apps += 1;
|
||||
return { id: appId, maxContexts: APP_CONTEXT_BUDGET };
|
||||
},
|
||||
contextsOf(appId: string): readonly ContextDeclaration[] {
|
||||
if (appIndex(appId) < 0) return [];
|
||||
const contexts = topology.app(appId).contexts;
|
||||
reads.contexts += contexts.length;
|
||||
return contexts.map((context) => ({
|
||||
id: context.id,
|
||||
appId,
|
||||
routePath: context.routePath,
|
||||
maxFeatures: CONTEXT_FEATURE_BUDGET,
|
||||
}));
|
||||
},
|
||||
featuresOf(contextId: string): readonly FeatureDeclaration[] {
|
||||
const owner = contextOwner(contextId);
|
||||
if (!owner || appIndex(owner.app) < 0) return [];
|
||||
const context = topology.app(owner.app).contexts[owner.context];
|
||||
if (!context) return [];
|
||||
reads.features += context.featureIds.length;
|
||||
return context.featureIds.map((id) => ({ id, contextId, directory: id }));
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Everything one deployable app compiles and boots with, produced by the
|
||||
* production generator's own app selection (`selectAppIndexes`).
|
||||
*/
|
||||
export function selectAppInputs(topology: SyntheticTopology, appId: string) {
|
||||
const source = declarationSource(topology);
|
||||
const selected = selectAppIndexes(source, appId);
|
||||
const contextIds = [...new Set(selected.entries.map((entry) => entry.contextId))];
|
||||
const generated = [selected.featureIndex, selected.contextIndex, selected.appIndex];
|
||||
// Every module the generated entries hand to the compiler/bundler (static
|
||||
// and lazy imports), resolved from `src/.generated/`.
|
||||
const compileInputPaths = [
|
||||
...new Set(
|
||||
generated.flatMap((text) =>
|
||||
[...text.matchAll(/(?:from\s+|import\()\s*"(\.\.\/[^"]+)"/gu)].map((match) =>
|
||||
`src/${(match[1] as string).slice("../".length)}`,
|
||||
),
|
||||
),
|
||||
),
|
||||
].sort();
|
||||
return Object.freeze({
|
||||
contextIds,
|
||||
featureIds: selected.entries.map((entry) => entry.id),
|
||||
routeRootIds: [...new Set(selected.entries.map((entry) => entry.contextRoutePath))],
|
||||
generatedFeatureIndex: selected.featureIndex,
|
||||
generatedContextIndex: selected.contextIndex,
|
||||
generatedAppIndex: selected.appIndex,
|
||||
generatedHashes: generated.map((text) => createHash("sha256").update(text).digest("hex")),
|
||||
compileInputPaths,
|
||||
contextBudget: APP_CONTEXT_BUDGET,
|
||||
reads: { ...source.reads },
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
createTelemetryPipeline,
|
||||
DEFAULT_TELEMETRY_PIPELINE_BUDGET,
|
||||
} from "../../../src/platform/telemetry/telemetry-pipeline.ts";
|
||||
import type { TelemetryEvent } from "../../../src/platform/telemetry/telemetry-event.ts";
|
||||
import { telemetryAmplification } from "../traffic/amplification-metrics.ts";
|
||||
import { recordScaleMetrics } from "../scale-artifact.ts";
|
||||
|
||||
const IDENTICAL: TelemetryEvent<unknown> = {
|
||||
kind: "catalog.load.failed",
|
||||
severity: "error",
|
||||
featureId: "catalog",
|
||||
payload: { operation: "list", reason: "NETWORK_UNREACHABLE" },
|
||||
};
|
||||
|
||||
/**
|
||||
* Spec §43. Many logical failures -> fingerprint/dedup/sample -> a bounded
|
||||
* number of buffered and sent telemetry events.
|
||||
*/
|
||||
async function runTelemetryStorm(
|
||||
options: Readonly<{
|
||||
identicalEvents: number;
|
||||
distinctKinds?: number;
|
||||
maxSendsPerMinute: number;
|
||||
maxBufferedEvents: number;
|
||||
collector?: "up" | "down";
|
||||
}>,
|
||||
) {
|
||||
let networkSends = 0;
|
||||
let now = 0;
|
||||
const pipeline = createTelemetryPipeline({
|
||||
transport: {
|
||||
async send() {
|
||||
networkSends += 1;
|
||||
if (options.collector === "down") throw new Error("collector unavailable");
|
||||
},
|
||||
},
|
||||
budget: {
|
||||
...DEFAULT_TELEMETRY_PIPELINE_BUDGET,
|
||||
maxSendsPerMinute: options.maxSendsPerMinute,
|
||||
maxBufferedEvents: options.maxBufferedEvents,
|
||||
},
|
||||
now: () => now,
|
||||
setTimer: () => 0,
|
||||
clearTimer: () => {},
|
||||
});
|
||||
let peakBufferedEvents = 0;
|
||||
const distinct = options.distinctKinds ?? 1;
|
||||
// The whole storm lands inside one rate/dedup window (0.5ms apart).
|
||||
for (let i = 0; i < options.identicalEvents; i += 1) {
|
||||
now += 0.5;
|
||||
await pipeline.emit({
|
||||
...IDENTICAL,
|
||||
payload: { operation: "list", reason: `R${i % distinct}` },
|
||||
interactionId: `interaction-${i}`,
|
||||
});
|
||||
if (i % 1_000 === 0) {
|
||||
await pipeline.flush();
|
||||
peakBufferedEvents = Math.max(peakBufferedEvents, pipeline.stats().buffered);
|
||||
}
|
||||
}
|
||||
await pipeline.flush();
|
||||
const stats = pipeline.stats();
|
||||
peakBufferedEvents = Math.max(peakBufferedEvents, stats.peakBuffered);
|
||||
pipeline.dispose();
|
||||
return { networkSends, peakBufferedEvents, stats };
|
||||
}
|
||||
|
||||
describe("telemetry storm", () => {
|
||||
it("does not turn 100000 identical failures into 100000 sends", async () => {
|
||||
const result = await runTelemetryStorm({
|
||||
identicalEvents: 100_000,
|
||||
maxSendsPerMinute: 20,
|
||||
maxBufferedEvents: 200,
|
||||
});
|
||||
|
||||
expect(result.networkSends).toBeLessThanOrEqual(20);
|
||||
expect(result.peakBufferedEvents).toBeLessThanOrEqual(200);
|
||||
expect(result.stats.deduplicated).toBe(99_999);
|
||||
expect(
|
||||
telemetryAmplification({ logicalEvents: 100_000, telemetrySends: result.networkSends }),
|
||||
).toBeLessThanOrEqual(0.0002);
|
||||
await recordScaleMetrics("telemetry-storm-identical", {
|
||||
emitted: 100_000,
|
||||
deduplicated: result.stats.deduplicated,
|
||||
sampledOut: result.stats.sampledOut,
|
||||
dropped: result.stats.dropped,
|
||||
transmitted: result.networkSends,
|
||||
peakBuffered: result.peakBufferedEvents,
|
||||
});
|
||||
});
|
||||
|
||||
it("bounds sends and buffer when a storm has many distinct fingerprints", async () => {
|
||||
const result = await runTelemetryStorm({
|
||||
identicalEvents: 100_000,
|
||||
distinctKinds: 5_000,
|
||||
maxSendsPerMinute: 20,
|
||||
maxBufferedEvents: 200,
|
||||
});
|
||||
expect(result.networkSends).toBeLessThanOrEqual(20);
|
||||
expect(result.peakBufferedEvents).toBeLessThanOrEqual(200);
|
||||
expect(result.stats.fingerprints).toBeLessThanOrEqual(
|
||||
DEFAULT_TELEMETRY_PIPELINE_BUDGET.maxFingerprints,
|
||||
);
|
||||
await recordScaleMetrics("telemetry-storm-distinct", {
|
||||
emitted: 100_000,
|
||||
deduplicated: result.stats.deduplicated,
|
||||
dropped: result.stats.dropped,
|
||||
transmitted: result.networkSends,
|
||||
peakBuffered: result.peakBufferedEvents,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* A feature-shaped business operation: it reports its failure through the
|
||||
* telemetry port fire-and-forget and returns its own result regardless.
|
||||
*/
|
||||
function createCatalogLoader(
|
||||
telemetry: Readonly<{ emit(event: TelemetryEvent<unknown>): unknown }>,
|
||||
upstream: () => Promise<boolean>,
|
||||
) {
|
||||
return async (): Promise<Readonly<{ ok: boolean }>> => {
|
||||
const ok = await upstream();
|
||||
if (!ok) {
|
||||
try {
|
||||
void Promise.resolve(
|
||||
telemetry.emit({ ...IDENTICAL, severity: "warning" }),
|
||||
).catch(() => undefined);
|
||||
} catch {
|
||||
// Contained at the call site.
|
||||
}
|
||||
}
|
||||
return { ok };
|
||||
};
|
||||
}
|
||||
|
||||
describe("collector outage", () => {
|
||||
it("keeps business promises fulfilled and buffers bounded while every send fails", async () => {
|
||||
const pipeline = createTelemetryPipeline({
|
||||
transport: { send: () => Promise.reject(new Error("collector unavailable")) },
|
||||
budget: { ...DEFAULT_TELEMETRY_PIPELINE_BUDGET, maxBufferedEvents: 50 },
|
||||
});
|
||||
let call = 0;
|
||||
const load = createCatalogLoader(pipeline, async () => call++ % 2 === 1);
|
||||
|
||||
const results = await Promise.all(Array.from({ length: 5_000 }, () => load()));
|
||||
await pipeline.flush();
|
||||
|
||||
expect(results).toHaveLength(5_000);
|
||||
expect(results.filter((result) => result.ok)).toHaveLength(2_500);
|
||||
expect(pipeline.stats().buffered).toBeLessThanOrEqual(50);
|
||||
expect(pipeline.stats().peakBuffered).toBeLessThanOrEqual(50);
|
||||
pipeline.dispose();
|
||||
});
|
||||
|
||||
it("keeps the business result when the port itself throws", async () => {
|
||||
const throwing = { emit: vi.fn(() => { throw new Error("port broken"); }) };
|
||||
const load = createCatalogLoader(throwing, async () => false);
|
||||
await expect(load()).resolves.toEqual({ ok: false });
|
||||
expect(throwing.emit).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
requestAmplification,
|
||||
retryAmplification,
|
||||
telemetryAmplification,
|
||||
} from "./amplification-metrics.ts";
|
||||
|
||||
describe("amplification metrics", () => {
|
||||
it("computes exact request and retry amplification", () => {
|
||||
expect(requestAmplification({
|
||||
interactions: 1_000,
|
||||
physicalRequests: 1_150,
|
||||
})).toBe(1.15);
|
||||
|
||||
expect(retryAmplification({
|
||||
initialRequests: 1_000,
|
||||
totalRequests: 1_200,
|
||||
})).toBe(1.2);
|
||||
|
||||
expect(telemetryAmplification({ logicalEvents: 100_000, telemetrySends: 20 })).toBe(
|
||||
0.0002,
|
||||
);
|
||||
});
|
||||
|
||||
it.each([0, -1, Number.NaN, Number.POSITIVE_INFINITY])(
|
||||
"rejects an invalid denominator %s",
|
||||
(denominator) => {
|
||||
expect(() =>
|
||||
requestAmplification({ interactions: denominator, physicalRequests: 1 }),
|
||||
).toThrow(RangeError);
|
||||
expect(() =>
|
||||
retryAmplification({ initialRequests: denominator, totalRequests: 1 }),
|
||||
).toThrow(RangeError);
|
||||
expect(() =>
|
||||
telemetryAmplification({ logicalEvents: denominator, telemetrySends: 1 }),
|
||||
).toThrow(RangeError);
|
||||
},
|
||||
);
|
||||
|
||||
it("rejects a negative numerator", () => {
|
||||
expect(() =>
|
||||
requestAmplification({ interactions: 1, physicalRequests: -1 }),
|
||||
).toThrow(RangeError);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Spec §34. Operational amplification metrics.
|
||||
*
|
||||
* - request amplification = physical requests / logical interactions
|
||||
* - retry amplification = total physical attempts / initial requests
|
||||
* - telemetry amplification = telemetry sends / logical failures or events
|
||||
*/
|
||||
function ratio(numerator: number, denominator: number, name: string): number {
|
||||
if (!Number.isFinite(denominator) || denominator <= 0) {
|
||||
throw new RangeError(`${name} denominator must be positive: ${denominator}`);
|
||||
}
|
||||
if (!Number.isFinite(numerator) || numerator < 0) {
|
||||
throw new RangeError(`${name} numerator must be non-negative: ${numerator}`);
|
||||
}
|
||||
return Math.round((numerator / denominator) * 1e9) / 1e9;
|
||||
}
|
||||
|
||||
export function requestAmplification(
|
||||
input: Readonly<{ interactions: number; physicalRequests: number }>,
|
||||
): number {
|
||||
return ratio(input.physicalRequests, input.interactions, "request amplification");
|
||||
}
|
||||
|
||||
export function retryAmplification(
|
||||
input: Readonly<{ initialRequests: number; totalRequests: number }>,
|
||||
): number {
|
||||
return ratio(input.totalRequests, input.initialRequests, "retry amplification");
|
||||
}
|
||||
|
||||
export function telemetryAmplification(
|
||||
input: Readonly<{ logicalEvents: number; telemetrySends: number }>,
|
||||
): number {
|
||||
return ratio(input.telemetrySends, input.logicalEvents, "telemetry amplification");
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
manualClock,
|
||||
scopeSnapshot,
|
||||
testExecutionContext,
|
||||
testExecutor,
|
||||
type Transport,
|
||||
} from "../../helpers/traffic-test-kit.ts";
|
||||
import {
|
||||
requestAmplification,
|
||||
retryAmplification,
|
||||
} from "./amplification-metrics.ts";
|
||||
import { runSyntheticTraffic, TEST_QUERY, upstream } from "./synthetic-traffic.ts";
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe("total outage", () => {
|
||||
it("bounds physical requests when all upstream calls fail", async () => {
|
||||
const result = await runSyntheticTraffic({
|
||||
interactions: 10_000,
|
||||
upstream: "always-fail",
|
||||
maxPhysicalRequestsPerInteraction: 2,
|
||||
maxRetriesPerInteraction: 1,
|
||||
});
|
||||
|
||||
expect(result.physicalRequests).toBeLessThanOrEqual(20_000);
|
||||
expect(result.succeeded).toBe(0);
|
||||
expect(requestAmplification(result)).toBeLessThanOrEqual(2);
|
||||
expect(
|
||||
retryAmplification({
|
||||
initialRequests: result.initialRequests,
|
||||
totalRequests: result.physicalRequests,
|
||||
}),
|
||||
).toBeLessThanOrEqual(2);
|
||||
}, 60_000);
|
||||
|
||||
it("keeps amplification bounded when one interaction fans out into many operations", async () => {
|
||||
const result = await runSyntheticTraffic({
|
||||
interactions: 1_000,
|
||||
upstream: "always-fail",
|
||||
maxPhysicalRequestsPerInteraction: 3,
|
||||
maxRetriesPerInteraction: 1,
|
||||
operationsPerInteraction: 6,
|
||||
});
|
||||
// Unbudgeted: 6 operations x 3 attempts = 18 per interaction.
|
||||
expect(requestAmplification(result)).toBeLessThanOrEqual(3);
|
||||
}, 60_000);
|
||||
|
||||
it("stays at 1x amplification when upstream is healthy", async () => {
|
||||
const result = await runSyntheticTraffic({
|
||||
interactions: 2_000,
|
||||
upstream: "healthy",
|
||||
maxPhysicalRequestsPerInteraction: 2,
|
||||
maxRetriesPerInteraction: 1,
|
||||
});
|
||||
expect(result.succeeded).toBe(2_000);
|
||||
expect(requestAmplification(result)).toBe(1);
|
||||
}, 60_000);
|
||||
});
|
||||
|
||||
describe("partial outage", () => {
|
||||
it("lets healthy contexts complete while one context fails completely", async () => {
|
||||
const transports = {
|
||||
payment: upstream("always-fail"),
|
||||
order: upstream("healthy"),
|
||||
catalog: upstream("healthy"),
|
||||
};
|
||||
const executors = Object.fromEntries(
|
||||
Object.entries(transports).map(([contextId, transport]) => [
|
||||
contextId,
|
||||
testExecutor(transport, { maxRetryAttempts: 2 }),
|
||||
]),
|
||||
) as Record<keyof typeof transports, ReturnType<typeof testExecutor>>;
|
||||
|
||||
const successes: Record<string, number> = { payment: 0, order: 0, catalog: 0 };
|
||||
await Promise.all(
|
||||
Array.from({ length: 300 }, async (_, index) => {
|
||||
const contextId = (["payment", "order", "catalog"] as const)[index % 3] as
|
||||
| "payment"
|
||||
| "order"
|
||||
| "catalog";
|
||||
const execution = testExecutionContext({ maxPhysicalRequests: 2, maxRetries: 1 });
|
||||
const outcome = await executors[contextId].execute(TEST_QUERY, { limit: 1 }, {
|
||||
routeId: "TEST_ROUTE",
|
||||
scope: scopeSnapshot(),
|
||||
execution,
|
||||
});
|
||||
if (outcome.kind === "SUCCESS") successes[contextId] = (successes[contextId] ?? 0) + 1;
|
||||
}),
|
||||
);
|
||||
|
||||
expect(successes).toEqual({ payment: 0, order: 100, catalog: 100 });
|
||||
expect(transports.payment.calls).toBeLessThanOrEqual(200);
|
||||
expect(transports.order.calls).toBe(100);
|
||||
expect(transports.catalog.calls).toBe(100);
|
||||
});
|
||||
});
|
||||
|
||||
describe("slow upstream", () => {
|
||||
it("bounds active and queued work, drains waiters at the deadline, and never retries after it", async () => {
|
||||
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
||||
const clock = manualClock(0);
|
||||
let active = 0;
|
||||
let peakActive = 0;
|
||||
let calls = 0;
|
||||
const transport: Transport = (_input, init) => {
|
||||
calls += 1;
|
||||
active += 1;
|
||||
peakActive = Math.max(peakActive, active);
|
||||
return new Promise<Response>((_resolve, reject) => {
|
||||
init?.signal?.addEventListener(
|
||||
"abort",
|
||||
() => {
|
||||
active -= 1;
|
||||
reject(new DOMException("aborted", "AbortError"));
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
});
|
||||
};
|
||||
const execution = testExecutionContext({
|
||||
deadlineMs: 1_000,
|
||||
maxPhysicalRequests: 20,
|
||||
maxRetries: 5,
|
||||
maxConcurrentRequests: 2,
|
||||
maxQueuedRequests: 3,
|
||||
clock: clock.now,
|
||||
});
|
||||
const executor = testExecutor(transport, { monotonicNow: clock.now });
|
||||
const context = { routeId: "TEST_ROUTE", scope: scopeSnapshot(), execution };
|
||||
|
||||
const pending = Array.from({ length: 10 }, () =>
|
||||
executor.execute(TEST_QUERY, { limit: 1 }, context),
|
||||
);
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
expect(execution.traffic.concurrency.active).toBe(2);
|
||||
expect(execution.traffic.concurrency.queued).toBe(3);
|
||||
|
||||
clock.advance(1_000);
|
||||
await vi.advanceTimersByTimeAsync(1_000);
|
||||
const outcomes = await Promise.all(pending);
|
||||
|
||||
expect(peakActive).toBeLessThanOrEqual(2);
|
||||
expect(execution.traffic.concurrency.active).toBe(0);
|
||||
expect(execution.traffic.concurrency.queued).toBe(0);
|
||||
expect(calls).toBe(2);
|
||||
expect(execution.traffic.retries.consumed).toBe(0);
|
||||
expect(
|
||||
outcomes.filter(
|
||||
(outcome) =>
|
||||
outcome.kind === "TRANSPORT_FAILURE" &&
|
||||
outcome.failure.kind === "OVERLOADED_BEFORE_SEND",
|
||||
),
|
||||
).toHaveLength(5);
|
||||
expect(
|
||||
outcomes.filter(
|
||||
(outcome) =>
|
||||
outcome.kind === "TRANSPORT_FAILURE" && outcome.failure.kind === "TIMEOUT",
|
||||
),
|
||||
).toHaveLength(5);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { retryDelayFor } from "../../../src/adapters/http/http-retry-lifecycle.ts";
|
||||
|
||||
/** Deterministic per-client PRNG (mulberry32), seeded by client index. */
|
||||
function seededRandom(seed: number): () => number {
|
||||
let state = seed >>> 0;
|
||||
return () => {
|
||||
state = (state + 0x6d2b79f5) >>> 0;
|
||||
let value = state;
|
||||
value = Math.imul(value ^ (value >>> 15), value | 1);
|
||||
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
|
||||
return ((value ^ (value >>> 14)) >>> 0) / 4_294_967_296;
|
||||
};
|
||||
}
|
||||
|
||||
function calculateRetryDelay(
|
||||
options: Readonly<{ retryAfterMs: number; jitterSeed: number; attempt: number }>,
|
||||
): number {
|
||||
const delay = retryDelayFor(
|
||||
options.retryAfterMs,
|
||||
options.attempt - 1,
|
||||
seededRandom(options.jitterSeed),
|
||||
);
|
||||
if (delay === null) throw new Error("Retry-After above the ceiling is not retried");
|
||||
return delay;
|
||||
}
|
||||
|
||||
describe("Retry-After jitter distribution", () => {
|
||||
it("spreads retries around Retry-After instead of synchronizing all clients", () => {
|
||||
const delays = Array.from({ length: 10_000 }, (_, seed) =>
|
||||
calculateRetryDelay({
|
||||
retryAfterMs: 3_000,
|
||||
jitterSeed: seed,
|
||||
attempt: 1,
|
||||
}),
|
||||
);
|
||||
|
||||
expect(new Set(delays).size).toBeGreaterThan(100);
|
||||
expect(Math.max(...delays) - Math.min(...delays)).toBeGreaterThan(500);
|
||||
});
|
||||
|
||||
it("never retries earlier than the server asked", () => {
|
||||
for (let seed = 0; seed < 1_000; seed += 1) {
|
||||
expect(
|
||||
calculateRetryDelay({ retryAfterMs: 1_000, jitterSeed: seed, attempt: 1 }),
|
||||
).toBeGreaterThanOrEqual(1_000);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps the jitter window bounded", () => {
|
||||
const delays = Array.from({ length: 5_000 }, (_, seed) =>
|
||||
calculateRetryDelay({ retryAfterMs: 5_000, jitterSeed: seed, attempt: 3 }),
|
||||
);
|
||||
expect(Math.max(...delays)).toBeLessThan(5_000 + 2_000);
|
||||
});
|
||||
|
||||
it("spreads a cohort into many small buckets rather than one spike", () => {
|
||||
const perBucket = new Map<number, number>();
|
||||
for (let seed = 0; seed < 10_000; seed += 1) {
|
||||
const bucket = Math.floor(
|
||||
calculateRetryDelay({ retryAfterMs: 3_000, jitterSeed: seed, attempt: 1 }) / 50,
|
||||
);
|
||||
perBucket.set(bucket, (perBucket.get(bucket) ?? 0) + 1);
|
||||
}
|
||||
// No 50ms slice receives more than ~10% of the cohort.
|
||||
expect(Math.max(...perBucket.values())).toBeLessThan(1_000);
|
||||
});
|
||||
|
||||
it("does not retry when Retry-After exceeds the ceiling", () => {
|
||||
expect(retryDelayFor(10_000, 0, () => 0.5)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
import type { InstalledHttpContract } from "../../../src/contracts/external-contract-runtime.ts";
|
||||
import { TEST_LIST_HTTP_CONTRACT } from "../../helpers/external-contract-fixture.ts";
|
||||
import {
|
||||
jsonRows,
|
||||
networkFailure,
|
||||
scopeSnapshot,
|
||||
testExecutionContext,
|
||||
testExecutor,
|
||||
type Transport,
|
||||
} from "../../helpers/traffic-test-kit.ts";
|
||||
|
||||
export const TEST_QUERY = TEST_LIST_HTTP_CONTRACT as InstalledHttpContract<
|
||||
unknown,
|
||||
unknown,
|
||||
unknown
|
||||
>;
|
||||
|
||||
export type UpstreamMode = "always-fail" | "healthy" | "fail-first";
|
||||
|
||||
export function upstream(mode: UpstreamMode): Transport & { calls: number } {
|
||||
let seen = 0;
|
||||
const transport = (async () => {
|
||||
transport.calls += 1;
|
||||
seen += 1;
|
||||
if (mode === "always-fail") return networkFailure();
|
||||
if (mode === "fail-first" && seen % 2 === 1) return networkFailure();
|
||||
return jsonRows(1);
|
||||
}) as unknown as Transport & { calls: number };
|
||||
transport.calls = 0;
|
||||
return transport;
|
||||
}
|
||||
|
||||
export type SyntheticTrafficResult = Readonly<{
|
||||
interactions: number;
|
||||
physicalRequests: number;
|
||||
initialRequests: number;
|
||||
succeeded: number;
|
||||
failed: number;
|
||||
}>;
|
||||
|
||||
/**
|
||||
* Spec §32 / §42. Drives many independent interactions through the real V3
|
||||
* executor, each with its own interaction budget, and counts physical
|
||||
* dispatches at the transport.
|
||||
*/
|
||||
export async function runSyntheticTraffic(
|
||||
options: Readonly<{
|
||||
interactions: number;
|
||||
upstream: UpstreamMode;
|
||||
maxPhysicalRequestsPerInteraction: number;
|
||||
maxRetriesPerInteraction: number;
|
||||
operationsPerInteraction?: number;
|
||||
batchSize?: number;
|
||||
}>,
|
||||
): Promise<SyntheticTrafficResult> {
|
||||
const transport = upstream(options.upstream);
|
||||
const executor = testExecutor(transport, { maxRetryAttempts: 2 });
|
||||
const operations = options.operationsPerInteraction ?? 1;
|
||||
const batchSize = options.batchSize ?? 500;
|
||||
let succeeded = 0;
|
||||
let failed = 0;
|
||||
for (let start = 0; start < options.interactions; start += batchSize) {
|
||||
const size = Math.min(batchSize, options.interactions - start);
|
||||
const outcomes = await Promise.all(
|
||||
Array.from({ length: size }, async () => {
|
||||
const execution = testExecutionContext({
|
||||
maxPhysicalRequests: options.maxPhysicalRequestsPerInteraction,
|
||||
maxRetries: options.maxRetriesPerInteraction,
|
||||
maxConcurrentRequests: 2,
|
||||
maxQueuedRequests: 8,
|
||||
});
|
||||
const context = { routeId: "TEST_ROUTE", scope: scopeSnapshot(), execution };
|
||||
return Promise.all(
|
||||
Array.from({ length: operations }, () =>
|
||||
executor.execute(TEST_QUERY, { limit: 1 }, context),
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
for (const outcome of outcomes.flat()) {
|
||||
if (outcome.kind === "SUCCESS") succeeded += 1;
|
||||
else failed += 1;
|
||||
}
|
||||
}
|
||||
return Object.freeze({
|
||||
interactions: options.interactions,
|
||||
physicalRequests: transport.calls,
|
||||
initialRequests: options.interactions * operations,
|
||||
succeeded,
|
||||
failed,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseTscExtendedDiagnostics } from "../../scripts/scale/collect-tsc-diagnostics.ts";
|
||||
|
||||
describe("parseTscExtendedDiagnostics", () => {
|
||||
it("parses the documented diagnostic fields", () => {
|
||||
const text = `
|
||||
Files: 321
|
||||
Types: 12000
|
||||
Symbols: 18000
|
||||
Instantiations: 5000
|
||||
Memory used: 128000K
|
||||
Total time: 4.20s
|
||||
`;
|
||||
|
||||
expect(parseTscExtendedDiagnostics(text)).toMatchObject({
|
||||
files: 321,
|
||||
types: 12_000,
|
||||
symbols: 18_000,
|
||||
instantiations: 5_000,
|
||||
memoryKb: 128_000,
|
||||
totalTimeSeconds: 4.2,
|
||||
});
|
||||
});
|
||||
|
||||
it("parses real TypeScript 7 output captured from this repository", async () => {
|
||||
const text = await readFile(
|
||||
path.resolve("tests/scale/fixtures/typescript/extended-diagnostics.txt"),
|
||||
"utf8",
|
||||
);
|
||||
const parsed = parseTscExtendedDiagnostics(text);
|
||||
expect(parsed.files).toBeGreaterThan(0);
|
||||
expect(parsed.types).toBeGreaterThan(0);
|
||||
expect(parsed.instantiations).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("fails loudly when a required metric is missing", () => {
|
||||
expect(() => parseTscExtendedDiagnostics("Files: 1\nTypes: 2\n")).toThrow(
|
||||
/symbols/,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user