Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion stryker.shared.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { packageStrykerConfig } from "./stryker.shared";
import { totalmem } from "node:os";
import {
RUNNER_HEAP_SHARE_OF_MEMORY,
packageStrykerConfig,
runnerHeapLimitMiB,
} from "./stryker.shared";

describe("packageStrykerConfig", () => {
afterEach(() => {
Expand Down Expand Up @@ -53,3 +58,33 @@ describe("packageStrykerConfig", () => {
expect(packageStrykerConfig().reporters).toContain("json");
});
});

describe("runnerHeapLimitMiB", () => {
const MIB = 1024 * 1024;

it("gives a single worker its share of the machine's memory", () => {
expect(runnerHeapLimitMiB(16 * 1024 * MIB, 1)).toBe(
Math.floor(16 * 1024 * RUNNER_HEAP_SHARE_OF_MEMORY),
);
});

it("divides the share between concurrent workers, so all of them together stay inside it", () => {
expect(runnerHeapLimitMiB(16 * 1024 * MIB, 4)).toBe(
Math.floor(runnerHeapLimitMiB(16 * 1024 * MIB, 1) / 4),
);
});

it("never reserves more than the machine has", () => {
expect(RUNNER_HEAP_SHARE_OF_MEMORY).toBeGreaterThan(0);
expect(RUNNER_HEAP_SHARE_OF_MEMORY).toBeLessThan(1);
});
});

describe("packageStrykerConfig runner heap bound", () => {
it("caps the heap of every mutant test runner process, so a mutant that grows without bound is stopped inside the runner rather than by the machine", () => {
const args = packageStrykerConfig({ concurrency: 2 }).testRunnerNodeArgs;
expect(args).toContain(
`--max-old-space-size=${String(runnerHeapLimitMiB(totalmem(), 2))}`,
);
});
});
26 changes: 25 additions & 1 deletion stryker.shared.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { totalmem } from "node:os";
import { fileURLToPath } from "node:url";
import type { PartialStrykerOptions } from "@stryker-mutator/api/core";

Expand Down Expand Up @@ -30,6 +31,25 @@ const RUNNER_PRELOAD = fileURLToPath(
new URL("./stryker.runner-preload.ts", import.meta.url),
);

/** The share of the machine's memory that all of a package's mutant test runner processes together may hold as V8 heap. The rest is left for Stryker's own process, the TypeScript checker, vitest's workers and the operating system (and, on a CI runner, the runner agent, whose loss of contact with the server ends the job), so a mutant that grows memory without bound has to be stopped by the heap limit before it takes the machine. */
export const RUNNER_HEAP_SHARE_OF_MEMORY = 1 / 2;

const BYTES_PER_MIB = 1024 * 1024;

/**
* The `--max-old-space-size` value, in MiB, for each of `workers` concurrent mutant test runner processes on a machine with `totalBytes` of memory: the runners' share of memory (RUNNER_HEAP_SHARE_OF_MEMORY) divided evenly between them.
*
* Why a heap limit at all: a mutant can turn a bounded loop into one that never ends or that allocates without bound, and Stryker's `timeoutMS` stops it only by elapsed time, which says nothing about how much memory it takes in that time. On a hosted runner that is the difference between a mutant classified as timed out and a job killed with "The runner has received a shutdown signal" and no report for its slice, which the workflow's gate then reports as a failed package. Under the limit the runner process dies of its own out-of-memory error, which Stryker restarts and records against that mutant, and every other mutant of the slice still reports.
*/
export function runnerHeapLimitMiB(
totalBytes: number,
workers: number,
): number {
return Math.floor(
((totalBytes / BYTES_PER_MIB) * RUNNER_HEAP_SHARE_OF_MEMORY) / workers,
);
}

/**
* Test code, which is never mutated whatever a package's own `mutate` globs include: the test files themselves, and every `test-support/` directory under `src/`, which holds the fixtures and helpers a suite split across several files lifts out of them. A mutant in test code asks whether the suite notices a change to its own scaffolding, not whether it notices a change to the code that ships, and test-support ships no more than a test file does: every package's tsdown entry list leaves it out, and eslint.shared.ts already treats it as test code. Mutating it made the score depend on whether a suite's fixtures happened to live inline in one test file or in a module beside it. Appended after a package's own globs, so a package's include pattern cannot accidentally re-admit it.
*/
Expand Down Expand Up @@ -107,7 +127,11 @@ export function packageStrykerConfig(
// dist/coverage/.turbo are build/tooling output Stryker would otherwise copy into every mutant's own sandbox for nothing — none of it is ever read by a test.
ignorePatterns: ["dist", "coverage", ".turbo"],
// stryker.runner-preload.ts, resolved to an absolute path because each runner child process runs from its own sandbox directory. It drops vitest's github-actions reporter from these processes; the file itself explains why.
testRunnerNodeArgs: ["--import", RUNNER_PRELOAD],
testRunnerNodeArgs: [
"--import",
RUNNER_PRELOAD,
`--max-old-space-size=${String(runnerHeapLimitMiB(totalmem(), concurrency))}`,
],
// json is what the mutation workflow merges across a package's slices, so the package-level score is computed from every file rather than from one slice.
reporters: ["progress", "clear-text", "html", "json"],
tempDirName: ".stryker-tmp",
Expand Down
Loading