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 | * | |
7d82d90e JB |
43 | * @param {number} n - The number of fibonacci numbers to generate. |
44 | * @returns {number} - The nth fibonacci number. | |
bdacc2d2 JB |
45 | */ |
46 | function fibonacci (n) { | |
47 | if (n <= 1) return 1 | |
48 | return fibonacci(n - 1) + fibonacci(n - 2) | |
49 | } | |
50 | ||
7d82d90e JB |
51 | /** |
52 | * Intentionally inefficient implementation. | |
53 | * | |
54 | * @param {number} n - The number to calculate the factorial of. | |
55 | * @returns {number} - The factorial of n. | |
56 | */ | |
57 | function factorial (n) { | |
58 | if (n === 0) { | |
59 | return 1 | |
60 | } else { | |
61 | return factorial(n - 1) * n | |
62 | } | |
63 | } | |
64 | ||
292ad316 JB |
65 | const LIST_FORMATTER = new Intl.ListFormat('en-US', { |
66 | style: 'long', | |
67 | type: 'conjunction' | |
68 | }) | |
69 | ||
bdacc2d2 JB |
70 | module.exports = { |
71 | runPoolifierTest, | |
72 | jsonIntegerSerialization, | |
73 | generateRandomInteger, | |
74 | fibonacci, | |
7d82d90e | 75 | factorial, |
bdacc2d2 JB |
76 | LIST_FORMATTER |
77 | } |