diff --git a/src/infra/vitest-config.test.ts b/src/infra/vitest-config.test.ts index 883144316fc..22f3e3ebae5 100644 --- a/src/infra/vitest-config.test.ts +++ b/src/infra/vitest-config.test.ts @@ -107,7 +107,7 @@ describe("resolveLocalVitestMaxWorkers", () => { "threads", idleVitestStats, ), - ).toBe(1); + ).toBe(2); }); it("caps very large hosts at six local workers", () => { @@ -127,7 +127,7 @@ describe("resolveLocalVitestMaxWorkers", () => { }); describe("resolveLocalVitestScheduling", () => { - it("falls back to serial when other Vitest workers are already active", () => { + it("scales back to half capacity when other Vitest work is already consuming most cores", () => { expect( resolveLocalVitestScheduling( {}, @@ -138,19 +138,19 @@ describe("resolveLocalVitestScheduling", () => { }, "threads", { - otherVitestRootCount: 1, - otherVitestWorkerCount: 3, - otherVitestCpuPercent: 120, + otherVitestRootCount: 2, + otherVitestWorkerCount: 12, + otherVitestCpuPercent: 1200, }, ), ).toEqual({ - maxWorkers: 1, - fileParallelism: false, + maxWorkers: 4, + fileParallelism: true, throttledBySystem: true, }); }); - it("caps moderate contention to two workers", () => { + it("keeps big hosts parallel under moderate contention", () => { expect( resolveLocalVitestScheduling( {}, @@ -162,12 +162,12 @@ describe("resolveLocalVitestScheduling", () => { "threads", { otherVitestRootCount: 1, - otherVitestWorkerCount: 0, - otherVitestCpuPercent: 10, + otherVitestWorkerCount: 7, + otherVitestCpuPercent: 700, }, ), ).toEqual({ - maxWorkers: 2, + maxWorkers: 6, fileParallelism: true, throttledBySystem: true, }); diff --git a/vitest.shared.config.ts b/vitest.shared.config.ts index d387b9bb2f9..cc02c2ba161 100644 --- a/vitest.shared.config.ts +++ b/vitest.shared.config.ts @@ -134,28 +134,27 @@ export function resolveLocalVitestScheduling( }; } + const totalCpuPercentCapacity = Math.max(100, cpuCount * 100); + const otherVitestCpuRatio = processStats.otherVitestCpuPercent / totalCpuPercentCapacity; + const otherVitestWorkerRatio = processStats.otherVitestWorkerCount / cpuCount; + const highSystemContention = - loadRatio >= 1 || - processStats.otherVitestWorkerCount >= 2 || - processStats.otherVitestCpuPercent >= 150 || - processStats.otherVitestRootCount >= 2; + loadRatio >= 1 || otherVitestWorkerRatio >= 0.75 || otherVitestCpuRatio >= 0.75; if (highSystemContention) { + const maxWorkers = Math.max(1, Math.floor(inferred / 2)); return { - maxWorkers: 1, - fileParallelism: false, - throttledBySystem: true, + maxWorkers, + fileParallelism: maxWorkers > 1, + throttledBySystem: maxWorkers < inferred, }; } const moderateSystemContention = - loadRatio >= 0.75 || - processStats.otherVitestWorkerCount >= 1 || - processStats.otherVitestCpuPercent >= 75 || - processStats.otherVitestRootCount >= 1; + loadRatio >= 0.75 || otherVitestWorkerRatio >= 0.4 || otherVitestCpuRatio >= 0.4; if (moderateSystemContention) { - const maxWorkers = Math.min(inferred, 2); + const maxWorkers = Math.max(2, Math.ceil(inferred * 0.75)); return { maxWorkers, fileParallelism: true,