test: make vitest worker caps deterministic

This commit is contained in:
Peter Steinberger 2026-04-05 13:29:28 +01:00
parent c74b222ec1
commit 987f4bba80
No known key found for this signature in database
2 changed files with 10 additions and 50 deletions

View File

@ -7,12 +7,6 @@ import baseConfig, {
} from "../../vitest.config.ts";
import { parseVitestProcessStats } from "../../vitest.system-load.ts";
const idleVitestStats = {
otherVitestRootCount: 0,
otherVitestWorkerCount: 0,
otherVitestCpuPercent: 0,
} as const;
describe("resolveLocalVitestMaxWorkers", () => {
it("uses a moderate local worker cap on larger hosts", () => {
expect(
@ -26,7 +20,6 @@ describe("resolveLocalVitestMaxWorkers", () => {
totalMemoryBytes: 64 * 1024 ** 3,
},
"threads",
idleVitestStats,
),
).toBe(6);
});
@ -43,7 +36,6 @@ describe("resolveLocalVitestMaxWorkers", () => {
totalMemoryBytes: 128 * 1024 ** 3,
},
"threads",
idleVitestStats,
),
).toBe(2);
});
@ -60,7 +52,6 @@ describe("resolveLocalVitestMaxWorkers", () => {
totalMemoryBytes: 128 * 1024 ** 3,
},
"threads",
idleVitestStats,
),
).toBe(3);
});
@ -75,7 +66,6 @@ describe("resolveLocalVitestMaxWorkers", () => {
totalMemoryBytes: 16 * 1024 ** 3,
},
"threads",
idleVitestStats,
),
).toBe(2);
});
@ -90,7 +80,6 @@ describe("resolveLocalVitestMaxWorkers", () => {
totalMemoryBytes: 128 * 1024 ** 3,
},
"threads",
idleVitestStats,
),
).toBe(8);
});
@ -105,7 +94,6 @@ describe("resolveLocalVitestMaxWorkers", () => {
totalMemoryBytes: 128 * 1024 ** 3,
},
"threads",
idleVitestStats,
),
).toBe(2);
});
@ -120,54 +108,43 @@ describe("resolveLocalVitestMaxWorkers", () => {
totalMemoryBytes: 256 * 1024 ** 3,
},
"threads",
idleVitestStats,
),
).toBe(12);
});
});
describe("resolveLocalVitestScheduling", () => {
it("scales back to half capacity when other Vitest work is already consuming most cores", () => {
it("scales back to half capacity when the host load is already saturated", () => {
expect(
resolveLocalVitestScheduling(
{},
{
cpuCount: 16,
loadAverage1m: 0.5,
loadAverage1m: 16,
totalMemoryBytes: 128 * 1024 ** 3,
},
"threads",
{
otherVitestRootCount: 2,
otherVitestWorkerCount: 12,
otherVitestCpuPercent: 1200,
},
),
).toEqual({
maxWorkers: 4,
maxWorkers: 2,
fileParallelism: true,
throttledBySystem: true,
});
});
it("keeps big hosts parallel under moderate contention", () => {
it("keeps big hosts parallel under moderate host contention", () => {
expect(
resolveLocalVitestScheduling(
{},
{
cpuCount: 16,
loadAverage1m: 0.5,
loadAverage1m: 12,
totalMemoryBytes: 128 * 1024 ** 3,
},
"threads",
{
otherVitestRootCount: 1,
otherVitestWorkerCount: 7,
otherVitestCpuPercent: 700,
},
),
).toEqual({
maxWorkers: 6,
maxWorkers: 5,
fileParallelism: true,
throttledBySystem: true,
});
@ -185,7 +162,6 @@ describe("resolveLocalVitestScheduling", () => {
totalMemoryBytes: 128 * 1024 ** 3,
},
"threads",
idleVitestStats,
),
).toEqual({
maxWorkers: 8,

View File

@ -7,11 +7,7 @@ import {
BUNDLED_PLUGIN_TEST_GLOB,
} from "./vitest.bundled-plugin-paths.ts";
import { loadVitestExperimentalConfig } from "./vitest.performance-config.ts";
import {
detectVitestProcessStats,
shouldPrintVitestThrottle,
type VitestProcessStats,
} from "./vitest.system-load.ts";
import { shouldPrintVitestThrottle } from "./vitest.system-load.ts";
const clamp = (value: number, min: number, max: number) => Math.max(min, Math.min(max, value));
@ -61,16 +57,14 @@ export function resolveLocalVitestMaxWorkers(
env: Record<string, string | undefined> = process.env,
system: VitestHostInfo = detectVitestHostInfo(),
pool: OpenClawVitestPool = resolveDefaultVitestPool(env),
processStats: VitestProcessStats = detectVitestProcessStats(env),
): number {
return resolveLocalVitestScheduling(env, system, pool, processStats).maxWorkers;
return resolveLocalVitestScheduling(env, system, pool).maxWorkers;
}
export function resolveLocalVitestScheduling(
env: Record<string, string | undefined> = process.env,
system: VitestHostInfo = detectVitestHostInfo(),
pool: OpenClawVitestPool = resolveDefaultVitestPool(env),
processStats: VitestProcessStats = detectVitestProcessStats(env),
): LocalVitestScheduling {
const override = parsePositiveInt(env.OPENCLAW_VITEST_MAX_WORKERS ?? env.OPENCLAW_TEST_WORKERS);
if (override !== null) {
@ -134,14 +128,7 @@ export function resolveLocalVitestScheduling(
};
}
const totalCpuPercentCapacity = Math.max(100, cpuCount * 100);
const otherVitestCpuRatio = processStats.otherVitestCpuPercent / totalCpuPercentCapacity;
const otherVitestWorkerRatio = processStats.otherVitestWorkerCount / cpuCount;
const highSystemContention =
loadRatio >= 1 || otherVitestWorkerRatio >= 0.75 || otherVitestCpuRatio >= 0.75;
if (highSystemContention) {
if (loadRatio >= 1) {
const maxWorkers = Math.max(1, Math.floor(inferred / 2));
return {
maxWorkers,
@ -150,10 +137,7 @@ export function resolveLocalVitestScheduling(
};
}
const moderateSystemContention =
loadRatio >= 0.75 || otherVitestWorkerRatio >= 0.4 || otherVitestCpuRatio >= 0.4;
if (moderateSystemContention) {
if (loadRatio >= 0.75) {
const maxWorkers = Math.max(2, Math.ceil(inferred * 0.75));
return {
maxWorkers,