src/lib/perf/measure.ts
Linked repository file from this build.
import { gzipSync } from "node:zlib";
import type { StoredReview } from "@/features/reviews/review-store";
import type { CatalogReview } from "@/features/catalog/query";
import type { SnapReview } from "@/programs/snap-negative/types";
import { fiscalYear, reviewKey } from "@/features/reviews/review-key";
/** JSON byte length of the value the browser would parse. */
export function jsonBytes(value: unknown): number {
return Buffer.byteLength(JSON.stringify(value));
}
/** Local gzip estimate; actual HTTP compression is measured by the browser suite. */
export function gzipBytes(value: unknown): number {
return gzipSync(Buffer.from(JSON.stringify(value))).byteLength;
}
/** Median wall time after one warmup call. Timing tests use this to ignore the first compile. */
export function medianMs(work: () => void, runs = 5): number {
work();
const samples: number[] = [];
for (let index = 0; index < runs; index++) {
const started = performance.now();
work();
samples.push(performance.now() - started);
}
samples.sort((left, right) => left - right);
return samples[Math.floor(samples.length / 2)]!;
}
export async function medianAsyncMs(work: () => Promise<void>, runs = 3): Promise<number> {
await work();
const samples: number[] = [];
for (let index = 0; index < runs; index++) {
const started = performance.now();
await work();
samples.push(performance.now() - started);
}
samples.sort((left, right) => left - right);
return samples[Math.floor(samples.length / 2)]!;
}
/** Wire shape of a single workspace response. */
export function storedReview(review: SnapReview): StoredReview {
return {
review,
version: 1,
requestId: review.reviewNumber,
actor: { name: "Seed", role: "examiner" },
savedAt: "2026-09-08T12:00:00.000Z",
};
}
/** Wire shape of one /api/catalog page. Catalog already uses this projection in production. */
export function catalogPage(reviews: SnapReview[]): CatalogReview[] {
return reviews.slice(0, 10).map((review) => ({
reviewNumber: review.reviewNumber,
reviewKey: reviewKey(review),
fiscalYear: fiscalYear(review.sampleMonth),
clientName: review.clientName,
caseNumber: review.caseNumber,
examiner: review.examiner,
state: review.state,
outcome: review.outcome,
updatedAt: "2026-09-08T12:00:00.000Z",
routedBy: review.examiner,
unread: false,
flagged: false,
}));
}