Skip to content

Commit 2f7d42c

Browse files
chore: update threadsCount logic to use availableParallelism (#4126)
1 parent c21c0ef commit 2f7d42c

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MessageChannel } from 'node:worker_threads'
2-
import { cpus } from 'node:os'
2+
import * as nodeos from 'node:os'
33
import { pathToFileURL } from 'node:url'
44
import { createBirpc } from 'birpc'
55
import { resolve } from 'pathe'
@@ -39,9 +39,14 @@ function createWorkerChannel(project: WorkspaceProject) {
3939
}
4040

4141
export function createThreadsPool(ctx: Vitest, { execArgv, env }: PoolProcessOptions): ProcessPool {
42+
const numCpus
43+
= typeof nodeos.availableParallelism === 'function'
44+
? nodeos.availableParallelism()
45+
: nodeos.cpus().length
46+
4247
const threadsCount = ctx.config.watch
43-
? Math.max(Math.floor(cpus().length / 2), 1)
44-
: Math.max(cpus().length - 1, 1)
48+
? Math.max(Math.floor(numCpus / 2), 1)
49+
: Math.max(numCpus - 1, 1)
4550

4651
const maxThreads = ctx.config.maxThreads ?? threadsCount
4752
const minThreads = ctx.config.minThreads ?? threadsCount

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MessageChannel } from 'node:worker_threads'
2-
import { cpus } from 'node:os'
2+
import * as nodeos from 'node:os'
33
import { pathToFileURL } from 'node:url'
44
import { createBirpc } from 'birpc'
55
import { resolve } from 'pathe'
@@ -40,9 +40,14 @@ function createWorkerChannel(project: WorkspaceProject) {
4040
}
4141

4242
export function createVmThreadsPool(ctx: Vitest, { execArgv, env }: PoolProcessOptions): ProcessPool {
43+
const numCpus
44+
= typeof nodeos.availableParallelism === 'function'
45+
? nodeos.availableParallelism()
46+
: nodeos.cpus().length
47+
4348
const threadsCount = ctx.config.watch
44-
? Math.max(Math.floor(cpus().length / 2), 1)
45-
: Math.max(cpus().length - 1, 1)
49+
? Math.max(Math.floor(numCpus / 2), 1)
50+
: Math.max(numCpus - 1, 1)
4651

4752
const maxThreads = ctx.config.maxThreads ?? threadsCount
4853
const minThreads = ctx.config.minThreads ?? threadsCount

packages/vitest/src/utils/memory-limit.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
* LICENSE file in the root directory of facebook/jest GitHub project tree.
66
*/
77

8-
import { cpus } from 'node:os'
8+
import * as nodeos from 'node:os'
99
import type { ResolvedConfig } from '../types'
1010

1111
function getDefaultThreadsCount(config: ResolvedConfig) {
12+
const numCpus
13+
= typeof nodeos.availableParallelism === 'function'
14+
? nodeos.availableParallelism()
15+
: nodeos.cpus().length
16+
1217
return config.watch
13-
? Math.max(Math.floor(cpus().length / 2), 1)
14-
: Math.max(cpus().length - 1, 1)
18+
? Math.max(Math.floor(numCpus / 2), 1)
19+
: Math.max(numCpus - 1, 1)
1520
}
1621

1722
export function getWorkerMemoryLimit(config: ResolvedConfig) {

0 commit comments

Comments
 (0)