refactor: move helpers to utils.ts file
[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
024daf59 11export async function runTest (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
024daf59 32export function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) {
548140e6 33 if (max < min || max < 0 || min < 0) {
4af5c11a
JB
34 throw new RangeError('Invalid interval')
35 }
c2d7d79b 36 max = Math.floor(max)
4af5c11a 37 if (min != null && min !== 0) {
c2d7d79b 38 min = Math.ceil(min)
872585ea 39 return Math.floor(Math.random() * (max - min + 1)) + min
74750c7f 40 }
872585ea 41 return Math.floor(Math.random() * (max + 1))
74750c7f
JB
42}
43
cdace0e5
JB
44function jsonIntegerSerialization (n) {
45 for (let i = 0; i < n; i++) {
46 const o = {
47 a: i
48 }
49 JSON.stringify(o)
50 }
51}
52
bdacc2d2
JB
53/**
54 * Intentionally inefficient implementation.
7d82d90e
JB
55 * @param {number} n - The number of fibonacci numbers to generate.
56 * @returns {number} - The nth fibonacci number.
bdacc2d2 57 */
932da732 58function fibonacci (n) {
024daf59 59 if (n <= 1) return n
bdacc2d2
JB
60 return fibonacci(n - 1) + fibonacci(n - 2)
61}
62
7d82d90e
JB
63/**
64 * Intentionally inefficient implementation.
7d82d90e
JB
65 * @param {number} n - The number to calculate the factorial of.
66 * @returns {number} - The factorial of n.
67 */
932da732 68function factorial (n) {
7d82d90e
JB
69 if (n === 0) {
70 return 1
7d82d90e 71 }
965415bb 72 return factorial(n - 1) * n
7d82d90e
JB
73}
74
932da732 75function readWriteFiles (
670734fc
JB
76 n,
77 baseDirectory = `/tmp/poolifier-benchmarks/${crypto.randomInt(
78 281474976710655
79 )}`
80) {
81 if (fs.existsSync(baseDirectory) === true) {
82 fs.rmSync(baseDirectory, { recursive: true })
cdace0e5 83 }
670734fc 84 fs.mkdirSync(baseDirectory, { recursive: true })
cdace0e5
JB
85 for (let i = 0; i < n; i++) {
86 const filePath = `${baseDirectory}/${i}`
87 fs.writeFileSync(filePath, i.toString(), {
88 encoding: 'utf8',
89 flag: 'a'
90 })
91 fs.readFileSync(filePath, 'utf8')
92 }
670734fc 93 fs.rmSync(baseDirectory, { recursive: true })
cdace0e5
JB
94}
95
024daf59 96export function executeWorkerFunction (data) {
2d2e32c2
JB
97 switch (data.function) {
98 case WorkerFunctions.jsonIntegerSerialization:
d1a9aa41 99 return jsonIntegerSerialization(data.taskSize || 1000)
2d2e32c2 100 case WorkerFunctions.fibonacci:
d1a9aa41 101 return fibonacci(data.taskSize || 1000)
2d2e32c2 102 case WorkerFunctions.factorial:
d1a9aa41 103 return factorial(data.taskSize || 1000)
cdace0e5
JB
104 case WorkerFunctions.readWriteFiles:
105 return readWriteFiles(data.taskSize || 1000)
2d2e32c2
JB
106 default:
107 throw new Error('Unknown worker function')
108 }
109}
110
024daf59 111export function buildPool (workerType, poolType, poolSize, poolOptions) {
cdace0e5 112 switch (poolType) {
bb615bd0 113 case PoolTypes.fixed:
cdace0e5 114 switch (workerType) {
bb615bd0 115 case WorkerTypes.thread:
cdace0e5
JB
116 return new FixedThreadPool(
117 poolSize,
8a970421 118 './benchmarks/internal/thread-worker.mjs',
cdace0e5
JB
119 poolOptions
120 )
bb615bd0 121 case WorkerTypes.cluster:
cdace0e5
JB
122 return new FixedClusterPool(
123 poolSize,
8a970421 124 './benchmarks/internal/cluster-worker.mjs',
cdace0e5
JB
125 poolOptions
126 )
127 }
128 break
bb615bd0 129 case PoolTypes.dynamic:
cdace0e5 130 switch (workerType) {
bb615bd0 131 case WorkerTypes.thread:
cdace0e5
JB
132 return new DynamicThreadPool(
133 poolSize / 2,
134 poolSize * 3,
8a970421 135 './benchmarks/internal/thread-worker.mjs',
cdace0e5
JB
136 poolOptions
137 )
bb615bd0 138 case WorkerTypes.cluster:
cdace0e5
JB
139 return new DynamicClusterPool(
140 poolSize / 2,
141 poolSize * 3,
8a970421 142 './benchmarks/internal/cluster-worker.mjs',
cdace0e5
JB
143 poolOptions
144 )
145 }
146 break
147 }
148}