chore: v2.6.20
[poolifier.git] / benchmarks / benchmarks-utils.mjs
CommitLineData
8f810074
JB
1import crypto from 'crypto'
2import fs from 'fs'
3import {
cdace0e5
JB
4 DynamicClusterPool,
5 DynamicThreadPool,
6 FixedClusterPool,
7 FixedThreadPool
8f810074
JB
8} from '../lib/index.mjs'
9import { PoolTypes, WorkerFunctions, WorkerTypes } from './benchmarks-types.mjs'
2d2e32c2 10
bac873bd 11export const runTest = async (pool, { taskExecutions, workerData }) => {
ff5e76e1
JB
12 return new Promise((resolve, reject) => {
13 let executions = 0
cdace0e5 14 for (let i = 1; i <= taskExecutions; i++) {
ff5e76e1
JB
15 pool
16 .execute(workerData)
fe2f6f84 17 .then(() => {
0762fbb1 18 ++executions
cdace0e5 19 if (executions === taskExecutions) {
ca6c7d70 20 return resolve({ ok: 1 })
ff5e76e1
JB
21 }
22 return null
23 })
23ff945a
JB
24 .catch(err => {
25 console.error(err)
26 return reject(err)
27 })
ff5e76e1
JB
28 }
29 })
30}
31
bac873bd
JB
32export const generateRandomInteger = (
33 max = Number.MAX_SAFE_INTEGER,
34 min = 0
35) => {
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
bac873bd 47const jsonIntegerSerialization = n => {
cdace0e5
JB
48 for (let i = 0; i < n; i++) {
49 const o = {
50 a: i
51 }
52 JSON.stringify(o)
53 }
30b963d4 54 return { ok: 1 }
cdace0e5
JB
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 61 */
bac873bd 62const fibonacci = n => {
024daf59 63 if (n <= 1) return n
bdacc2d2
JB
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 */
bac873bd 72const factorial = n => {
7d82d90e
JB
73 if (n === 0) {
74 return 1
7d82d90e 75 }
965415bb 76 return factorial(n - 1) * n
7d82d90e
JB
77}
78
bac873bd 79const readWriteFiles = (
670734fc
JB
80 n,
81 baseDirectory = `/tmp/poolifier-benchmarks/${crypto.randomInt(
82 281474976710655
83 )}`
bac873bd 84) => {
670734fc
JB
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 })
30b963d4 98 return { ok: 1 }
cdace0e5
JB
99}
100
bac873bd 101export const executeWorkerFunction = data => {
2d2e32c2
JB
102 switch (data.function) {
103 case WorkerFunctions.jsonIntegerSerialization:
d1a9aa41 104 return jsonIntegerSerialization(data.taskSize || 1000)
2d2e32c2 105 case WorkerFunctions.fibonacci:
d1a9aa41 106 return fibonacci(data.taskSize || 1000)
2d2e32c2 107 case WorkerFunctions.factorial:
d1a9aa41 108 return factorial(data.taskSize || 1000)
cdace0e5
JB
109 case WorkerFunctions.readWriteFiles:
110 return readWriteFiles(data.taskSize || 1000)
2d2e32c2
JB
111 default:
112 throw new Error('Unknown worker function')
113 }
114}
115
bac873bd 116export const buildPool = (workerType, poolType, poolSize, poolOptions) => {
cdace0e5 117 switch (poolType) {
bb615bd0 118 case PoolTypes.fixed:
cdace0e5 119 switch (workerType) {
bb615bd0 120 case WorkerTypes.thread:
cdace0e5
JB
121 return new FixedThreadPool(
122 poolSize,
8a970421 123 './benchmarks/internal/thread-worker.mjs',
cdace0e5
JB
124 poolOptions
125 )
bb615bd0 126 case WorkerTypes.cluster:
cdace0e5
JB
127 return new FixedClusterPool(
128 poolSize,
8a970421 129 './benchmarks/internal/cluster-worker.mjs',
cdace0e5
JB
130 poolOptions
131 )
132 }
133 break
bb615bd0 134 case PoolTypes.dynamic:
cdace0e5 135 switch (workerType) {
bb615bd0 136 case WorkerTypes.thread:
cdace0e5 137 return new DynamicThreadPool(
31a7d5be 138 Math.floor(poolSize / 2),
02749105 139 poolSize,
8a970421 140 './benchmarks/internal/thread-worker.mjs',
cdace0e5
JB
141 poolOptions
142 )
bb615bd0 143 case WorkerTypes.cluster:
cdace0e5 144 return new DynamicClusterPool(
31a7d5be 145 Math.floor(poolSize / 2),
02749105 146 poolSize,
8a970421 147 './benchmarks/internal/cluster-worker.mjs',
cdace0e5
JB
148 poolOptions
149 )
150 }
151 break
152 }
153}