Add WRR worker choice strategy
[poolifier.git] / benchmarks / internal / benchmark-utils.js
CommitLineData
74750c7f 1async 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 })
14 .catch(err => console.error(err))
15 }
16 })
17}
18
bdacc2d2
JB
19function jsonIntegerSerialization (n) {
20 for (let i = 0; i < n; i++) {
21 const o = {
22 a: i
23 }
24 JSON.stringify(o)
25 }
26}
27
74750c7f 28function generateRandomInteger (max, min = 0) {
c2d7d79b 29 max = Math.floor(max)
74750c7f 30 if (min) {
c2d7d79b 31 min = Math.ceil(min)
872585ea 32 return Math.floor(Math.random() * (max - min + 1)) + min
74750c7f 33 }
872585ea 34 return Math.floor(Math.random() * (max + 1))
74750c7f
JB
35}
36
bdacc2d2
JB
37/**
38 * Intentionally inefficient implementation.
39 *
bdaf31cd 40 * @param {number} n
bdacc2d2
JB
41 * @returns {number}
42 */
43function fibonacci (n) {
44 if (n <= 1) return 1
45 return fibonacci(n - 1) + fibonacci(n - 2)
46}
47
292ad316
JB
48const LIST_FORMATTER = new Intl.ListFormat('en-US', {
49 style: 'long',
50 type: 'conjunction'
51})
52
bdacc2d2
JB
53module.exports = {
54 runPoolifierTest,
55 jsonIntegerSerialization,
56 generateRandomInteger,
57 fibonacci,
58 LIST_FORMATTER
59}