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 jsonIntegerSerialization = n => {
59 for (let i = 0; i < n; i++) {
69 * Intentionally inefficient implementation.
70 * @param {number} n - The number of fibonacci numbers to generate.
71 * @returns {number} - The nth fibonacci number.
73 const fibonacci = n => {
75 return fibonacci(n - 1) + fibonacci(n - 2)
79 * Intentionally inefficient implementation.
80 * @param {number} n - The number to calculate the factorial of.
81 * @returns {number} - The factorial of n.
83 const factorial = n => {
87 return factorial(n - 1) * n
90 const executeTaskFunction = data => {
91 switch (data.function) {
92 case TaskFunctions.jsonIntegerSerialization:
93 return jsonIntegerSerialization(data.n || 100)
94 case TaskFunctions.fibonacci:
95 return fibonacci(data.n || 25)
96 case TaskFunctions.factorial:
97 return factorial(data.n || 100)
99 throw new Error('Unknown worker function')
107 jsonIntegerSerialization,