Add eslint report to sonar
[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 => {
15 console.error(err)
16 return reject(err)
17 })
18 }
19 })
20 }
21
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
31 function generateRandomInteger (max, min = 0) {
32 max = Math.floor(max)
33 if (min) {
34 min = Math.ceil(min)
35 return Math.floor(Math.random() * (max - min + 1)) + min
36 }
37 return Math.floor(Math.random() * (max + 1))
38 }
39
40 /**
41 * Intentionally inefficient implementation.
42 *
43 * @param {number} n - The number of fibonacci numbers to generate.
44 * @returns {number} - The nth fibonacci number.
45 */
46 function fibonacci (n) {
47 if (n <= 1) return 1
48 return fibonacci(n - 1) + fibonacci(n - 2)
49 }
50
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
65 const LIST_FORMATTER = new Intl.ListFormat('en-US', {
66 style: 'long',
67 type: 'conjunction'
68 })
69
70 module.exports = {
71 runPoolifierTest,
72 jsonIntegerSerialization,
73 generateRandomInteger,
74 fibonacci,
75 factorial,
76 LIST_FORMATTER
77 }