From 7fd8b6a0ba7b9c17d61237e117cef27447dcdb65 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Fri, 31 Jul 2026 11:39:43 +0300 Subject: [PATCH] Optimize speed --- e2e/global-setup.ts | 77 ++++++++++++++++++++++++++---------------- e2e/global-teardown.ts | 12 +++++-- 2 files changed, 57 insertions(+), 32 deletions(-) diff --git a/e2e/global-setup.ts b/e2e/global-setup.ts index 61264aa8e..0be1878ec 100644 --- a/e2e/global-setup.ts +++ b/e2e/global-setup.ts @@ -66,14 +66,22 @@ async function ensureMinioRunning(): Promise { return true; } -function killProcessOnPort(port: number): void { +/** Kills anything listening on the port range, returning whether something was killed. */ +function killProcessesOnPorts(firstPort: number, lastPort: number): boolean { try { - // Try to find and kill any process on this port (macOS/Linux) - execSync(`lsof -ti :${port} | xargs -r kill -9 2>/dev/null || true`, { + const pids = execSync(`lsof -ti :${firstPort}-${lastPort} || true`, { + stdio: "pipe", + }) + .toString() + .trim(); + if (pids === "") return false; + + execSync(`kill -9 ${pids.split("\n").join(" ")} 2>/dev/null || true`, { stdio: "pipe", }); + return true; } catch { - // Ignore errors - port might already be free + return false; } } @@ -89,7 +97,7 @@ async function waitForServer(port: number, timeout = 120000): Promise { } catch { // Server not ready yet } - await new Promise((resolve) => setTimeout(resolve, 250)); + await new Promise((resolve) => setTimeout(resolve, 100)); } throw new Error(`Server on port ${port} did not start within ${timeout}ms`); } @@ -177,11 +185,14 @@ async function globalSetup(config: FullConfig) { // Kill any existing processes on our ports before starting // biome-ignore lint/suspicious/noConsole: CLI script output console.log("Cleaning up any existing processes on e2e ports..."); - for (let i = 0; i < workerCount; i++) { - killProcessOnPort(E2E_BASE_PORT + i); + const killedSomething = killProcessesOnPorts( + E2E_BASE_PORT, + E2E_BASE_PORT + workerCount - 1, + ); + if (killedSomething) { + // Wait briefly for ports to be released + await new Promise((resolve) => setTimeout(resolve, 500)); } - // Wait briefly for ports to be released - await new Promise((resolve) => setTimeout(resolve, 500)); for (let i = 0; i < workerCount; i++) { const port = E2E_BASE_PORT + i; @@ -192,27 +203,35 @@ async function globalSetup(config: FullConfig) { // Start server // biome-ignore lint/suspicious/noConsole: CLI script output console.log(`Starting server for worker ${i} on port ${port}...`); - const serverProcess = spawn("pnpm", ["start"], { - env: { - ...process.env, - DB_PATH: dbPath, - PORT: String(port), - DISCORD_CLIENT_ID: "123", - DISCORD_CLIENT_SECRET: "secret", - SESSION_SECRET: "secret", - VITE_SITE_DOMAIN: `http://localhost:${port}`, - VITE_E2E_TEST_RUN: "true", - STORAGE_END_POINT: "http://127.0.0.1:9000", - STORAGE_ACCESS_KEY: "minio-user", - STORAGE_SECRET: "minio-password", - STORAGE_REGION: "us-east-1", - STORAGE_BUCKET: "sendou", - // no system messages to a shared skalop instance (see build env above) - SKALOP_SYSTEM_MESSAGE_URL: "", - SKALOP_TOKEN: "", + // react-router-serve directly instead of `pnpm start`: ensureMigratedDb + // above already migrated (the script's `migrate up` step is redundant) and + // the Sentry instrument import only adds request overhead with no DSN set + const serverProcess = spawn( + "node_modules/.bin/react-router-serve", + ["./build/server/index.js"], + { + env: { + ...process.env, + NODE_ENV: "production", + DB_PATH: dbPath, + PORT: String(port), + DISCORD_CLIENT_ID: "123", + DISCORD_CLIENT_SECRET: "secret", + SESSION_SECRET: "secret", + VITE_SITE_DOMAIN: `http://localhost:${port}`, + VITE_E2E_TEST_RUN: "true", + STORAGE_END_POINT: "http://127.0.0.1:9000", + STORAGE_ACCESS_KEY: "minio-user", + STORAGE_SECRET: "minio-password", + STORAGE_REGION: "us-east-1", + STORAGE_BUCKET: "sendou", + // no system messages to a shared skalop instance (see build env above) + SKALOP_SYSTEM_MESSAGE_URL: "", + SKALOP_TOKEN: "", + }, + detached: false, }, - detached: false, - }); + ); SERVER_PROCESSES.push(serverProcess); diff --git a/e2e/global-teardown.ts b/e2e/global-teardown.ts index 10bcb3cf7..8d8bfb25b 100644 --- a/e2e/global-teardown.ts +++ b/e2e/global-teardown.ts @@ -14,14 +14,20 @@ async function globalTeardown(_config: FullConfig) { const servers = global.__E2E_SERVERS__ || []; + const exits: Promise[] = []; for (const server of servers) { - if (server && !server.killed) { + if (server && !server.killed && server.exitCode === null) { + exits.push( + new Promise((resolve) => server.once("exit", () => resolve())), + ); server.kill("SIGTERM"); } } - // Give processes a moment to clean up - await new Promise((resolve) => setTimeout(resolve, 1000)); + await Promise.race([ + Promise.all(exits), + new Promise((resolve) => setTimeout(resolve, 2000)), + ]); // Stop MinIO if we started it (check for marker file) if (fs.existsSync(MINIO_MARKER_FILE)) {