feat: workspace-concurrency based on cores amount (#3574)

This commit is contained in:
Pablo Sáez
2021-07-05 04:07:55 -04:00
committed by GitHub
parent 5aaf3e3fa1
commit 25f6968d47
4 changed files with 51 additions and 0 deletions

View 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)`

View 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
}

View File

@@ -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 }
}

View 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)
})