1 import crypto from 'node:crypto'
2 import fs from 'node:fs'
10 } from '../lib/index.mjs'
11 import { TaskFunctions } from './benchmarks-types.mjs'
13 export const buildPoolifierPool = (
22 case WorkerTypes.thread:
23 return new FixedThreadPool(
25 './benchmarks/internal/thread-worker.mjs',
28 case WorkerTypes.cluster:
29 return new FixedClusterPool(
31 './benchmarks/internal/cluster-worker.mjs',
36 case PoolTypes.dynamic:
38 case WorkerTypes.thread:
39 return new DynamicThreadPool(
40 Math.floor(poolSize / 2),
42 './benchmarks/internal/thread-worker.mjs',
45 case WorkerTypes.cluster:
46 return new DynamicClusterPool(
47 Math.floor(poolSize / 2),
49 './benchmarks/internal/cluster-worker.mjs',
57 export const runPoolifierTest = async (
59 { taskExecutions, workerData }
61 return new Promise((resolve, reject) => {
63 for (let i = 1; i <= taskExecutions; i++) {
68 if (executions === taskExecutions) {
69 return resolve({ ok: 1 })
81 export const getPoolImplementationName = pool => {
82 if (pool instanceof FixedThreadPool) {
83 return 'FixedThreadPool'
84 } else if (pool instanceof DynamicThreadPool) {
85 return 'DynamicThreadPool'
86 } else if (pool instanceof FixedClusterPool) {
87 return 'FixedClusterPool'
88 } else if (pool instanceof DynamicClusterPool) {
89 return 'DynamicClusterPool'
93 export const LIST_FORMATTER = new Intl.ListFormat('en-US', {
98 export const executeAsyncFn = async fn => {
103 // eslint-disable-next-line n/no-process-exit
108 export const generateRandomInteger = (
109 max = Number.MAX_SAFE_INTEGER,
112 if (max < min || max < 0 || min < 0) {
113 throw new RangeError('Invalid interval')
115 max = Math.floor(max)
116 if (min != null && min !== 0) {
118 return Math.floor(Math.random() * (max - min + 1)) + min
120 return Math.floor(Math.random() * (max + 1))
123 const jsonIntegerSerialization = n => {
124 for (let i = 0; i < n; i++) {
134 * Intentionally inefficient implementation.
135 * @param {number} n - The number of fibonacci numbers to generate.
136 * @returns {number} - The nth fibonacci number.
138 const fibonacci = n => {
140 return fibonacci(n - 1) + fibonacci(n - 2)
144 * Intentionally inefficient implementation.
145 * @param {number} n - The number to calculate the factorial of.
146 * @returns {number} - The factorial of n.
148 const factorial = n => {
152 return factorial(n - 1) * n
155 const readWriteFiles = (
157 baseDirectory = `/tmp/poolifier-benchmarks/${crypto.randomInt(
161 if (fs.existsSync(baseDirectory) === true) {
162 fs.rmSync(baseDirectory, { recursive: true })
164 fs.mkdirSync(baseDirectory, { recursive: true })
165 for (let i = 0; i < n; i++) {
166 const filePath = `${baseDirectory}/${i}`
167 fs.writeFileSync(filePath, i.toString(), {
171 fs.readFileSync(filePath, 'utf8')
173 fs.rmSync(baseDirectory, { recursive: true })
177 export const executeTaskFunction = data => {
178 switch (data.function) {
179 case TaskFunctions.jsonIntegerSerialization:
180 return jsonIntegerSerialization(data.taskSize || 1000)
181 case TaskFunctions.fibonacci:
182 return fibonacci(data.taskSize || 1000)
183 case TaskFunctions.factorial:
184 return factorial(data.taskSize || 1000)
185 case TaskFunctions.readWriteFiles:
186 return readWriteFiles(data.taskSize || 1000)
188 throw new Error('Unknown task function')