Commit | Line | Data |
---|---|---|
74750c7f | 1 | async function runPoolifierTest (pool, { tasks, workerData }) { |
ff5e76e1 JB |
2 | return new Promise((resolve, reject) => { |
3 | let executions = 0 | |
292ad316 | 4 | for (let i = 1; i <= tasks; i++) { |
ff5e76e1 JB |
5 | pool |
6 | .execute(workerData) | |
7 | .then(res => { | |
8 | executions++ | |
9 | if (executions === tasks) { | |
10 | return resolve('FINISH') | |
11 | } | |
12 | return null | |
13 | }) | |
23ff945a JB |
14 | .catch(err => { |
15 | console.error(err) | |
16 | return reject(err) | |
17 | }) | |
ff5e76e1 JB |
18 | } |
19 | }) | |
20 | } | |
21 | ||
bdacc2d2 JB |
22 | function jsonIntegerSerialization (n) { |
23 | for (let i = 0; i < n; i++) { | |
24 | const o = { | |
25 | a: i | |
26 | } | |
27 | JSON.stringify(o) | |
28 | } | |
29 | } | |
30 | ||
74750c7f | 31 | function generateRandomInteger (max, min = 0) { |
c2d7d79b | 32 | max = Math.floor(max) |
74750c7f | 33 | if (min) { |
c2d7d79b | 34 | min = Math.ceil(min) |
872585ea | 35 | return Math.floor(Math.random() * (max - min + 1)) + min |
74750c7f | 36 | } |
872585ea | 37 | return Math.floor(Math.random() * (max + 1)) |
74750c7f JB |
38 | } |
39 | ||
bdacc2d2 JB |
40 | /** |
41 | * Intentionally inefficient implementation. | |
42 | * | |
bdaf31cd | 43 | * @param {number} n |
bdacc2d2 JB |
44 | * @returns {number} |
45 | */ | |
46 | function fibonacci (n) { | |
47 | if (n <= 1) return 1 | |
48 | return fibonacci(n - 1) + fibonacci(n - 2) | |
49 | } | |
50 | ||
292ad316 JB |
51 | const LIST_FORMATTER = new Intl.ListFormat('en-US', { |
52 | style: 'long', | |
53 | type: 'conjunction' | |
54 | }) | |
55 | ||
bdacc2d2 JB |
56 | module.exports = { |
57 | runPoolifierTest, | |
58 | jsonIntegerSerialization, | |
59 | generateRandomInteger, | |
60 | fibonacci, | |
61 | LIST_FORMATTER | |
62 | } |