Add dynamic worker choice strategy change at runtime
[poolifier.git] / benchmarks / internal / benchmark-utils.js
1 async function runPoolifierTest (pool, { tasks, workerData }) {
2 return new Promise((resolve, reject) => {
3 let executions = 0
4 for (let i = 1; i <= tasks; i++) {
5 pool
6 .execute(workerData)
7 .then(res => {
8 executions++
9 if (executions === tasks) {
10 return resolve('FINISH')
11 }
12 return null
13 })
14 .catch(err => console.error(err))
15 }
16 })
17 }
18
19 function jsonIntegerSerialization (n) {
20 for (let i = 0; i < n; i++) {
21 const o = {
22 a: i
23 }
24 JSON.stringify(o)
25 }
26 }
27
28 function generateRandomInteger (max, min = 0) {
29 max = Math.floor(max)
30 if (min) {
31 min = Math.ceil(min)
32 return Math.floor(Math.random() * (max - min + 1)) + min
33 }
34 return Math.floor(Math.random() * (max + 1))
35 }
36
37 /**
38 * Intentionally inefficient implementation.
39 *
40 * @param {number} n
41 * @returns {number}
42 */
43 function fibonacci (n) {
44 if (n <= 1) return 1
45 return fibonacci(n - 1) + fibonacci(n - 2)
46 }
47
48 const LIST_FORMATTER = new Intl.ListFormat('en-US', {
49 style: 'long',
50 type: 'conjunction'
51 })
52
53 module.exports = {
54 runPoolifierTest,
55 jsonIntegerSerialization,
56 generateRandomInteger,
57 fibonacci,
58 LIST_FORMATTER
59 }