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