Files
clean-architecture-frontend…/tests/scale/browser-soak.contract.test.ts
T

154 lines
5.1 KiB
TypeScript

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");
});
});