feat: add initial continous benchmarking
[poolifier.git] / benchmarks / benchmarks-utils.mjs
1 import crypto from 'node:crypto'
2 import fs from 'node:fs'
3 import {
4 DynamicClusterPool,
5 DynamicThreadPool,
6 FixedClusterPool,
7 FixedThreadPool,
8 PoolTypes,
9 WorkerTypes
10 } from '../lib/index.mjs'
11 import { TaskFunctions } from './benchmarks-types.mjs'
12
13 export const buildPoolifierPool = (
14 workerType,
15 poolType,
16 poolSize,
17 poolOptions
18 ) => {
19 switch (poolType) {
20 case PoolTypes.fixed:
21 switch (workerType) {
22 case WorkerTypes.thread:
23 return new FixedThreadPool(
24 poolSize,
25 './benchmarks/internal/thread-worker.mjs',
26 poolOptions
27 )
28 case WorkerTypes.cluster:
29 return new FixedClusterPool(
30 poolSize,
31 './benchmarks/internal/cluster-worker.mjs',
32 poolOptions
33 )
34 }
35 break
36 case PoolTypes.dynamic:
37 switch (workerType) {
38 case WorkerTypes.thread:
39 return new DynamicThreadPool(
40 Math.floor(poolSize / 2),
41 poolSize,
42 './benchmarks/internal/thread-worker.mjs',
43 poolOptions
44 )
45 case WorkerTypes.cluster:
46 return new DynamicClusterPool(
47 Math.floor(poolSize / 2),
48 poolSize,
49 './benchmarks/internal/cluster-worker.mjs',
50 poolOptions
51 )
52 }
53 break
54 }
55 }
56
57 export const runPoolifierTest = async (
58 pool,
59 { taskExecutions, workerData }
60 ) => {
61 return new Promise((resolve, reject) => {
62 let executions = 0
63 for (let i = 1; i <= taskExecutions; i++) {
64 pool
65 .execute(workerData)
66 .then(() => {
67 ++executions
68 if (executions === taskExecutions) {
69 return resolve({ ok: 1 })
70 }
71 return null
72 })
73 .catch(err => {
74 console.error(err)
75 return reject(err)
76 })
77 }
78 })
79 }
80
81 export const LIST_FORMATTER = new Intl.ListFormat('en-US', {
82 style: 'long',
83 type: 'conjunction'
84 })
85
86 export const executeAsyncFn = async fn => {
87 try {
88 await fn()
89 } catch (e) {
90 console.error(e)
91 // eslint-disable-next-line n/no-process-exit
92 process.exit(1)
93 }
94 }
95
96 export const generateRandomInteger = (
97 max = Number.MAX_SAFE_INTEGER,
98 min = 0
99 ) => {
100 if (max < min || max < 0 || min < 0) {
101 throw new RangeError('Invalid interval')
102 }
103 max = Math.floor(max)
104 if (min != null && min !== 0) {
105 min = Math.ceil(min)
106 return Math.floor(Math.random() * (max - min + 1)) + min
107 }
108 return Math.floor(Math.random() * (max + 1))
109 }
110
111 const jsonIntegerSerialization = n => {
112 for (let i = 0; i < n; i++) {
113 const o = {
114 a: i
115 }
116 JSON.stringify(o)
117 }
118 return { ok: 1 }
119 }
120
121 /**
122 * Intentionally inefficient implementation.
123 * @param {number} n - The number of fibonacci numbers to generate.
124 * @returns {number} - The nth fibonacci number.
125 */
126 const fibonacci = n => {
127 if (n <= 1) return n
128 return fibonacci(n - 1) + fibonacci(n - 2)
129 }
130
131 /**
132 * Intentionally inefficient implementation.
133 * @param {number} n - The number to calculate the factorial of.
134 * @returns {number} - The factorial of n.
135 */
136 const factorial = n => {
137 if (n === 0) {
138 return 1
139 }
140 return factorial(n - 1) * n
141 }
142
143 const readWriteFiles = (
144 n,
145 baseDirectory = `/tmp/poolifier-benchmarks/${crypto.randomInt(
146 281474976710655
147 )}`
148 ) => {
149 if (fs.existsSync(baseDirectory) === true) {
150 fs.rmSync(baseDirectory, { recursive: true })
151 }
152 fs.mkdirSync(baseDirectory, { recursive: true })
153 for (let i = 0; i < n; i++) {
154 const filePath = `${baseDirectory}/${i}`
155 fs.writeFileSync(filePath, i.toString(), {
156 encoding: 'utf8',
157 flag: 'a'
158 })
159 fs.readFileSync(filePath, 'utf8')
160 }
161 fs.rmSync(baseDirectory, { recursive: true })
162 return { ok: 1 }
163 }
164
165 export const executeTaskFunction = data => {
166 switch (data.function) {
167 case TaskFunctions.jsonIntegerSerialization:
168 return jsonIntegerSerialization(data.taskSize || 1000)
169 case TaskFunctions.fibonacci:
170 return fibonacci(data.taskSize || 1000)
171 case TaskFunctions.factorial:
172 return factorial(data.taskSize || 1000)
173 case TaskFunctions.readWriteFiles:
174 return readWriteFiles(data.taskSize || 1000)
175 default:
176 throw new Error('Unknown task function')
177 }
178 }