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