refactor: split TestUtils class into arrow functions
[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 }
54}
55
bdacc2d2
JB
56/**
57 * Intentionally inefficient implementation.
7d82d90e
JB
58 * @param {number} n - The number of fibonacci numbers to generate.
59 * @returns {number} - The nth fibonacci number.
bdacc2d2 60 */
bac873bd 61const fibonacci = n => {
024daf59 62 if (n <= 1) return n
bdacc2d2
JB
63 return fibonacci(n - 1) + fibonacci(n - 2)
64}
65
7d82d90e
JB
66/**
67 * Intentionally inefficient implementation.
7d82d90e
JB
68 * @param {number} n - The number to calculate the factorial of.
69 * @returns {number} - The factorial of n.
70 */
bac873bd 71const factorial = n => {
7d82d90e
JB
72 if (n === 0) {
73 return 1
7d82d90e 74 }
965415bb 75 return factorial(n - 1) * n
7d82d90e
JB
76}
77
bac873bd 78const readWriteFiles = (
670734fc
JB
79 n,
80 baseDirectory = `/tmp/poolifier-benchmarks/${crypto.randomInt(
81 281474976710655
82 )}`
bac873bd 83) => {
670734fc
JB
84 if (fs.existsSync(baseDirectory) === true) {
85 fs.rmSync(baseDirectory, { recursive: true })
cdace0e5 86 }
670734fc 87 fs.mkdirSync(baseDirectory, { recursive: true })
cdace0e5
JB
88 for (let i = 0; i < n; i++) {
89 const filePath = `${baseDirectory}/${i}`
90 fs.writeFileSync(filePath, i.toString(), {
91 encoding: 'utf8',
92 flag: 'a'
93 })
94 fs.readFileSync(filePath, 'utf8')
95 }
670734fc 96 fs.rmSync(baseDirectory, { recursive: true })
cdace0e5
JB
97}
98
bac873bd 99export const executeWorkerFunction = data => {
2d2e32c2
JB
100 switch (data.function) {
101 case WorkerFunctions.jsonIntegerSerialization:
d1a9aa41 102 return jsonIntegerSerialization(data.taskSize || 1000)
2d2e32c2 103 case WorkerFunctions.fibonacci:
d1a9aa41 104 return fibonacci(data.taskSize || 1000)
2d2e32c2 105 case WorkerFunctions.factorial:
d1a9aa41 106 return factorial(data.taskSize || 1000)
cdace0e5
JB
107 case WorkerFunctions.readWriteFiles:
108 return readWriteFiles(data.taskSize || 1000)
2d2e32c2
JB
109 default:
110 throw new Error('Unknown worker function')
111 }
112}
113
bac873bd 114export const buildPool = (workerType, poolType, poolSize, poolOptions) => {
cdace0e5 115 switch (poolType) {
bb615bd0 116 case PoolTypes.fixed:
cdace0e5 117 switch (workerType) {
bb615bd0 118 case WorkerTypes.thread:
cdace0e5
JB
119 return new FixedThreadPool(
120 poolSize,
8a970421 121 './benchmarks/internal/thread-worker.mjs',
cdace0e5
JB
122 poolOptions
123 )
bb615bd0 124 case WorkerTypes.cluster:
cdace0e5
JB
125 return new FixedClusterPool(
126 poolSize,
8a970421 127 './benchmarks/internal/cluster-worker.mjs',
cdace0e5
JB
128 poolOptions
129 )
130 }
131 break
bb615bd0 132 case PoolTypes.dynamic:
cdace0e5 133 switch (workerType) {
bb615bd0 134 case WorkerTypes.thread:
cdace0e5
JB
135 return new DynamicThreadPool(
136 poolSize / 2,
137 poolSize * 3,
8a970421 138 './benchmarks/internal/thread-worker.mjs',
cdace0e5
JB
139 poolOptions
140 )
bb615bd0 141 case WorkerTypes.cluster:
cdace0e5
JB
142 return new DynamicClusterPool(
143 poolSize / 2,
144 poolSize * 3,
8a970421 145 './benchmarks/internal/cluster-worker.mjs',
cdace0e5
JB
146 poolOptions
147 )
148 }
149 break
150 }
151}