-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow fractions to define thread limits. eg minThreads: 0.5 use…
…s 50% of available cores
- Loading branch information
1 parent
d7bd76c
commit 64c0f3e
Showing
2 changed files
with
73 additions
and
1 deletion.
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,50 @@ | ||
import Tinypool from 'tinypool' | ||
import { cpus } from 'os' | ||
const cpuCount = cpus().length | ||
|
||
const testIf = (condition) => (condition ? test : test.skip) | ||
|
||
describe('options', () => { | ||
// TODO mock amount instead? | ||
testIf(cpuCount > 1)('fractional thread limits can be set', async () => { | ||
const min = 0.5 | ||
const max = 0.75 | ||
const p = new Tinypool({ | ||
minThreads: min, | ||
maxThreads: max, | ||
}) | ||
|
||
expect(p.options.minThreads).toBe(Math.floor(cpuCount * min)) | ||
expect(p.options.maxThreads).toBe(Math.floor(cpuCount * max)) | ||
}) | ||
|
||
test('fractional thread limits result is 1 for very low fractions', async () => { | ||
const min = 0.00005 | ||
const max = 0.00006 | ||
const p = new Tinypool({ | ||
minThreads: min, | ||
maxThreads: max, | ||
}) | ||
|
||
expect(p.options.minThreads).toBe(1) | ||
expect(p.options.maxThreads).toBe(1) | ||
}) | ||
|
||
testIf(cpuCount > 2)( | ||
'fractional thread limits in the wrong order throw an error', | ||
async () => { | ||
expect(() => { | ||
new Tinypool({ | ||
minThreads: 0.75, | ||
maxThreads: 0.25, | ||
}) | ||
}).toThrow() | ||
expect(() => { | ||
new Tinypool({ | ||
minThreads: 0.75, | ||
maxThreads: 1, | ||
}) | ||
}).toThrow() | ||
} | ||
) | ||
}) |