























11// Vitest system load helper probes host load before expensive test lanes.
2-import { spawnSync } from "node:child_process";
3243type EnvMap = Record<string, string | undefined>;
54@@ -9,30 +8,8 @@ export type VitestProcessStats = {
98otherVitestCpuPercent: number;
109};
111012-type PsResult = {
13-status: number | null;
14-stdout: string;
15-};
16-17-type DetectVitestProcessStatsOptions = {
18-platform?: NodeJS.Platform;
19-selfPid?: number;
20-runPs?: () => PsResult;
21-};
22-23-const EMPTY_VITEST_PROCESS_STATS: VitestProcessStats = {
24-otherVitestRootCount: 0,
25-otherVitestWorkerCount: 0,
26-otherVitestCpuPercent: 0,
27-};
28-2911const BOOLEAN_TRUE_VALUES = new Set(["1", "true"]);
301231-function isExplicitlyEnabled(value: string | undefined): boolean {
32-const normalized = value?.trim().toLowerCase();
33-return normalized ? BOOLEAN_TRUE_VALUES.has(normalized) : false;
34-}
35-3613function isVitestWorkerArgs(args: string): boolean {
3714return args.includes("/vitest/dist/workers/") || args.includes("\\vitest\\dist\\workers\\");
3815}
@@ -55,7 +32,11 @@ export function parseVitestProcessStats(
5532psOutput: string,
5633selfPid: number = process.pid,
5734): VitestProcessStats {
58-const stats = { ...EMPTY_VITEST_PROCESS_STATS };
35+const stats: VitestProcessStats = {
36+otherVitestRootCount: 0,
37+otherVitestWorkerCount: 0,
38+otherVitestCpuPercent: 0,
39+};
59406041for (const line of psOutput.split("\n")) {
6142const trimmed = line.trim();
@@ -90,33 +71,6 @@ export function parseVitestProcessStats(
9071return stats;
9172}
927393-export function detectVitestProcessStats(
94-env: EnvMap = process.env,
95-options: DetectVitestProcessStatsOptions = {},
96-): VitestProcessStats {
97-const platform = options.platform ?? process.platform;
98-if (platform === "win32") {
99-return { ...EMPTY_VITEST_PROCESS_STATS };
100-}
101-102-if (isExplicitlyEnabled(env.OPENCLAW_VITEST_DISABLE_SYSTEM_THROTTLE)) {
103-return { ...EMPTY_VITEST_PROCESS_STATS };
104-}
105-106-const result =
107-options.runPs?.() ??
108-spawnSync("ps", ["-xao", "pid=,pcpu=,args="], {
109-encoding: "utf8",
110-stdio: ["ignore", "pipe", "ignore"],
111-});
112-113-if (result.status === 0 && typeof result.stdout === "string" && result.stdout.length > 0) {
114-return parseVitestProcessStats(result.stdout, options.selfPid ?? process.pid);
115-}
116-117-return { ...EMPTY_VITEST_PROCESS_STATS };
118-}
119-12074export function shouldPrintVitestThrottle(env: EnvMap = process.env): boolean {
12175const normalized = env.OPENCLAW_VITEST_PRINT_SYSTEM_THROTTLE?.trim().toLowerCase();
12276return normalized ? BOOLEAN_TRUE_VALUES.has(normalized) : false;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。