refactor: use eslint plugin to sort imports
[poolifier.git] / benchmarks / benchmarks-utils.js
CommitLineData
670734fc 1const crypto = require('crypto')
cdace0e5 2const fs = require('fs')
cdace0e5
JB
3const {
4 DynamicClusterPool,
5 DynamicThreadPool,
6 FixedClusterPool,
7 FixedThreadPool
8} = require('../lib')
65d7a1c9
JB
9const {
10 PoolTypes,
11 WorkerFunctions,
12 WorkerTypes
13} = require('./benchmarks-types')
2d2e32c2 14
cdace0e5 15async function runTest (pool, { taskExecutions, workerData }) {
ff5e76e1
JB
16 return new Promise((resolve, reject) => {
17 let executions = 0
cdace0e5 18 for (let i = 1; i <= taskExecutions; i++) {
ff5e76e1
JB
19 pool
20 .execute(workerData)
fe2f6f84 21 .then(() => {
0762fbb1 22 ++executions
cdace0e5 23 if (executions === taskExecutions) {
ca6c7d70 24 return resolve({ ok: 1 })
ff5e76e1
JB
25 }
26 return null
27 })
23ff945a
JB
28 .catch(err => {
29 console.error(err)
30 return reject(err)
31 })
ff5e76e1
JB
32 }
33 })
34}
35
9bc18a6d 36function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) {
548140e6 37 if (max < min || max < 0 || min < 0) {
4af5c11a
JB
38 throw new RangeError('Invalid interval')
39 }
c2d7d79b 40 max = Math.floor(max)
4af5c11a 41 if (min != null && min !== 0) {
c2d7d79b 42 min = Math.ceil(min)
872585ea 43 return Math.floor(Math.random() * (max - min + 1)) + min
74750c7f 44 }
872585ea 45 return Math.floor(Math.random() * (max + 1))
74750c7f
JB
46}
47
cdace0e5
JB
48function jsonIntegerSerialization (n) {
49 for (let i = 0; i < n; i++) {
50 const o = {
51 a: i
52 }
53 JSON.stringify(o)
54 }
55}
56
bdacc2d2
JB
57/**
58 * Intentionally inefficient implementation.
7d82d90e
JB
59 * @param {number} n - The number of fibonacci numbers to generate.
60 * @returns {number} - The nth fibonacci number.
bdacc2d2
JB
61 */
62function fibonacci (n) {
63 if (n <= 1) return 1
64 return fibonacci(n - 1) + fibonacci(n - 2)
65}
66
7d82d90e
JB
67/**
68 * Intentionally inefficient implementation.
7d82d90e
JB
69 * @param {number} n - The number to calculate the factorial of.
70 * @returns {number} - The factorial of n.
71 */
72function factorial (n) {
73 if (n === 0) {
74 return 1
7d82d90e 75 }
965415bb 76 return factorial(n - 1) * n
7d82d90e
JB
77}
78
670734fc
JB
79function readWriteFiles (
80 n,
81 baseDirectory = `/tmp/poolifier-benchmarks/${crypto.randomInt(
82 281474976710655
83 )}`
84) {
85 if (fs.existsSync(baseDirectory) === true) {
86 fs.rmSync(baseDirectory, { recursive: true })
cdace0e5 87 }
670734fc 88 fs.mkdirSync(baseDirectory, { recursive: true })
cdace0e5
JB
89 for (let i = 0; i < n; i++) {
90 const filePath = `${baseDirectory}/${i}`
91 fs.writeFileSync(filePath, i.toString(), {
92 encoding: 'utf8',
93 flag: 'a'
94 })
95 fs.readFileSync(filePath, 'utf8')
96 }
670734fc 97 fs.rmSync(baseDirectory, { recursive: true })
cdace0e5
JB
98}
99
2d2e32c2
JB
100function executeWorkerFunction (data) {
101 switch (data.function) {
102 case WorkerFunctions.jsonIntegerSerialization:
d1a9aa41 103 return jsonIntegerSerialization(data.taskSize || 1000)
2d2e32c2 104 case WorkerFunctions.fibonacci:
d1a9aa41 105 return fibonacci(data.taskSize || 1000)
2d2e32c2 106 case WorkerFunctions.factorial:
d1a9aa41 107 return factorial(data.taskSize || 1000)
cdace0e5
JB
108 case WorkerFunctions.readWriteFiles:
109 return readWriteFiles(data.taskSize || 1000)
2d2e32c2
JB
110 default:
111 throw new Error('Unknown worker function')
112 }
113}
114
1f29c16f 115function buildPool (workerType, poolType, poolSize, poolOptions) {
cdace0e5
JB
116 switch (poolType) {
117 case PoolTypes.FIXED:
118 switch (workerType) {
119 case WorkerTypes.THREAD:
120 return new FixedThreadPool(
121 poolSize,
122 './benchmarks/internal/thread-worker.js',
123 poolOptions
124 )
125 case WorkerTypes.CLUSTER:
126 return new FixedClusterPool(
127 poolSize,
128 './benchmarks/internal/cluster-worker.js',
129 poolOptions
130 )
131 }
132 break
133 case PoolTypes.DYNAMIC:
134 switch (workerType) {
135 case WorkerTypes.THREAD:
136 return new DynamicThreadPool(
137 poolSize / 2,
138 poolSize * 3,
139 './benchmarks/internal/thread-worker.js',
140 poolOptions
141 )
142 case WorkerTypes.CLUSTER:
143 return new DynamicClusterPool(
144 poolSize / 2,
145 poolSize * 3,
146 './benchmarks/internal/cluster-worker.js',
147 poolOptions
148 )
149 }
150 break
151 }
152}
153
bdacc2d2 154module.exports = {
2d2e32c2 155 WorkerFunctions,
cdace0e5 156 buildPool,
2d2e32c2 157 executeWorkerFunction,
bdacc2d2 158 generateRandomInteger,
cdace0e5
JB
159 readWriteFiles,
160 runTest
bdacc2d2 161}