mirror of
https://github.com/pnpm/pnpm.git
synced 2025-12-29 18:18:11 -05:00
feat: workspace-concurrency based on cores amount (#3574)
This commit is contained in:
5
.changeset/mean-insects-leave.md
Normal file
5
.changeset/mean-insects-leave.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@pnpm/config": minor
|
||||
---
|
||||
|
||||
Add `workspace-concurrency` based on CPU cores amount, just set `workspace-concurrency` as zero or negative, the concurrency limit is set as `max((amount of cores) - abs(workspace-concurrency), 1)`
|
||||
13
packages/config/src/concurrency.ts
Normal file
13
packages/config/src/concurrency.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { cpus } from 'os'
|
||||
|
||||
export function getWorkspaceConcurrency (option: number | undefined): number {
|
||||
if (typeof option !== 'number') return 4
|
||||
|
||||
if (option <= 0) {
|
||||
// If option is <= 0, it uses the amount of cores minus the absolute of the number given
|
||||
// but always returning at least 1
|
||||
return Math.max(1, cpus().length - Math.abs(option))
|
||||
}
|
||||
|
||||
return option
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
ConfigWithDeprecatedSettings,
|
||||
UniversalOptions,
|
||||
} from './Config'
|
||||
import { getWorkspaceConcurrency } from './concurrency'
|
||||
|
||||
export { Config, UniversalOptions }
|
||||
|
||||
@@ -447,6 +448,8 @@ export default async (
|
||||
}
|
||||
}
|
||||
|
||||
pnpmConfig.workspaceConcurrency = getWorkspaceConcurrency(pnpmConfig.workspaceConcurrency)
|
||||
|
||||
return { config: pnpmConfig, warnings }
|
||||
}
|
||||
|
||||
|
||||
30
packages/config/test/concurrency.test.ts
Normal file
30
packages/config/test/concurrency.test.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { cpus } from 'os'
|
||||
import { getWorkspaceConcurrency } from '../lib/concurrency'
|
||||
|
||||
const hostCores = cpus().length
|
||||
|
||||
test('default workspace concurrency', () => {
|
||||
const n = getWorkspaceConcurrency(undefined)
|
||||
|
||||
expect(n).toBe(4)
|
||||
})
|
||||
|
||||
test('get back positive amount', () => {
|
||||
expect(getWorkspaceConcurrency(5)).toBe(5)
|
||||
})
|
||||
|
||||
test('match host cores amount', () => {
|
||||
const n = getWorkspaceConcurrency(0)
|
||||
|
||||
expect(n).toBe(hostCores)
|
||||
})
|
||||
|
||||
test('host cores minus X', () => {
|
||||
const n1 = getWorkspaceConcurrency(-1)
|
||||
|
||||
expect(n1).toBe(Math.max(1, hostCores - 1))
|
||||
|
||||
const n2 = getWorkspaceConcurrency(-9999)
|
||||
|
||||
expect(n2).toBe(1)
|
||||
})
|
||||
Reference in New Issue
Block a user