diff --git a/tests/platform/cardinality/extreme-scale-registry.test.ts b/tests/platform/cardinality/extreme-scale-registry.test.ts index 255b459f..84e7de93 100644 --- a/tests/platform/cardinality/extreme-scale-registry.test.ts +++ b/tests/platform/cardinality/extreme-scale-registry.test.ts @@ -7,106 +7,143 @@ import { selectCompiledProductFeatures, } from "../../../src/contracts/product-features.ts"; import { composeUniqueRecord } from "../../../src/platform/cardinality/compose-unique-record.ts"; +import { assertLinearOrBetter } from "../../../src/platform/cardinality/growth-regression.ts"; +/* + * This file runs in merge gates on CPUs nobody chose (FE-GATE-005 coverage and + * the pr scale tier), so it asserts only what holds on any CPU: how a cost + * grows from 10k to 100k entries, and how a registry lookup compares with a + * plain-object lookup timed in the same process. Absolute timings belong to + * check:scale:baseline, which compares them only on the CPU that recorded + * config/scale/benchmark-baseline.json. + */ const SMALL_REGISTRY_SIZE = 10_000; const LARGE_REGISTRY_SIZE = 100_000; -const FEATURE_SCALE_SIZE = 100_000; -const LOOKUP_OPERATIONS = 1_000_000; +const SIZE_RATIO = LARGE_REGISTRY_SIZE / SMALL_REGISTRY_SIZE; +const LOOKUP_OPERATIONS = 200_000; const MEASUREMENT_RUNS = 3; const MAX_COMPOSE_GROWTH_RATIO = 30; -const MAX_LARGE_COMPOSE_MS = 3_000; -const MAX_MEMBERSHIP_MS = 3_000; -const MAX_LOOKUP_MS = 1_500; +/** Registry lookups against plain-object lookups on the same 100k keys. */ +const MAX_LOOKUP_OVERHEAD_RATIO = 3; function syntheticRecords(count: number): readonly Readonly>[] { return Array.from({ length: count }, (_, index) => ({ [`key-${index}`]: index })); } -function median(values: readonly number[]): number { - const sorted = [...values].sort((left, right) => left - right); - return sorted[Math.floor(sorted.length / 2)] ?? Number.POSITIVE_INFINITY; +function elapsedMs(action: () => void, repetitions: number): number { + const startedAt = performance.now(); + for (let repetition = 0; repetition < repetitions; repetition += 1) action(); + return (performance.now() - startedAt) / repetitions; } -function measureMedianElapsedMs(action: () => void): number { - action(); - const samples = Array.from({ length: MEASUREMENT_RUNS }, () => { - const startedAt = performance.now(); - action(); - return performance.now() - startedAt; - }); - return median(samples); +/** + * Per-call cost of `left` and `right`, timed in alternating runs so both see + * the same background load. `leftRepetitions` makes the 10k side do as much + * work per run as the 100k side: on a shared core a 10ms run can finish inside + * one scheduler slice while a 100ms run waits behind other processes, which + * reads as steeper growth than the code has. Other processes and GC pauses only + * ever add time, so each side keeps its fastest run (check:scale:baseline reads + * growth from minMs for the same reason). + */ +function timeAlternately( + left: () => void, + right: () => void, + leftRepetitions = 1, +): Readonly<{ leftMs: number; rightMs: number }> { + left(); + right(); + let leftMs = Number.POSITIVE_INFINITY; + let rightMs = Number.POSITIVE_INFINITY; + for (let run = 0; run < MEASUREMENT_RUNS; run += 1) { + leftMs = Math.min(leftMs, elapsedMs(left, leftRepetitions)); + rightMs = Math.min(rightMs, elapsedMs(right, 1)); + } + return Object.freeze({ leftMs, rightMs }); } -function measureCompose(records: readonly Readonly>[]): Readonly<{ - elapsedMs: number; - registry: Readonly>; -}> { - let registry: Readonly> = {}; - const elapsedMs = measureMedianElapsedMs(() => { - registry = composeUniqueRecord(records, () => "synthetic registry"); - }); - return Object.freeze({ elapsedMs, registry }); +function lookupChecksum(registry: Readonly>): number { + let checksum = 0; + for (let index = 0; index < LOOKUP_OPERATIONS; index += 1) { + checksum += registry[`key-${index % LARGE_REGISTRY_SIZE}`] ?? -1; + } + return checksum; } describe("extreme-scale registry composition", () => { it("keeps 10k -> 100k composition growth below a quadratic regression budget", () => { const smallRecords = syntheticRecords(SMALL_REGISTRY_SIZE); const largeRecords = syntheticRecords(LARGE_REGISTRY_SIZE); - const small = measureCompose(smallRecords); - const large = measureCompose(largeRecords); - const growthRatio = large.elapsedMs / Math.max(small.elapsedMs, 1); + const compose = (records: typeof smallRecords) => + composeUniqueRecord(records, () => "synthetic registry"); + const { leftMs: smallMs, rightMs: largeMs } = timeAlternately( + () => compose(smallRecords), + () => compose(largeRecords), + SIZE_RATIO, + ); + const growthRatio = largeMs / Math.max(smallMs, 1); - expect(Object.keys(small.registry)).toHaveLength(SMALL_REGISTRY_SIZE); - expect(Object.keys(large.registry)).toHaveLength(LARGE_REGISTRY_SIZE); - expect(large.elapsedMs).toBeLessThan(MAX_LARGE_COMPOSE_MS); + expect(Object.keys(compose(smallRecords))).toHaveLength(SMALL_REGISTRY_SIZE); + expect(Object.keys(compose(largeRecords))).toHaveLength(LARGE_REGISTRY_SIZE); expect(growthRatio).toBeLessThan(MAX_COMPOSE_GROWTH_RATIO); }); - it("keeps one million lookups bounded on a 100k-entry runtime registry", () => { - const { registry } = measureCompose(syntheticRecords(LARGE_REGISTRY_SIZE)); - let checksum = 0; - const elapsedMs = measureMedianElapsedMs(() => { - let currentChecksum = 0; - for (let index = 0; index < LOOKUP_OPERATIONS; index += 1) { - currentChecksum += registry[`key-${index % LARGE_REGISTRY_SIZE}`] ?? -1; - } - checksum = currentChecksum; - }); + it("keeps lookups on a 100k-entry registry as cheap as on a plain object", () => { + const records = syntheticRecords(LARGE_REGISTRY_SIZE); + const registry = composeUniqueRecord(records, () => "synthetic registry"); + const plain: Record = {}; + for (const record of records) Object.assign(plain, record); + const { leftMs: registryMs, rightMs: plainMs } = timeAlternately( + () => lookupChecksum(registry), + () => lookupChecksum(plain), + ); + + const checksum = lookupChecksum(registry); expect(checksum).toBeGreaterThan(0); - expect(elapsedMs).toBeLessThan(MAX_LOOKUP_MS); + expect(checksum).toBe(lookupChecksum(plain)); + expect(registryMs / plainMs).toBeLessThan(MAX_LOOKUP_OVERHEAD_RATIO); }); }); +function membershipInput(size: number) { + const compiled = Array.from({ length: size }, (_, index) => + Object.freeze({ featureId: `feature-${index.toString().padStart(6, "0")}` }), + ); + const declared = compiled + .filter((_, index) => index % 10 === 0) + .map((feature) => feature.featureId) + .join(","); + const compiledIds = compiled.map((feature) => feature.featureId); + const installedIds = selectCompiledProductFeatures(compiled, declared).map( + (feature) => feature.featureId, + ); + return { + select: () => selectCompiledProductFeatures(compiled, declared), + resolve: () => resolveProductFeatures(compiledIds, installedIds), + }; +} + describe("extreme-scale product feature membership", () => { - it("selects and resolves 100k feature ids within the membership budget", () => { - const compiled = Array.from({ length: FEATURE_SCALE_SIZE }, (_, index) => - Object.freeze({ featureId: `feature-${index.toString().padStart(6, "0")}` }), - ); - const requestedIds = compiled - .filter((_, index) => index % 10 === 0) - .map((feature) => feature.featureId); - const declared = requestedIds.join(","); + it("selects and resolves 100k feature ids with near-linear growth from 10k", () => { + const small = membershipInput(SMALL_REGISTRY_SIZE); + const large = membershipInput(LARGE_REGISTRY_SIZE); + const selection = timeAlternately(small.select, large.select, SIZE_RATIO); + const resolution = timeAlternately(small.resolve, large.resolve, SIZE_RATIO); + const statuses = large.resolve(); - let selected = compiled.slice(0, 0); - const selectionElapsedMs = measureMedianElapsedMs(() => { - selected = [...selectCompiledProductFeatures(compiled, declared)]; - }); - - let statuses = resolveProductFeatures([], []); - const installedIds = selected.map((feature) => feature.featureId); - const compiledIds = compiled.map((feature) => feature.featureId); - const resolveElapsedMs = measureMedianElapsedMs(() => { - statuses = resolveProductFeatures(compiledIds, installedIds); - }); - - expect(selected).toHaveLength(FEATURE_SCALE_SIZE / 10); - expect(statuses).toHaveLength(FEATURE_SCALE_SIZE); + expect(large.select()).toHaveLength(LARGE_REGISTRY_SIZE / 10); + expect(statuses).toHaveLength(LARGE_REGISTRY_SIZE); expect(statuses.filter((status) => status.state === "ACTIVE")).toHaveLength( - FEATURE_SCALE_SIZE / 10, + LARGE_REGISTRY_SIZE / 10, ); - expect(selectionElapsedMs).toBeLessThan(MAX_MEMBERSHIP_MS); - expect(resolveElapsedMs).toBeLessThan(MAX_MEMBERSHIP_MS); + assertLinearOrBetter([ + { n: SMALL_REGISTRY_SIZE, cost: selection.leftMs }, + { n: LARGE_REGISTRY_SIZE, cost: selection.rightMs }, + ]); + assertLinearOrBetter([ + { n: SMALL_REGISTRY_SIZE, cost: resolution.leftMs }, + { n: LARGE_REGISTRY_SIZE, cost: resolution.rightMs }, + ]); }); });