50dcc40c6edaff1a45d885c7c0dd5d531efe3ae6
1 const fs
= require('fs')
6 } = require('./benchmarks-types')
14 async
function runTest (pool
, { taskExecutions
, workerData
}) {
15 return new Promise((resolve
, reject
) => {
17 for (let i
= 1; i
<= taskExecutions
; i
++) {
22 if (executions
=== taskExecutions
) {
23 return resolve({ ok
: 1 })
35 function generateRandomInteger (max
= Number
.MAX_SAFE_INTEGER
, min
= 0) {
36 if (max
< min
|| max
< 0 || min
< 0) {
37 throw new RangeError('Invalid interval')
40 if (min
!= null && min
!== 0) {
42 return Math
.floor(Math
.random() * (max
- min
+ 1)) + min
44 return Math
.floor(Math
.random() * (max
+ 1))
47 function jsonIntegerSerialization (n
) {
48 for (let i
= 0; i
< n
; i
++) {
57 * 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.
70 * @param {number} n - The number to calculate the factorial of.
71 * @returns {number} - The factorial of n.
73 function factorial (n
) {
77 return factorial(n
- 1) * n
80 function readWriteFiles (n
) {
81 const baseDirectory
= '/tmp/poolifier-benchmarks'
82 if (fs
.existsSync(baseDirectory
) === false) {
83 fs
.mkdirSync(baseDirectory
, { recursive
: true })
85 for (let i
= 0; i
< n
; i
++) {
86 const filePath
= `${baseDirectory}/${i}`
87 fs
.writeFileSync(filePath
, i
.toString(), {
91 fs
.readFileSync(filePath
, 'utf8')
95 function executeWorkerFunction (data
) {
96 switch (data
.function) {
97 case WorkerFunctions
.jsonIntegerSerialization
:
98 return jsonIntegerSerialization(data
.taskSize
|| 1000)
99 case WorkerFunctions
.fibonacci
:
100 return fibonacci(data
.taskSize
|| 1000)
101 case WorkerFunctions
.factorial
:
102 return factorial(data
.taskSize
|| 1000)
103 case WorkerFunctions
.readWriteFiles
:
104 return readWriteFiles(data
.taskSize
|| 1000)
106 throw new Error('Unknown worker function')
110 function buildPool (poolType
, poolSize
, workerType
, poolOptions
) {
112 case PoolTypes
.FIXED
:
113 switch (workerType
) {
114 case WorkerTypes
.THREAD
:
115 return new FixedThreadPool(
117 './benchmarks/internal/thread-worker.js',
120 case WorkerTypes
.CLUSTER
:
121 return new FixedClusterPool(
123 './benchmarks/internal/cluster-worker.js',
128 case PoolTypes
.DYNAMIC
:
129 switch (workerType
) {
130 case WorkerTypes
.THREAD
:
131 return new DynamicThreadPool(
134 './benchmarks/internal/thread-worker.js',
137 case WorkerTypes
.CLUSTER
:
138 return new DynamicClusterPool(
141 './benchmarks/internal/cluster-worker.js',
152 executeWorkerFunction
,
153 generateRandomInteger
,