1 const crypto
= require('crypto')
2 const fs
= require('fs')
13 } = require('./benchmarks-types')
15 async
function runTest (pool
, { taskExecutions
, workerData
}) {
16 return new Promise((resolve
, reject
) => {
18 for (let i
= 1; i
<= taskExecutions
; i
++) {
23 if (executions
=== taskExecutions
) {
24 return resolve({ ok
: 1 })
36 function generateRandomInteger (max
= Number
.MAX_SAFE_INTEGER
, min
= 0) {
37 if (max
< min
|| max
< 0 || min
< 0) {
38 throw new RangeError('Invalid interval')
41 if (min
!= null && min
!== 0) {
43 return Math
.floor(Math
.random() * (max
- min
+ 1)) + min
45 return Math
.floor(Math
.random() * (max
+ 1))
48 function jsonIntegerSerialization (n
) {
49 for (let i
= 0; i
< n
; i
++) {
58 * Intentionally inefficient implementation.
59 * @param {number} n - The number of fibonacci numbers to generate.
60 * @returns {number} - The nth fibonacci number.
62 function fibonacci (n
) {
64 return fibonacci(n
- 1) + fibonacci(n
- 2)
68 * Intentionally inefficient implementation.
69 * @param {number} n - The number to calculate the factorial of.
70 * @returns {number} - The factorial of n.
72 function factorial (n
) {
76 return factorial(n
- 1) * n
79 function readWriteFiles (
81 baseDirectory
= `/tmp/poolifier-benchmarks/${crypto.randomInt(
85 if (fs
.existsSync(baseDirectory
) === true) {
86 fs
.rmSync(baseDirectory
, { recursive
: true })
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(), {
95 fs
.readFileSync(filePath
, 'utf8')
97 fs
.rmSync(baseDirectory
, { recursive
: true })
100 function executeWorkerFunction (data
) {
101 switch (data
.function) {
102 case WorkerFunctions
.jsonIntegerSerialization
:
103 return jsonIntegerSerialization(data
.taskSize
|| 1000)
104 case WorkerFunctions
.fibonacci
:
105 return fibonacci(data
.taskSize
|| 1000)
106 case WorkerFunctions
.factorial
:
107 return factorial(data
.taskSize
|| 1000)
108 case WorkerFunctions
.readWriteFiles
:
109 return readWriteFiles(data
.taskSize
|| 1000)
111 throw new Error('Unknown worker function')
115 function buildPool (workerType
, poolType
, poolSize
, poolOptions
) {
117 case PoolTypes
.FIXED
:
118 switch (workerType
) {
119 case WorkerTypes
.THREAD
:
120 return new FixedThreadPool(
122 './benchmarks/internal/thread-worker.js',
125 case WorkerTypes
.CLUSTER
:
126 return new FixedClusterPool(
128 './benchmarks/internal/cluster-worker.js',
133 case PoolTypes
.DYNAMIC
:
134 switch (workerType
) {
135 case WorkerTypes
.THREAD
:
136 return new DynamicThreadPool(
139 './benchmarks/internal/thread-worker.js',
142 case WorkerTypes
.CLUSTER
:
143 return new DynamicClusterPool(
146 './benchmarks/internal/cluster-worker.js',
157 executeWorkerFunction
,
158 generateRandomInteger
,