feat(test): use host-aware local full-suite defaults (#65264)

* feat(test): use host-aware local full-suite defaults

* fix(test): remove undefined local profile host info
This commit is contained in:
Nimrod Gutman
2026-04-12 12:46:20 +03:00
committed by GitHub
parent 913d23c877
commit c247e36664
7 changed files with 334 additions and 112 deletions

View File

@@ -1,7 +1,12 @@
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { pluginSdkSubpaths } from "../../scripts/lib/plugin-sdk-entries.mjs";
import {
detectVitestHostInfo as detectVitestHostInfoImpl,
isCiLikeEnv,
resolveLocalVitestMaxWorkers as resolveLocalVitestMaxWorkersImpl,
resolveLocalVitestScheduling as resolveLocalVitestSchedulingImpl,
} from "../../scripts/lib/vitest-local-scheduling.mjs";
import {
BUNDLED_PLUGIN_ROOT_DIR,
BUNDLED_PLUGIN_TEST_GLOB,
@@ -9,18 +14,6 @@ import {
import { loadVitestExperimentalConfig } from "./vitest.performance-config.ts";
import { shouldPrintVitestThrottle } from "./vitest.system-load.ts";
const clamp = (value: number, min: number, max: number) => Math.max(min, Math.min(max, value));
function parsePositiveInt(value: string | undefined): number | null {
const parsed = Number.parseInt(value ?? "", 10);
return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
}
function isSystemThrottleDisabled(env: Record<string, string | undefined>): boolean {
const normalized = env.OPENCLAW_VITEST_DISABLE_SYSTEM_THROTTLE?.trim().toLowerCase();
return normalized === "1" || normalized === "true";
}
type VitestHostInfo = {
cpuCount?: number;
loadAverage1m?: number;
@@ -45,12 +38,7 @@ export const jsdomOptimizedDeps = {
};
function detectVitestHostInfo(): Required<VitestHostInfo> {
return {
cpuCount:
typeof os.availableParallelism === "function" ? os.availableParallelism() : os.cpus().length,
loadAverage1m: os.loadavg()[0] ?? 0,
totalMemoryBytes: os.totalmem(),
};
return detectVitestHostInfoImpl() as Required<VitestHostInfo>;
}
export function resolveLocalVitestMaxWorkers(
@@ -58,7 +46,7 @@ export function resolveLocalVitestMaxWorkers(
system: VitestHostInfo = detectVitestHostInfo(),
pool: OpenClawVitestPool = resolveDefaultVitestPool(env),
): number {
return resolveLocalVitestScheduling(env, system, pool).maxWorkers;
return resolveLocalVitestMaxWorkersImpl(env, system, pool);
}
export function resolveLocalVitestScheduling(
@@ -66,91 +54,7 @@ export function resolveLocalVitestScheduling(
system: VitestHostInfo = detectVitestHostInfo(),
pool: OpenClawVitestPool = resolveDefaultVitestPool(env),
): LocalVitestScheduling {
const override = parsePositiveInt(env.OPENCLAW_VITEST_MAX_WORKERS ?? env.OPENCLAW_TEST_WORKERS);
if (override !== null) {
const maxWorkers = clamp(override, 1, 16);
return {
maxWorkers,
fileParallelism: maxWorkers > 1,
throttledBySystem: false,
};
}
const cpuCount = Math.max(1, system.cpuCount ?? 1);
const loadAverage1m = Math.max(0, system.loadAverage1m ?? 0);
const totalMemoryGb = (system.totalMemoryBytes ?? 0) / 1024 ** 3;
// Keep smaller hosts conservative, but let large local boxes actually use
// their cores. Thread workers scale much better than the old fork-first cap.
let inferred =
cpuCount <= 2
? 1
: cpuCount <= 4
? 2
: cpuCount <= 8
? 4
: Math.max(1, Math.floor(cpuCount * 0.75));
if (totalMemoryGb <= 16) {
inferred = Math.min(inferred, 2);
} else if (totalMemoryGb <= 32) {
inferred = Math.min(inferred, 4);
} else if (totalMemoryGb <= 64) {
inferred = Math.min(inferred, 6);
} else if (totalMemoryGb <= 128) {
inferred = Math.min(inferred, 8);
} else if (totalMemoryGb <= 256) {
inferred = Math.min(inferred, 12);
} else {
inferred = Math.min(inferred, 16);
}
const loadRatio = loadAverage1m > 0 ? loadAverage1m / cpuCount : 0;
if (loadRatio >= 1) {
inferred = Math.max(1, Math.floor(inferred / 2));
} else if (loadRatio >= 0.75) {
inferred = Math.max(1, inferred - 2);
} else if (loadRatio >= 0.5) {
inferred = Math.max(1, inferred - 1);
}
if (pool === "forks") {
inferred = Math.min(inferred, 8);
}
inferred = clamp(inferred, 1, 16);
if (isSystemThrottleDisabled(env)) {
return {
maxWorkers: inferred,
fileParallelism: true,
throttledBySystem: false,
};
}
if (loadRatio >= 1) {
const maxWorkers = Math.max(1, Math.floor(inferred / 2));
return {
maxWorkers,
fileParallelism: maxWorkers > 1,
throttledBySystem: maxWorkers < inferred,
};
}
if (loadRatio >= 0.75) {
const maxWorkers = Math.max(2, Math.ceil(inferred * 0.75));
return {
maxWorkers,
fileParallelism: true,
throttledBySystem: maxWorkers < inferred,
};
}
return {
maxWorkers: inferred,
fileParallelism: true,
throttledBySystem: false,
};
return resolveLocalVitestSchedulingImpl(env, system, pool) as LocalVitestScheduling;
}
export function resolveDefaultVitestPool(
@@ -164,7 +68,7 @@ export const nonIsolatedRunnerPath = path.join(repoRoot, "test", "non-isolated-r
export function resolveRepoRootPath(value: string): string {
return path.isAbsolute(value) ? value : path.join(repoRoot, value);
}
const isCI = process.env.CI === "true" || process.env.GITHUB_ACTIONS === "true";
const isCI = isCiLikeEnv(process.env);
const isWindows = process.platform === "win32";
const defaultPool = resolveDefaultVitestPool();
const localScheduling = resolveLocalVitestScheduling(