1 const { TaskFunctions } = require('./test-types.cjs')
3 const waitWorkerEvents = async (pool, workerEvent, numberOfEventsToWait) => {
4 return await new Promise(resolve => {
6 if (numberOfEventsToWait === 0) {
10 for (const workerNode of pool.workerNodes) {
11 workerNode.worker.on(workerEvent, () => {
13 if (events === numberOfEventsToWait) {
21 const waitPoolEvents = async (pool, poolEvent, numberOfEventsToWait) => {
22 return await new Promise(resolve => {
24 if (numberOfEventsToWait === 0) {
28 pool.emitter?.on(poolEvent, () => {
30 if (events === numberOfEventsToWait) {
37 const sleep = async ms => {
38 return await new Promise(resolve => setTimeout(resolve, ms))
41 const sleepTaskFunction = async (
47 return await new Promise((resolve, reject) => {
51 ? reject(new Error(rejectionMessage))
58 const generateRandomInteger = (max = Number.MAX_SAFE_INTEGER, min = 0) => {
59 if (max < min || max < 0 || min < 0) {
60 throw new RangeError('Invalid interval')
63 if (min != null && min !== 0) {
65 return Math.floor(Math.random() * (max - min + 1)) + min
67 return Math.floor(Math.random() * (max + 1))
70 const jsonIntegerSerialization = n => {
71 for (let i = 0; i < n; i++) {
81 * Intentionally inefficient implementation.
82 * @param {number} n - The number of fibonacci numbers to generate.
83 * @returns {number} - The nth fibonacci number.
85 const fibonacci = n => {
87 return fibonacci(n - 1) + fibonacci(n - 2)
91 * Intentionally inefficient implementation.
92 * @param {number} n - The number to calculate the factorial of.
93 * @returns {number} - The factorial of n.
95 const factorial = n => {
99 return factorial(n - 1) * n
102 const executeTaskFunction = data => {
103 switch (data.function) {
104 case TaskFunctions.jsonIntegerSerialization:
105 return jsonIntegerSerialization(data.n || 100)
106 case TaskFunctions.fibonacci:
107 return fibonacci(data.n || 25)
108 case TaskFunctions.factorial:
109 return factorial(data.n || 100)
111 throw new Error('Unknown worker function')
119 generateRandomInteger,
120 jsonIntegerSerialization,