chore: v2.6.20
[poolifier.git] / benchmarks / benchmarks-utils.mjs
1 import crypto from 'crypto'
2 import fs from 'fs'
3 import {
4 DynamicClusterPool,
5 DynamicThreadPool,
6 FixedClusterPool,
7 FixedThreadPool
8 } from '../lib/index.mjs'
9 import { PoolTypes, WorkerFunctions, WorkerTypes } from './benchmarks-types.mjs'
10
11 export const runTest = async (pool, { taskExecutions, workerData }) => {
12 return new Promise((resolve, reject) => {
13 let executions = 0
14 for (let i = 1; i <= taskExecutions; i++) {
15 pool
16 .execute(workerData)
17 .then(() => {
18 ++executions
19 if (executions === taskExecutions) {
20 return resolve({ ok: 1 })
21 }
22 return null
23 })
24 .catch(err => {
25 console.error(err)
26 return reject(err)
27 })
28 }
29 })
30 }
31
32 export const generateRandomInteger = (
33 max = Number.MAX_SAFE_INTEGER,
34 min = 0
35 ) => {
36 if (max < min || max < 0 || min < 0) {
37 throw new RangeError('Invalid interval')
38 }
39 max = Math.floor(max)
40 if (min != null && min !== 0) {
41 min = Math.ceil(min)
42 return Math.floor(Math.random() * (max - min + 1)) + min
43 }
44 return Math.floor(Math.random() * (max + 1))
45 }
46
47 const jsonIntegerSerialization = n => {
48 for (let i = 0; i < n; i++) {
49 const o = {
50 a: i
51 }
52 JSON.stringify(o)
53 }
54 return { ok: 1 }
55 }
56
57 /**
58 * Intentionally inefficient implementation.
59 * @param {number} n - The number of fibonacci numbers to generate.
60 * @returns {number} - The nth fibonacci number.
61 */
62 const fibonacci = n => {
63 if (n <= 1) return n
64 return fibonacci(n - 1) + fibonacci(n - 2)
65 }
66
67 /**
68 * Intentionally inefficient implementation.
69 * @param {number} n - The number to calculate the factorial of.
70 * @returns {number} - The factorial of n.
71 */
72 const factorial = n => {
73 if (n === 0) {
74 return 1
75 }
76 return factorial(n - 1) * n
77 }
78
79 const 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 })
87 }
88 fs.mkdirSync(baseDirectory, { recursive: true })
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 }
97 fs.rmSync(baseDirectory, { recursive: true })
98 return { ok: 1 }
99 }
100
101 export const executeWorkerFunction = data => {
102 switch (data.function) {
103 case WorkerFunctions.jsonIntegerSerialization:
104 return jsonIntegerSerialization(data.taskSize || 1000)
105 case WorkerFunctions.fibonacci:
106 return fibonacci(data.taskSize || 1000)
107 case WorkerFunctions.factorial:
108 return factorial(data.taskSize || 1000)
109 case WorkerFunctions.readWriteFiles:
110 return readWriteFiles(data.taskSize || 1000)
111 default:
112 throw new Error('Unknown worker function')
113 }
114 }
115
116 export const buildPool = (workerType, poolType, poolSize, poolOptions) => {
117 switch (poolType) {
118 case PoolTypes.fixed:
119 switch (workerType) {
120 case WorkerTypes.thread:
121 return new FixedThreadPool(
122 poolSize,
123 './benchmarks/internal/thread-worker.mjs',
124 poolOptions
125 )
126 case WorkerTypes.cluster:
127 return new FixedClusterPool(
128 poolSize,
129 './benchmarks/internal/cluster-worker.mjs',
130 poolOptions
131 )
132 }
133 break
134 case PoolTypes.dynamic:
135 switch (workerType) {
136 case WorkerTypes.thread:
137 return new DynamicThreadPool(
138 Math.floor(poolSize / 2),
139 poolSize,
140 './benchmarks/internal/thread-worker.mjs',
141 poolOptions
142 )
143 case WorkerTypes.cluster:
144 return new DynamicClusterPool(
145 Math.floor(poolSize / 2),
146 poolSize,
147 './benchmarks/internal/cluster-worker.mjs',
148 poolOptions
149 )
150 }
151 break
152 }
153 }