-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow configuring the number of threads (#173)
- Loading branch information
Showing
5 changed files
with
93 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const os = require('os'); | ||
|
||
// eslint-disable-next-line unicorn/explicit-length-check | ||
const CPUS = os.cpus() && os.cpus().length; | ||
const THREADS = CPUS > 2 ? CPUS - 1 : 1; | ||
|
||
function getThreads(config) { | ||
const { threads } = config; | ||
|
||
switch (threads) { | ||
case 'auto': | ||
case -1: | ||
case '': | ||
case true: | ||
case null: | ||
case undefined: | ||
return THREADS; | ||
case 0: | ||
case 1: | ||
case false: | ||
return 1; | ||
default: | ||
if (threads > 1) { | ||
return threads; | ||
} | ||
} | ||
} | ||
|
||
module.exports = getThreads; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert').strict; | ||
const os = require('os'); | ||
const getThreads = require('../lib/getThreads.js'); | ||
|
||
// eslint-disable-next-line unicorn/explicit-length-check | ||
const CPUS = os.cpus() && os.cpus().length; | ||
const THREADS = CPUS > 2 ? CPUS - 1 : 1; | ||
|
||
describe('getThreads', () => { | ||
it('should use the number of available threads -1', done => { | ||
const config = {}; | ||
|
||
for (const option of ['auto', -1, '', true, null, undefined]) { | ||
config.threads = option; | ||
const expected = THREADS; | ||
const actual = getThreads(config); | ||
|
||
assert.equal(actual, expected, `"thread: ${option}" failed!`); | ||
} | ||
|
||
done(); | ||
}); | ||
|
||
it('should return 1 with false, 0, or 1', done => { | ||
const config = {}; | ||
|
||
for (const option of [0, 1, false]) { | ||
config.threads = option; | ||
const expected = 1; | ||
const actual = getThreads(config); | ||
assert.equal(actual, expected); | ||
} | ||
|
||
done(); | ||
}); | ||
|
||
it('should return the number of threads', done => { | ||
const config = { | ||
threads: 4 | ||
}; | ||
const expected = 4; | ||
const actual = getThreads(config); | ||
|
||
assert.equal(actual, expected); | ||
done(); | ||
}); | ||
}); |