Commit | Line | Data |
---|---|---|
e4bc7a49 | 1 | const { WorkerFunctions } = require('./benchmarks-types') |
2d2e32c2 | 2 | |
74750c7f | 3 | async function runPoolifierTest (pool, { tasks, workerData }) { |
ff5e76e1 JB |
4 | return new Promise((resolve, reject) => { |
5 | let executions = 0 | |
292ad316 | 6 | for (let i = 1; i <= tasks; i++) { |
ff5e76e1 JB |
7 | pool |
8 | .execute(workerData) | |
fe2f6f84 | 9 | .then(() => { |
ff5e76e1 JB |
10 | executions++ |
11 | if (executions === tasks) { | |
12 | return resolve('FINISH') | |
13 | } | |
14 | return null | |
15 | }) | |
23ff945a JB |
16 | .catch(err => { |
17 | console.error(err) | |
18 | return reject(err) | |
19 | }) | |
ff5e76e1 JB |
20 | } |
21 | }) | |
22 | } | |
23 | ||
bdacc2d2 JB |
24 | function jsonIntegerSerialization (n) { |
25 | for (let i = 0; i < n; i++) { | |
26 | const o = { | |
27 | a: i | |
28 | } | |
29 | JSON.stringify(o) | |
30 | } | |
31 | } | |
32 | ||
9bc18a6d | 33 | function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { |
c2d7d79b | 34 | max = Math.floor(max) |
74750c7f | 35 | if (min) { |
c2d7d79b | 36 | min = Math.ceil(min) |
872585ea | 37 | return Math.floor(Math.random() * (max - min + 1)) + min |
74750c7f | 38 | } |
872585ea | 39 | return Math.floor(Math.random() * (max + 1)) |
74750c7f JB |
40 | } |
41 | ||
bdacc2d2 JB |
42 | /** |
43 | * Intentionally inefficient implementation. | |
44 | * | |
7d82d90e JB |
45 | * @param {number} n - The number of fibonacci numbers to generate. |
46 | * @returns {number} - The nth fibonacci number. | |
bdacc2d2 JB |
47 | */ |
48 | function fibonacci (n) { | |
49 | if (n <= 1) return 1 | |
50 | return fibonacci(n - 1) + fibonacci(n - 2) | |
51 | } | |
52 | ||
7d82d90e JB |
53 | /** |
54 | * Intentionally inefficient implementation. | |
55 | * | |
56 | * @param {number} n - The number to calculate the factorial of. | |
57 | * @returns {number} - The factorial of n. | |
58 | */ | |
59 | function factorial (n) { | |
60 | if (n === 0) { | |
61 | return 1 | |
62 | } else { | |
63 | return factorial(n - 1) * n | |
64 | } | |
65 | } | |
66 | ||
2d2e32c2 JB |
67 | function executeWorkerFunction (data) { |
68 | switch (data.function) { | |
69 | case WorkerFunctions.jsonIntegerSerialization: | |
d1a9aa41 | 70 | return jsonIntegerSerialization(data.taskSize || 1000) |
2d2e32c2 | 71 | case WorkerFunctions.fibonacci: |
d1a9aa41 | 72 | return fibonacci(data.taskSize || 1000) |
2d2e32c2 | 73 | case WorkerFunctions.factorial: |
d1a9aa41 | 74 | return factorial(data.taskSize || 1000) |
2d2e32c2 JB |
75 | default: |
76 | throw new Error('Unknown worker function') | |
77 | } | |
78 | } | |
79 | ||
292ad316 JB |
80 | const LIST_FORMATTER = new Intl.ListFormat('en-US', { |
81 | style: 'long', | |
82 | type: 'conjunction' | |
83 | }) | |
84 | ||
bdacc2d2 | 85 | module.exports = { |
2d2e32c2 JB |
86 | LIST_FORMATTER, |
87 | WorkerFunctions, | |
88 | executeWorkerFunction, | |
bdacc2d2 | 89 | generateRandomInteger, |
2d2e32c2 | 90 | runPoolifierTest |
bdacc2d2 | 91 | } |