Skip to content

Commit

Permalink
feat: workspace-concurrency based on cores amount (#3574)
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloSzx authored Jul 5, 2021
1 parent 5aaf3e3 commit 25f6968
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-insects-leave.md
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions packages/config/src/concurrency.ts
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ConfigWithDeprecatedSettings,
UniversalOptions,
} from './Config'
import { getWorkspaceConcurrency } from './concurrency'

export { Config, UniversalOptions }

Expand Down Expand Up @@ -447,6 +448,8 @@ export default async (
}
}

pnpmConfig.workspaceConcurrency = getWorkspaceConcurrency(pnpmConfig.workspaceConcurrency)

return { config: pnpmConfig, warnings }
}

Expand Down
30 changes: 30 additions & 0 deletions packages/config/test/concurrency.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit 25f6968

Please sign in to comment.