Add perf regression test harnesses

- Seed reusable perf fixtures for server scenarios
- Add perf provider adapter and web perf tests
- Capture artifacts for virtualization and websocket runs
This commit is contained in:
Julius Marminge committed 2026-04-13 10:05:10 -07:00
1 parent 1bf048eb8b
commit eecc2bc28d
26 files changed
+3688 -5

No files matched your search

+123
View File
@@ -0,0 +1,123 @@
import { mkdir, writeFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import type { PerfThresholdProfile } from "./thresholds.ts";
export interface PerfActionDuration {
readonly name: string;
readonly durationMs: number;
readonly startedAtMs: number;
readonly endedAtMs: number;
}
export interface PerfLongTaskSample {
readonly startTimeMs: number;
readonly durationMs: number;
readonly name: string;
}
export interface PerfMountedRowSample {
readonly label: string;
readonly count: number;
readonly capturedAtMs: number;
}
export interface BrowserPerfMetrics {
readonly actions: ReadonlyArray<PerfActionDuration>;
readonly longTasks: ReadonlyArray<PerfLongTaskSample>;
readonly rafGapsMs: ReadonlyArray<number>;
readonly mountedRowSamples: ReadonlyArray<PerfMountedRowSample>;
}
export interface PerfServerMetricSample {
readonly capturedAt: string;
readonly cpuUserMicros?: number;
readonly cpuSystemMicros?: number;
readonly rssBytes?: number;
readonly heapTotalBytes?: number;
readonly heapUsedBytes?: number;
readonly externalBytes?: number;
readonly arrayBuffersBytes?: number;
}
export interface PerfArtifactSummary {
readonly maxMountedTimelineRows: number;
readonly threadSwitchP50Ms: number | null;
readonly threadSwitchP95Ms: number | null;
readonly maxLongTaskMs: number;
readonly longTasksOver50Ms: number;
readonly maxRafGapMs: number;
readonly burstCompletionMs: number | null;
}
export interface PerfRunArtifact {
readonly suite: string;
readonly scenarioId: string;
readonly startedAt: string;
readonly completedAt: string;
readonly thresholds: PerfThresholdProfile;
readonly summary: PerfArtifactSummary;
readonly browserMetrics: BrowserPerfMetrics;
readonly serverMetrics: ReadonlyArray<PerfServerMetricSample> | null;
readonly metadata?: Record<string, unknown>;
}
export function percentile(values: ReadonlyArray<number>, target: number): number | null {
if (values.length === 0) {
return null;
}
const sorted = values.toSorted((left, right) => left - right);
const clampedTarget = Math.min(Math.max(target, 0), 1);
const index = Math.min(sorted.length - 1, Math.ceil(sorted.length * clampedTarget) - 1);
return sorted[index] ?? null;
}
export function summarizeBrowserPerfMetrics(
browserMetrics: BrowserPerfMetrics,
options?: {
readonly threadSwitchActionPrefix?: string;
readonly burstActionName?: string;
},
): PerfArtifactSummary {
const threadSwitchActions = browserMetrics.actions
.filter((action) =>
options?.threadSwitchActionPrefix
? action.name.startsWith(options.threadSwitchActionPrefix)
: action.name.startsWith("thread-switch"),
)
.map((action) => action.durationMs);
const burstCompletionAction = browserMetrics.actions.find(
(action) => action.name === (options?.burstActionName ?? "burst-completion"),
);
const maxMountedTimelineRows = browserMetrics.mountedRowSamples.reduce(
(maxCount, sample) => Math.max(maxCount, sample.count),
0,
);
const maxLongTaskMs = browserMetrics.longTasks.reduce(
(maxDuration, sample) => Math.max(maxDuration, sample.durationMs),
0,
);
const longTasksOver50Ms = browserMetrics.longTasks.filter(
(sample) => sample.durationMs > 50,
).length;
const maxRafGapMs = browserMetrics.rafGapsMs.reduce((maxGap, gap) => Math.max(maxGap, gap), 0);
return {
maxMountedTimelineRows,
threadSwitchP50Ms: percentile(threadSwitchActions, 0.5),
threadSwitchP95Ms: percentile(threadSwitchActions, 0.95),
maxLongTaskMs,
longTasksOver50Ms,
maxRafGapMs,
burstCompletionMs: burstCompletionAction?.durationMs ?? null,
};
}
export async function writePerfArtifact(
outputPath: string,
artifact: PerfRunArtifact,
): Promise<void> {
const resolvedOutputPath = resolve(outputPath);
await mkdir(dirname(resolvedOutputPath), { recursive: true });
await writeFile(`${resolvedOutputPath}`, `${JSON.stringify(artifact, null, 2)}\n`, "utf8");
}
+109
View File
@@ -0,0 +1,109 @@
import type { BrowserPerfMetrics } from "./artifact.ts";
export const PERF_BROWSER_GLOBAL = "__t3PerfCollector";
const DEFAULT_TIMELINE_ROW_SELECTOR = "[data-timeline-row-kind]";
export interface BrowserPerfCollector {
readonly startAction: (name: string) => void;
readonly endAction: (name: string) => number | null;
readonly sampleMountedRows: (label: string) => number;
readonly snapshot: () => BrowserPerfMetrics;
readonly reset: () => void;
}
declare global {
interface Window {
__t3PerfCollector?: BrowserPerfCollector;
}
}
export function installBrowserPerfCollector(
timelineRowSelector = DEFAULT_TIMELINE_ROW_SELECTOR,
): void {
if (typeof window === "undefined" || window.__t3PerfCollector) {
return;
}
const actionStarts = new Map<string, number>();
const actions: Array<BrowserPerfMetrics["actions"][number]> = [];
const longTasks: Array<BrowserPerfMetrics["longTasks"][number]> = [];
const rafGapsMs: number[] = [];
const mountedRowSamples: Array<BrowserPerfMetrics["mountedRowSamples"][number]> = [];
let previousAnimationFrameTs = 0;
const animationFrameLoop = (timestampMs: number) => {
if (previousAnimationFrameTs > 0) {
rafGapsMs.push(timestampMs - previousAnimationFrameTs);
}
previousAnimationFrameTs = timestampMs;
window.requestAnimationFrame(animationFrameLoop);
};
window.requestAnimationFrame(animationFrameLoop);
if (typeof PerformanceObserver !== "undefined") {
try {
const longTaskObserver = new PerformanceObserver((list: PerformanceObserverEntryList) => {
for (const entry of list.getEntries()) {
longTasks.push({
name: entry.name,
startTimeMs: entry.startTime,
durationMs: entry.duration,
});
}
});
longTaskObserver.observe({ entryTypes: ["longtask"] } as PerformanceObserverInit);
} catch {
// Ignore browsers or runtimes that do not expose long-task observation.
}
}
const readMountedRows = () => document.querySelectorAll(timelineRowSelector).length;
window.__t3PerfCollector = {
startAction(name: string) {
actionStarts.set(name, performance.now());
},
endAction(name: string) {
const startedAtMs = actionStarts.get(name);
if (startedAtMs === undefined) {
return null;
}
const endedAtMs = performance.now();
const durationMs = endedAtMs - startedAtMs;
actions.push({
name,
durationMs,
startedAtMs,
endedAtMs,
});
actionStarts.delete(name);
return durationMs;
},
sampleMountedRows(label: string) {
const count = readMountedRows();
mountedRowSamples.push({
label,
count,
capturedAtMs: performance.now(),
});
return count;
},
snapshot() {
return {
actions: [...actions],
longTasks: [...longTasks],
rafGapsMs: [...rafGapsMs],
mountedRowSamples: [...mountedRowSamples],
};
},
reset() {
actionStarts.clear();
actions.length = 0;
longTasks.length = 0;
rafGapsMs.length = 0;
mountedRowSamples.length = 0;
previousAnimationFrameTs = 0;
window.requestAnimationFrame(animationFrameLoop);
},
};
}
+1
View File
@@ -0,0 +1 @@
export * from "../../../packages/shared/src/perf/scenarioCatalog";
+14
View File
@@ -0,0 +1,14 @@
import type { PerfServerMetricSample } from "./artifact.ts";
export interface PerfServerSampler {
readonly start: (input: { readonly pid: number }) => Promise<void>;
readonly stop: () => Promise<ReadonlyArray<PerfServerMetricSample> | null>;
}
export class NoopServerSampler implements PerfServerSampler {
async start(_input: { readonly pid: number }): Promise<void> {}
async stop(): Promise<null> {
return null;
}
}
+23
View File
@@ -0,0 +1,23 @@
export interface PerfThresholdProfile {
readonly maxMountedTimelineRows: number;
readonly threadSwitchP50Ms: number;
readonly threadSwitchP95Ms: number;
readonly maxLongTaskMs: number;
readonly maxRafGapMs: number;
readonly burstCompletionMs: number;
readonly longTasksOver50MsMax: number;
}
export const PERF_THRESHOLDS = {
local: {
maxMountedTimelineRows: 140,
threadSwitchP50Ms: 250,
threadSwitchP95Ms: 500,
maxLongTaskMs: 120,
maxRafGapMs: 120,
burstCompletionMs: 5_000,
longTasksOver50MsMax: 2,
},
} as const satisfies Record<string, PerfThresholdProfile>;
export type PerfThresholdProfileName = keyof typeof PERF_THRESHOLDS;