Compare commits

...
Author SHA1 Message Date
Cursor Agent 89497fed15 fix: evict rejected template dir promises and extract shared perf process utilities
- Fix getTemplateDir to evict the cached promise on rejection so transient
  failures don't permanently poison the cache for subsequent calls.

- Extract pickFreePort, waitForServerReady, stopChildProcess, cleanupPerfRunDir,
  verifyBuiltArtifacts, and parsePerfSeededState into test/perf/support/perfProcess.ts,
  replacing duplicated copies in scripts/open-perf-app.ts and
  apps/web/test/perf/appHarness.ts.
2026-04-02 08:27:04 +00:00
6 changed files with 130 additions and 203 deletions

No files matched your search

@@ -546,7 +546,10 @@ async function getTemplateDir(scenarioId: PerfSeedScenarioId): Promise<string> {
if (existing) {
return existing;
}
const created = createTemplateDir(scenarioId);
const created = createTemplateDir(scenarioId).catch((error: unknown) => {
templateDirPromises.delete(scenarioId);
throw error;
});
templateDirPromises.set(scenarioId, created);
return created;
}
+12 -101
View File
@@ -1,6 +1,5 @@
import { spawn, type ChildProcess } from "node:child_process";
import { access, mkdir, rm, writeFile } from "node:fs/promises";
import { createServer } from "node:net";
import { spawn } from "node:child_process";
import { mkdir, writeFile } from "node:fs/promises";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { once } from "node:events";
@@ -18,6 +17,14 @@ import {
installBrowserPerfCollector,
PERF_BROWSER_GLOBAL,
} from "../../../../test/perf/support/browserMetrics";
import {
pickFreePort,
waitForServerReady,
stopChildProcess,
cleanupPerfRunDir,
verifyBuiltArtifacts,
parsePerfSeededState,
} from "../../../../test/perf/support/perfProcess";
import type { PerfThresholdProfile } from "../../../../test/perf/support/thresholds";
import type {
PerfProviderScenarioId,
@@ -35,8 +42,6 @@ const serverBinPath = resolve(repoRoot, "apps/server/dist/bin.mjs");
const serverClientIndexPath = resolve(repoRoot, "apps/server/dist/client/index.html");
const PERF_ARTIFACT_DIR_ENV = "T3CODE_PERF_ARTIFACT_DIR";
const PERF_HEADFUL_ENV = "T3CODE_PERF_HEADFUL";
const PERF_SEED_JSON_START = "__T3_PERF_SEED_JSON_START__";
const PERF_SEED_JSON_END = "__T3_PERF_SEED_JSON_END__";
interface PerfSeedThreadSummary {
readonly id: string;
@@ -104,84 +109,6 @@ export interface PerfAppHarness {
}>;
}
async function pickFreePort(): Promise<number> {
return await new Promise<number>((resolvePort, reject) => {
const server = createServer();
server.on("error", reject);
server.listen(0, "127.0.0.1", () => {
const address = server.address();
if (!address || typeof address === "string") {
reject(new Error("Unable to resolve a free localhost port."));
return;
}
const { port } = address;
server.close((closeError) => {
if (closeError) {
reject(closeError);
return;
}
resolvePort(port);
});
});
});
}
async function waitForServerReady(url: string, process: ChildProcess): Promise<void> {
const startedAt = Date.now();
const timeoutMs = 45_000;
const requestTimeoutMs = 1_000;
while (Date.now() - startedAt < timeoutMs) {
if (process.exitCode !== null) {
throw new Error(`Perf server exited early with code ${process.exitCode}.`);
}
try {
const response = await fetch(url, {
redirect: "manual",
signal: AbortSignal.timeout(requestTimeoutMs),
});
if (response.ok) {
return;
}
} catch {
// Ignore connection races while the server is still starting.
}
await new Promise((resolveDelay) => setTimeout(resolveDelay, 200));
}
throw new Error(`Timed out waiting for perf server readiness at ${url}.`);
}
async function verifyBuiltArtifacts(): Promise<void> {
await Promise.all([access(serverBinPath), access(serverClientIndexPath)]).catch(() => {
throw new Error(
`Built perf artifacts are missing. Expected ${serverBinPath} and ${serverClientIndexPath}. Run bun run test:perf:web or build the app first.`,
);
});
}
async function stopChildProcess(process: ChildProcess): Promise<void> {
if (process.exitCode !== null) {
return;
}
process.kill("SIGTERM");
const exited = await new Promise<boolean>((resolveExited) => {
const timer = setTimeout(() => resolveExited(false), 5_000);
process.once("exit", () => {
clearTimeout(timer);
resolveExited(true);
});
});
if (!exited && process.exitCode === null) {
process.kill("SIGKILL");
await new Promise<void>((resolveExited) => {
process.once("exit", () => resolveExited());
});
}
}
async function ensureArtifactDir(suite: string, scenarioId: string): Promise<string> {
const baseArtifactDir = resolve(
process.env[PERF_ARTIFACT_DIR_ENV] ?? join(repoRoot, "artifacts/perf"),
@@ -192,10 +119,6 @@ async function ensureArtifactDir(suite: string, scenarioId: string): Promise<str
return artifactDir;
}
async function cleanupPerfRunDir(runParentDir: string): Promise<void> {
await rm(runParentDir, { recursive: true, force: true });
}
async function writeServerLogs(
artifactDir: string,
stdout: string,
@@ -231,22 +154,10 @@ async function invokeBrowserCollector<T>(
);
}
function parsePerfSeededState(stdout: string): PerfSeededState {
const startIndex = stdout.lastIndexOf(PERF_SEED_JSON_START);
const endIndex = stdout.lastIndexOf(PERF_SEED_JSON_END);
if (startIndex !== -1 && endIndex !== -1 && endIndex > startIndex) {
const payload = stdout.slice(startIndex + PERF_SEED_JSON_START.length, endIndex).trim();
return JSON.parse(payload) as PerfSeededState;
}
return JSON.parse(stdout) as PerfSeededState;
}
export async function startPerfAppHarness(
options: StartPerfAppHarnessOptions,
): Promise<PerfAppHarness> {
await verifyBuiltArtifacts();
await verifyBuiltArtifacts([serverBinPath, serverClientIndexPath]);
const seededState = await (async () => {
const seedProcess = spawn(
@@ -270,7 +181,7 @@ export async function startPerfAppHarness(
if (exitCode !== 0) {
throw new Error(`Perf seed command failed with code ${exitCode ?? "unknown"}.\n${stderr}`);
}
return parsePerfSeededState(stdout);
return parsePerfSeededState<PerfSeededState>(stdout);
})();
const artifactDir = await ensureArtifactDir(options.suite, options.seedScenarioId);
const port = await pickFreePort();
+1
View File
@@ -27,6 +27,7 @@
"test",
"../../test/perf/support/artifact.ts",
"../../test/perf/support/browserMetrics.ts",
"../../test/perf/support/perfProcess.ts",
"../../test/perf/support/serverSampler.ts",
"../../test/perf/support/thresholds.ts"
]
+12 -100
View File
@@ -1,17 +1,22 @@
import { spawn, type ChildProcess } from "node:child_process";
import { spawn } from "node:child_process";
import { once } from "node:events";
import { access, rm } from "node:fs/promises";
import { createServer } from "node:net";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import {
pickFreePort,
waitForServerReady,
stopChildProcess,
cleanupPerfRunDir,
verifyBuiltArtifacts,
parsePerfSeededState,
} from "../test/perf/support/perfProcess";
const repoRoot = fileURLToPath(new URL("../", import.meta.url));
const serverBinPath = resolve(repoRoot, "apps/server/dist/bin.mjs");
const serverClientIndexPath = resolve(repoRoot, "apps/server/dist/client/index.html");
const PERF_PROVIDER_ENV = "T3CODE_PERF_PROVIDER";
const PERF_SCENARIO_ENV = "T3CODE_PERF_SCENARIO";
const PERF_SEED_JSON_START = "__T3_PERF_SEED_JSON_START__";
const PERF_SEED_JSON_END = "__T3_PERF_SEED_JSON_END__";
type PerfSeedScenarioId = "large_threads" | "burst_base";
type PerfProviderScenarioId = "dense_assistant_stream";
@@ -151,47 +156,6 @@ function parseArgs(argv: ReadonlyArray<string>): CliOptions {
};
}
async function pickFreePort(): Promise<number> {
return await new Promise<number>((resolvePort, reject) => {
const server = createServer();
server.on("error", reject);
server.listen(0, "127.0.0.1", () => {
const address = server.address();
if (!address || typeof address === "string") {
reject(new Error("Unable to resolve a free localhost port."));
return;
}
server.close((closeError) => {
if (closeError) {
reject(closeError);
return;
}
resolvePort(address.port);
});
});
});
}
async function verifyBuiltArtifacts(): Promise<void> {
await Promise.all([access(serverBinPath), access(serverClientIndexPath)]).catch(() => {
throw new Error(
`Built perf artifacts are missing. Expected ${serverBinPath} and ${serverClientIndexPath}. Run bun run test:perf:web or build the app first.`,
);
});
}
function parsePerfSeededState(stdout: string): PerfSeededState {
const startIndex = stdout.lastIndexOf(PERF_SEED_JSON_START);
const endIndex = stdout.lastIndexOf(PERF_SEED_JSON_END);
if (startIndex === -1 || endIndex === -1 || endIndex <= startIndex) {
throw new Error(`Perf seed command did not emit the expected JSON markers.\n${stdout}`);
}
const payload = stdout.slice(startIndex + PERF_SEED_JSON_START.length, endIndex).trim();
return JSON.parse(payload) as PerfSeededState;
}
async function seedPerfState(scenarioId: PerfSeedScenarioId): Promise<PerfSeededState> {
const seedProcess = spawn("bun", ["run", "apps/server/scripts/seedPerfState.ts", scenarioId], {
cwd: repoRoot,
@@ -213,59 +177,7 @@ async function seedPerfState(scenarioId: PerfSeedScenarioId): Promise<PerfSeeded
throw new Error(`Perf seed command failed with code ${exitCode ?? "unknown"}.\n${stderr}`);
}
return parsePerfSeededState(stdout);
}
async function waitForServerReady(url: string, process: ChildProcess): Promise<void> {
const startedAt = Date.now();
const timeoutMs = 45_000;
const requestTimeoutMs = 1_000;
while (Date.now() - startedAt < timeoutMs) {
if (process.exitCode !== null) {
throw new Error(`Perf server exited early with code ${process.exitCode}.`);
}
try {
const response = await fetch(url, {
redirect: "manual",
signal: AbortSignal.timeout(requestTimeoutMs),
});
if (response.ok) {
return;
}
} catch {
// Ignore connection races during startup.
}
await new Promise((resolveDelay) => setTimeout(resolveDelay, 200));
}
throw new Error(`Timed out waiting for perf server readiness at ${url}.`);
}
async function stopChildProcess(process: ChildProcess): Promise<void> {
if (process.exitCode !== null) {
return;
}
process.kill("SIGTERM");
const exited = await new Promise<boolean>((resolveExited) => {
const timer = setTimeout(() => resolveExited(false), 5_000);
process.once("exit", () => {
clearTimeout(timer);
resolveExited(true);
});
});
if (!exited && process.exitCode === null) {
process.kill("SIGKILL");
await new Promise<void>((resolveExited) => {
process.once("exit", () => resolveExited());
});
}
}
async function cleanupPerfRunDir(runParentDir: string): Promise<void> {
await rm(runParentDir, { recursive: true, force: true });
return parsePerfSeededState<PerfSeededState>(stdout);
}
function openUrl(url: string): void {
@@ -320,7 +232,7 @@ function printSeedSummary(
async function main(): Promise<void> {
const options = parseArgs(process.argv.slice(2));
await verifyBuiltArtifacts();
await verifyBuiltArtifacts([serverBinPath, serverClientIndexPath]);
const seededState = await seedPerfState(options.scenarioId);
const port = options.port === 0 ? await pickFreePort() : options.port;
+1 -1
View File
@@ -12,5 +12,5 @@
}
]
},
"include": ["**/*.ts"]
"include": ["**/*.ts", "../test/perf/support/perfProcess.ts"]
}
+100
View File
@@ -0,0 +1,100 @@
import { type ChildProcess } from "node:child_process";
import { access, rm } from "node:fs/promises";
import { createServer } from "node:net";
const PERF_SEED_JSON_START = "__T3_PERF_SEED_JSON_START__";
const PERF_SEED_JSON_END = "__T3_PERF_SEED_JSON_END__";
export async function pickFreePort(): Promise<number> {
return await new Promise<number>((resolvePort, reject) => {
const server = createServer();
server.on("error", reject);
server.listen(0, "127.0.0.1", () => {
const address = server.address();
if (!address || typeof address === "string") {
reject(new Error("Unable to resolve a free localhost port."));
return;
}
const { port } = address;
server.close((closeError) => {
if (closeError) {
reject(closeError);
return;
}
resolvePort(port);
});
});
});
}
export async function waitForServerReady(url: string, process: ChildProcess): Promise<void> {
const startedAt = Date.now();
const timeoutMs = 45_000;
const requestTimeoutMs = 1_000;
while (Date.now() - startedAt < timeoutMs) {
if (process.exitCode !== null) {
throw new Error(`Perf server exited early with code ${process.exitCode}.`);
}
try {
const response = await fetch(url, {
redirect: "manual",
signal: AbortSignal.timeout(requestTimeoutMs),
});
if (response.ok) {
return;
}
} catch {
// Ignore connection races while the server is still starting.
}
await new Promise((resolveDelay) => setTimeout(resolveDelay, 200));
}
throw new Error(`Timed out waiting for perf server readiness at ${url}.`);
}
export async function stopChildProcess(process: ChildProcess): Promise<void> {
if (process.exitCode !== null) {
return;
}
process.kill("SIGTERM");
const exited = await new Promise<boolean>((resolveExited) => {
const timer = setTimeout(() => resolveExited(false), 5_000);
process.once("exit", () => {
clearTimeout(timer);
resolveExited(true);
});
});
if (!exited && process.exitCode === null) {
process.kill("SIGKILL");
await new Promise<void>((resolveExited) => {
process.once("exit", () => resolveExited());
});
}
}
export async function cleanupPerfRunDir(runParentDir: string): Promise<void> {
await rm(runParentDir, { recursive: true, force: true });
}
export async function verifyBuiltArtifacts(paths: ReadonlyArray<string>): Promise<void> {
await Promise.all(paths.map((p) => access(p))).catch(() => {
throw new Error(
`Built perf artifacts are missing. Expected ${paths.join(" and ")}. Run bun run test:perf:web or build the app first.`,
);
});
}
export function parsePerfSeededState<T>(stdout: string): T {
const startIndex = stdout.lastIndexOf(PERF_SEED_JSON_START);
const endIndex = stdout.lastIndexOf(PERF_SEED_JSON_END);
if (startIndex !== -1 && endIndex !== -1 && endIndex > startIndex) {
const payload = stdout.slice(startIndex + PERF_SEED_JSON_START.length, endIndex).trim();
return JSON.parse(payload) as T;
}
return JSON.parse(stdout) as T;
}