|
| 1 | +import { spawn } from "child_process"; |
| 2 | +import { once } from "events"; |
| 3 | +import { |
| 4 | + mkdtempSync, |
| 5 | + readdirSync, |
| 6 | + readFileSync, |
| 7 | + rmSync, |
| 8 | + writeFileSync, |
| 9 | +} from "fs"; |
| 10 | +import { createRequire } from "module"; |
| 11 | +import { tmpdir } from "os"; |
| 12 | +import { dirname, join } from "path"; |
| 13 | +import { fileURLToPath } from "url"; |
| 14 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 15 | + |
| 16 | +const FIXTURE_DIR = fileURLToPath( |
| 17 | + new URL("../../tests/fixtures/isolation", import.meta.url), |
| 18 | +); |
| 19 | +const VITEST_BIN = join( |
| 20 | + dirname(createRequire(import.meta.url).resolve("vitest/package.json")), |
| 21 | + "vitest.mjs", |
| 22 | +); |
| 23 | +const PROCESS_ISOLATION_ENV = "CODSPEED_PROCESS_ISOLATION"; |
| 24 | +const WALLTIME_FORKS_ENV = "CODSPEED_WALLTIME_FORKS"; |
| 25 | +const DEFAULT_FORKS = 3; |
| 26 | +/** The fixture's `iterations`: every fork measures at least this many rounds. */ |
| 27 | +const MIN_ROUNDS_PER_FORK = 4; |
| 28 | +const RUN_TIMEOUT_MS = 180_000; |
| 29 | +const RAN_LINE = /^ran (\d+) (.+)$/; |
| 30 | + |
| 31 | +let tmpDir: string; |
| 32 | + |
| 33 | +beforeEach(() => { |
| 34 | + tmpDir = mkdtempSync(join(tmpdir(), "codspeed-isolation-")); |
| 35 | +}); |
| 36 | + |
| 37 | +afterEach(() => { |
| 38 | + rmSync(tmpDir, { recursive: true, force: true }); |
| 39 | +}); |
| 40 | + |
| 41 | +interface FixtureOptions { |
| 42 | + isolation?: string; |
| 43 | + forksEnv?: number; |
| 44 | + pluginForks?: number; |
| 45 | + firstForks?: number; |
| 46 | + failing?: boolean; |
| 47 | +} |
| 48 | + |
| 49 | +async function runFixture(options: FixtureOptions = {}) { |
| 50 | + const logFile = join(tmpDir, "ran.log"); |
| 51 | + writeFileSync(logFile, ""); |
| 52 | + const profileFolder = join(tmpDir, "profile"); |
| 53 | + const env: NodeJS.ProcessEnv = { |
| 54 | + ...process.env, |
| 55 | + CODSPEED_ENV: "true", |
| 56 | + CODSPEED_RUNNER_MODE: "walltime", |
| 57 | + CODSPEED_PROFILE_FOLDER: profileFolder, |
| 58 | + ISOLATION_LOG_FILE: logFile, |
| 59 | + [PROCESS_ISOLATION_ENV]: options.isolation, |
| 60 | + [WALLTIME_FORKS_ENV]: options.forksEnv?.toString(), |
| 61 | + ISOLATION_FIXTURE_PLUGIN_FORKS: options.pluginForks?.toString(), |
| 62 | + ISOLATION_FIXTURE_FIRST_FORKS: options.firstForks?.toString(), |
| 63 | + ISOLATION_FIXTURE_FAIL: options.failing ? "1" : undefined, |
| 64 | + }; |
| 65 | + |
| 66 | + // Not spawnSync: blocking this worker times out its calls to Vitest. |
| 67 | + const child = spawn(process.execPath, [VITEST_BIN, "bench", "--run"], { |
| 68 | + cwd: FIXTURE_DIR, |
| 69 | + env, |
| 70 | + timeout: RUN_TIMEOUT_MS, |
| 71 | + stdio: ["ignore", "ignore", "pipe"], |
| 72 | + }); |
| 73 | + let stderr = ""; |
| 74 | + child.stderr?.on("data", (chunk) => (stderr += chunk)); |
| 75 | + const [status] = (await once(child, "close")) as [number | null]; |
| 76 | + |
| 77 | + const pidsByName = new Map<string, Set<number>>(); |
| 78 | + for (const line of readFileSync(logFile, "utf8").split("\n")) { |
| 79 | + const match = RAN_LINE.exec(line); |
| 80 | + if (!match) continue; |
| 81 | + const pids = pidsByName.get(match[2]) ?? new Set<number>(); |
| 82 | + pids.add(Number(match[1])); |
| 83 | + pidsByName.set(match[2], pids); |
| 84 | + } |
| 85 | + |
| 86 | + const roundsByName = new Map<string, number>(); |
| 87 | + const resultsDir = join(profileFolder, "results"); |
| 88 | + for (const file of status === 0 ? readdirSync(resultsDir) : []) { |
| 89 | + const data = JSON.parse(readFileSync(join(resultsDir, file), "utf8")); |
| 90 | + for (const benchmark of data.benchmarks) { |
| 91 | + roundsByName.set(benchmark.name, benchmark.stats.rounds); |
| 92 | + } |
| 93 | + } |
| 94 | + return { status, stderr, pidsByName, roundsByName }; |
| 95 | +} |
| 96 | + |
| 97 | +describe("walltime forks", () => { |
| 98 | + it( |
| 99 | + "runs every benchmark in 3 forks by default and pools their rounds", |
| 100 | + async () => { |
| 101 | + const run = await runFixture(); |
| 102 | + |
| 103 | + expect(run.status, run.stderr).toBe(0); |
| 104 | + for (const name of ["first", "second", "third"]) { |
| 105 | + expect(run.pidsByName.get(name)?.size).toBe(DEFAULT_FORKS); |
| 106 | + expect(run.roundsByName.get(name)).toBeGreaterThanOrEqual( |
| 107 | + DEFAULT_FORKS * MIN_ROUNDS_PER_FORK, |
| 108 | + ); |
| 109 | + } |
| 110 | + }, |
| 111 | + RUN_TIMEOUT_MS, |
| 112 | + ); |
| 113 | + |
| 114 | + it( |
| 115 | + "takes the benchmark's option over the plugin's", |
| 116 | + async () => { |
| 117 | + const run = await runFixture({ firstForks: 2, pluginForks: 1 }); |
| 118 | + |
| 119 | + expect(run.status, run.stderr).toBe(0); |
| 120 | + expect(run.pidsByName.get("first")?.size).toBe(2); |
| 121 | + expect(run.pidsByName.get("second")?.size).toBe(1); |
| 122 | + }, |
| 123 | + RUN_TIMEOUT_MS, |
| 124 | + ); |
| 125 | + |
| 126 | + it( |
| 127 | + "runs every benchmark in one process when isolation is disabled", |
| 128 | + async () => { |
| 129 | + const run = await runFixture({ isolation: "false" }); |
| 130 | + |
| 131 | + expect(run.status, run.stderr).toBe(0); |
| 132 | + const pids = new Set( |
| 133 | + [...run.pidsByName.values()].flatMap((set) => [...set]), |
| 134 | + ); |
| 135 | + expect(pids.size).toBe(1); |
| 136 | + }, |
| 137 | + RUN_TIMEOUT_MS, |
| 138 | + ); |
| 139 | + |
| 140 | + it( |
| 141 | + "fails the run when a benchmark throws in its fork", |
| 142 | + async () => { |
| 143 | + const run = await runFixture({ failing: true, forksEnv: 1 }); |
| 144 | + |
| 145 | + expect(run.status).not.toBe(0); |
| 146 | + }, |
| 147 | + RUN_TIMEOUT_MS, |
| 148 | + ); |
| 149 | +}); |
0 commit comments