Skip to content

Commit 3fb3e80

Browse files
authored
perf: avoid spawning extra workers if no tests will run there (#8446)
1 parent dd6186e commit 3fb3e80

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

packages/vitest/src/node/pool.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ export function createPool(ctx: Vitest): ProcessPool {
175175
const groupedSpecifications: Record<string, TestSpecification[]> = {}
176176
const groups = new Set<number>()
177177

178-
const factories: Record<LocalPool, () => ProcessPool> = {
179-
vmThreads: () => createVmThreadsPool(ctx, options),
180-
vmForks: () => createVmForksPool(ctx, options),
181-
threads: () => createThreadsPool(ctx, options),
182-
forks: () => createForksPool(ctx, options),
178+
const factories: Record<LocalPool, (specs: TestSpecification[]) => ProcessPool> = {
179+
vmThreads: specs => createVmThreadsPool(ctx, options, specs),
180+
vmForks: specs => createVmForksPool(ctx, options, specs),
181+
threads: specs => createThreadsPool(ctx, options, specs),
182+
forks: specs => createForksPool(ctx, options, specs),
183183
typescript: () => createTypecheckPool(ctx),
184184
}
185185

@@ -241,7 +241,7 @@ export function createPool(ctx: Vitest): ProcessPool {
241241

242242
if (pool in factories) {
243243
const factory = factories[pool]
244-
pools[pool] ??= factory()
244+
pools[pool] ??= factory(specs)
245245
return pools[pool]![method](specs, invalidate)
246246
}
247247

packages/vitest/src/node/pools/forks.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ContextRPC, ContextTestEnvironment } from '../../types/worker'
55
import type { Vitest } from '../core'
66
import type { PoolProcessOptions, ProcessPool, RunWithFiles } from '../pool'
77
import type { TestProject } from '../project'
8+
import type { TestSpecification } from '../spec'
89
import type { SerializedConfig } from '../types/config'
910
import EventEmitter from 'node:events'
1011
import * as nodeos from 'node:os'
@@ -65,6 +66,7 @@ function createChildProcessChannel(project: TestProject, collect = false) {
6566
export function createForksPool(
6667
vitest: Vitest,
6768
{ execArgv, env }: PoolProcessOptions,
69+
specifications: TestSpecification[],
6870
): ProcessPool {
6971
const numCpus
7072
= typeof nodeos.availableParallelism === 'function'
@@ -75,12 +77,16 @@ export function createForksPool(
7577
? Math.max(Math.floor(numCpus / 2), 1)
7678
: Math.max(numCpus - 1, 1)
7779

80+
const recommendedCount = vitest.config.watch
81+
? threadsCount
82+
: Math.min(threadsCount, specifications.length)
83+
7884
const poolOptions = vitest.config.poolOptions?.forks ?? {}
7985

8086
const maxThreads
81-
= poolOptions.maxForks ?? vitest.config.maxWorkers ?? threadsCount
87+
= poolOptions.maxForks ?? vitest.config.maxWorkers ?? recommendedCount
8288
const minThreads
83-
= poolOptions.minForks ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads)
89+
= poolOptions.minForks ?? vitest.config.minWorkers ?? Math.min(recommendedCount, maxThreads)
8490

8591
const worker = resolve(vitest.distPath, 'workers/forks.js')
8692

packages/vitest/src/node/pools/threads.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ContextTestEnvironment } from '../../types/worker'
55
import type { Vitest } from '../core'
66
import type { PoolProcessOptions, ProcessPool, RunWithFiles } from '../pool'
77
import type { TestProject } from '../project'
8+
import type { TestSpecification } from '../spec'
89
import type { SerializedConfig } from '../types/config'
910
import type { WorkerContext } from '../types/worker'
1011
import * as nodeos from 'node:os'
@@ -46,6 +47,7 @@ function createWorkerChannel(project: TestProject, collect: boolean) {
4647
export function createThreadsPool(
4748
vitest: Vitest,
4849
{ execArgv, env }: PoolProcessOptions,
50+
specifications: TestSpecification[],
4951
): ProcessPool {
5052
const numCpus
5153
= typeof nodeos.availableParallelism === 'function'
@@ -56,12 +58,16 @@ export function createThreadsPool(
5658
? Math.max(Math.floor(numCpus / 2), 1)
5759
: Math.max(numCpus - 1, 1)
5860

61+
const recommendedCount = vitest.config.watch
62+
? threadsCount
63+
: Math.min(threadsCount, specifications.length)
64+
5965
const poolOptions = vitest.config.poolOptions?.threads ?? {}
6066

6167
const maxThreads
62-
= poolOptions.maxThreads ?? vitest.config.maxWorkers ?? threadsCount
68+
= poolOptions.maxThreads ?? vitest.config.maxWorkers ?? recommendedCount
6369
const minThreads
64-
= poolOptions.minThreads ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads)
70+
= poolOptions.minThreads ?? vitest.config.minWorkers ?? Math.min(recommendedCount, maxThreads)
6571

6672
const worker = resolve(vitest.distPath, 'workers/threads.js')
6773

packages/vitest/src/node/pools/vmForks.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ContextRPC, ContextTestEnvironment } from '../../types/worker'
55
import type { Vitest } from '../core'
66
import type { PoolProcessOptions, ProcessPool, RunWithFiles } from '../pool'
77
import type { TestProject } from '../project'
8+
import type { TestSpecification } from '../spec'
89
import type { ResolvedConfig, SerializedConfig } from '../types/config'
910
import EventEmitter from 'node:events'
1011
import * as nodeos from 'node:os'
@@ -72,6 +73,7 @@ function createChildProcessChannel(project: TestProject, collect: boolean) {
7273
export function createVmForksPool(
7374
vitest: Vitest,
7475
{ execArgv, env }: PoolProcessOptions,
76+
specifications: TestSpecification[],
7577
): ProcessPool {
7678
const numCpus
7779
= typeof nodeos.availableParallelism === 'function'
@@ -82,12 +84,16 @@ export function createVmForksPool(
8284
? Math.max(Math.floor(numCpus / 2), 1)
8385
: Math.max(numCpus - 1, 1)
8486

87+
const recommendedCount = vitest.config.watch
88+
? threadsCount
89+
: Math.min(threadsCount, specifications.length)
90+
8591
const poolOptions = vitest.config.poolOptions?.vmForks ?? {}
8692

8793
const maxThreads
88-
= poolOptions.maxForks ?? vitest.config.maxWorkers ?? threadsCount
94+
= poolOptions.maxForks ?? vitest.config.maxWorkers ?? recommendedCount
8995
const minThreads
90-
= poolOptions.maxForks ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads)
96+
= poolOptions.maxForks ?? vitest.config.minWorkers ?? Math.min(recommendedCount, maxThreads)
9197

9298
const worker = resolve(vitest.distPath, 'workers/vmForks.js')
9399

packages/vitest/src/node/pools/vmThreads.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ContextTestEnvironment } from '../../types/worker'
55
import type { Vitest } from '../core'
66
import type { PoolProcessOptions, ProcessPool, RunWithFiles } from '../pool'
77
import type { TestProject } from '../project'
8+
import type { TestSpecification } from '../spec'
89
import type { ResolvedConfig, SerializedConfig } from '../types/config'
910
import type { WorkerContext } from '../types/worker'
1011
import * as nodeos from 'node:os'
@@ -49,6 +50,7 @@ function createWorkerChannel(project: TestProject, collect: boolean) {
4950
export function createVmThreadsPool(
5051
vitest: Vitest,
5152
{ execArgv, env }: PoolProcessOptions,
53+
specifications: TestSpecification[],
5254
): ProcessPool {
5355
const numCpus
5456
= typeof nodeos.availableParallelism === 'function'
@@ -59,12 +61,16 @@ export function createVmThreadsPool(
5961
? Math.max(Math.floor(numCpus / 2), 1)
6062
: Math.max(numCpus - 1, 1)
6163

64+
const recommendedCount = vitest.config.watch
65+
? threadsCount
66+
: Math.min(threadsCount, specifications.length)
67+
6268
const poolOptions = vitest.config.poolOptions?.vmThreads ?? {}
6369

6470
const maxThreads
65-
= poolOptions.maxThreads ?? vitest.config.maxWorkers ?? threadsCount
71+
= poolOptions.maxThreads ?? vitest.config.maxWorkers ?? recommendedCount
6672
const minThreads
67-
= poolOptions.minThreads ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads)
73+
= poolOptions.minThreads ?? vitest.config.minWorkers ?? Math.min(recommendedCount, maxThreads)
6874

6975
const worker = resolve(vitest.distPath, 'workers/vmThreads.js')
7076

0 commit comments

Comments
 (0)