} from '../lib/index.mjs'
import { PoolTypes, WorkerFunctions, WorkerTypes } from './benchmarks-types.mjs'
-export async function runTest (pool, { taskExecutions, workerData }) {
+export const runTest = async (pool, { taskExecutions, workerData }) => {
return new Promise((resolve, reject) => {
let executions = 0
for (let i = 1; i <= taskExecutions; i++) {
})
}
-export function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) {
+export const generateRandomInteger = (
+ max = Number.MAX_SAFE_INTEGER,
+ min = 0
+) => {
if (max < min || max < 0 || min < 0) {
throw new RangeError('Invalid interval')
}
return Math.floor(Math.random() * (max + 1))
}
-function jsonIntegerSerialization (n) {
+const jsonIntegerSerialization = n => {
for (let i = 0; i < n; i++) {
const o = {
a: i
* @param {number} n - The number of fibonacci numbers to generate.
* @returns {number} - The nth fibonacci number.
*/
-function fibonacci (n) {
+const fibonacci = n => {
if (n <= 1) return n
return fibonacci(n - 1) + fibonacci(n - 2)
}
* @param {number} n - The number to calculate the factorial of.
* @returns {number} - The factorial of n.
*/
-function factorial (n) {
+const factorial = n => {
if (n === 0) {
return 1
}
return factorial(n - 1) * n
}
-function readWriteFiles (
+const readWriteFiles = (
n,
baseDirectory = `/tmp/poolifier-benchmarks/${crypto.randomInt(
281474976710655
)}`
-) {
+) => {
if (fs.existsSync(baseDirectory) === true) {
fs.rmSync(baseDirectory, { recursive: true })
}
fs.rmSync(baseDirectory, { recursive: true })
}
-export function executeWorkerFunction (data) {
+export const executeWorkerFunction = data => {
switch (data.function) {
case WorkerFunctions.jsonIntegerSerialization:
return jsonIntegerSerialization(data.taskSize || 1000)
}
}
-export function buildPool (workerType, poolType, poolSize, poolOptions) {
+export const buildPool = (workerType, poolType, poolSize, poolOptions) => {
switch (poolType) {
case PoolTypes.fixed:
switch (workerType) {
const { expect } = require('expect')
const { DynamicClusterPool, PoolEvents } = require('../../../lib')
const { WorkerFunctions } = require('../../test-types')
-const TestUtils = require('../../test-utils')
+const { sleep, waitWorkerEvents } = require('../../test-utils')
describe('Dynamic cluster pool test suite', () => {
const min = 1
// The `busy` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool.
// So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool.
expect(poolBusy).toBe(max + 1)
- const numberOfExitEvents = await TestUtils.waitWorkerEvents(
- pool,
- 'exit',
- max - min
- )
+ const numberOfExitEvents = await waitWorkerEvents(pool, 'exit', max - min)
expect(numberOfExitEvents).toBe(max - min)
})
pool.execute()
}
expect(pool.workerNodes.length).toBeGreaterThan(min)
- await TestUtils.waitWorkerEvents(pool, 'exit', max - min)
+ await waitWorkerEvents(pool, 'exit', max - min)
expect(pool.workerNodes.length).toBe(min)
for (let i = 0; i < max * 2; i++) {
pool.execute()
}
expect(pool.workerNodes.length).toBeGreaterThan(min)
- await TestUtils.waitWorkerEvents(pool, 'exit', max - min)
+ await waitWorkerEvents(pool, 'exit', max - min)
expect(pool.workerNodes.length).toBe(min)
})
it('Shutdown test', async () => {
- const exitPromise = TestUtils.waitWorkerEvents(pool, 'exit', min)
+ const exitPromise = waitWorkerEvents(pool, 'exit', min)
await pool.destroy()
const numberOfExitEvents = await exitPromise
expect(numberOfExitEvents).toBe(min)
longRunningPool.execute()
}
expect(longRunningPool.workerNodes.length).toBe(max)
- await TestUtils.waitWorkerEvents(longRunningPool, 'exit', max - min)
+ await waitWorkerEvents(longRunningPool, 'exit', max - min)
expect(longRunningPool.workerNodes.length).toBe(min)
expect(
longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get(
longRunningPool.execute()
}
expect(longRunningPool.workerNodes.length).toBe(max)
- await TestUtils.sleep(1500)
+ await sleep(1500)
// Here we expect the workerNodes to be at the max size since the task is still executing
expect(longRunningPool.workerNodes.length).toBe(max)
// We need to clean up the resources after our test
const { expect } = require('expect')
const { FixedClusterPool, PoolEvents } = require('../../../lib')
const { WorkerFunctions } = require('../../test-types')
-const TestUtils = require('../../test-utils')
+const { waitWorkerEvents } = require('../../test-utils')
describe('Fixed cluster pool test suite', () => {
const numberOfWorkers = 6
})
it('Shutdown test', async () => {
- const exitPromise = TestUtils.waitWorkerEvents(
- pool,
- 'exit',
- numberOfWorkers
- )
+ const exitPromise = waitWorkerEvents(pool, 'exit', numberOfWorkers)
await pool.destroy()
const numberOfExitEvents = await exitPromise
expect(numberOfExitEvents).toBe(numberOfWorkers)
const {
WeightedRoundRobinWorkerChoiceStrategy
} = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
-const TestUtils = require('../../test-utils')
+const { generateRandomInteger } = require('../../test-utils')
describe('Weighted round robin strategy worker choice strategy test suite', () => {
// const min = 1
it('Verify that reset() resets internals', () => {
const strategy = new WeightedRoundRobinWorkerChoiceStrategy(pool)
- strategy.currentWorkerId = TestUtils.generateRandomInteger(
- Number.MAX_SAFE_INTEGER,
- 1
- )
- strategy.workerVirtualTaskRunTime = TestUtils.generateRandomInteger(
+ strategy.currentWorkerId = generateRandomInteger(Number.MAX_SAFE_INTEGER, 1)
+ strategy.workerVirtualTaskRunTime = generateRandomInteger(
Number.MAX_SAFE_INTEGER,
1
)
const { expect } = require('expect')
const { DynamicThreadPool, PoolEvents } = require('../../../lib')
const { WorkerFunctions } = require('../../test-types')
-const TestUtils = require('../../test-utils')
+const { sleep, waitWorkerEvents } = require('../../test-utils')
describe('Dynamic thread pool test suite', () => {
const min = 1
// The `busy` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool.
// So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool.
expect(poolBusy).toBe(max + 1)
- const numberOfExitEvents = await TestUtils.waitWorkerEvents(
- pool,
- 'exit',
- max - min
- )
+ const numberOfExitEvents = await waitWorkerEvents(pool, 'exit', max - min)
expect(numberOfExitEvents).toBe(max - min)
})
pool.execute()
}
expect(pool.workerNodes.length).toBe(max)
- await TestUtils.waitWorkerEvents(pool, 'exit', max - min)
+ await waitWorkerEvents(pool, 'exit', max - min)
expect(pool.workerNodes.length).toBe(min)
for (let i = 0; i < max * 2; i++) {
pool.execute()
}
expect(pool.workerNodes.length).toBe(max)
- await TestUtils.waitWorkerEvents(pool, 'exit', max - min)
+ await waitWorkerEvents(pool, 'exit', max - min)
expect(pool.workerNodes.length).toBe(min)
})
it('Shutdown test', async () => {
- const exitPromise = TestUtils.waitWorkerEvents(pool, 'exit', min)
+ const exitPromise = waitWorkerEvents(pool, 'exit', min)
await pool.destroy()
const numberOfExitEvents = await exitPromise
expect(numberOfExitEvents).toBe(min)
longRunningPool.execute()
}
expect(longRunningPool.workerNodes.length).toBe(max)
- await TestUtils.waitWorkerEvents(longRunningPool, 'exit', max - min)
+ await waitWorkerEvents(longRunningPool, 'exit', max - min)
expect(longRunningPool.workerNodes.length).toBe(min)
expect(
longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get(
longRunningPool.execute()
}
expect(longRunningPool.workerNodes.length).toBe(max)
- await TestUtils.sleep(1500)
+ await sleep(1500)
// Here we expect the workerNodes to be at the max size since the task is still executing
expect(longRunningPool.workerNodes.length).toBe(max)
// We need to clean up the resources after our test
const { expect } = require('expect')
const { FixedThreadPool, PoolEvents } = require('../../../lib')
const { WorkerFunctions } = require('../../test-types')
-const TestUtils = require('../../test-utils')
+const { waitWorkerEvents } = require('../../test-utils')
describe('Fixed thread pool test suite', () => {
const numberOfThreads = 6
})
it('Shutdown test', async () => {
- const exitPromise = TestUtils.waitWorkerEvents(
- pool,
- 'exit',
- numberOfThreads
- )
+ const exitPromise = waitWorkerEvents(pool, 'exit', numberOfThreads)
await pool.destroy()
const numberOfExitEvents = await exitPromise
expect(numberOfExitEvents).toBe(numberOfThreads)
const { WorkerFunctions } = require('./test-types')
-class TestUtils {
- static async waitWorkerEvents (pool, workerEvent, numberOfEventsToWait) {
- return new Promise(resolve => {
- let events = 0
- if (numberOfEventsToWait === 0) {
- resolve(events)
- }
- for (const workerNode of pool.workerNodes) {
- workerNode.worker.on(workerEvent, () => {
- ++events
- if (events === numberOfEventsToWait) {
- resolve(events)
- }
- })
- }
- })
- }
-
- static async waitPoolEvents (pool, poolEvent, numberOfEventsToWait) {
- return new Promise(resolve => {
- let events = 0
- if (numberOfEventsToWait === 0) {
- resolve(events)
- }
- pool.emitter.on(poolEvent, () => {
+const waitWorkerEvents = async (pool, workerEvent, numberOfEventsToWait) => {
+ return new Promise(resolve => {
+ let events = 0
+ if (numberOfEventsToWait === 0) {
+ resolve(events)
+ }
+ for (const workerNode of pool.workerNodes) {
+ workerNode.worker.on(workerEvent, () => {
++events
if (events === numberOfEventsToWait) {
resolve(events)
}
})
+ }
+ })
+}
+
+const waitPoolEvents = async (pool, poolEvent, numberOfEventsToWait) => {
+ return new Promise(resolve => {
+ let events = 0
+ if (numberOfEventsToWait === 0) {
+ resolve(events)
+ }
+ pool.emitter.on(poolEvent, () => {
+ ++events
+ if (events === numberOfEventsToWait) {
+ resolve(events)
+ }
})
- }
+ })
+}
- static async sleep (ms) {
- return new Promise(resolve => setTimeout(resolve, ms))
- }
+const sleep = async ms => {
+ return new Promise(resolve => setTimeout(resolve, ms))
+}
- static async sleepWorkerFunction (
- data,
- ms,
- rejection = false,
- rejectionMessage = ''
- ) {
- return new Promise((resolve, reject) => {
- setTimeout(
- () =>
- rejection === true
- ? reject(new Error(rejectionMessage))
- : resolve(data),
- ms
- )
- })
- }
+const sleepWorkerFunction = async (
+ data,
+ ms,
+ rejection = false,
+ rejectionMessage = ''
+) => {
+ return new Promise((resolve, reject) => {
+ setTimeout(
+ () =>
+ rejection === true
+ ? reject(new Error(rejectionMessage))
+ : resolve(data),
+ ms
+ )
+ })
+}
- static generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) {
- if (max < min || max < 0 || min < 0) {
- throw new RangeError('Invalid interval')
- }
- max = Math.floor(max)
- if (min != null && min !== 0) {
- min = Math.ceil(min)
- return Math.floor(Math.random() * (max - min + 1)) + min
- }
- return Math.floor(Math.random() * (max + 1))
+const generateRandomInteger = (max = Number.MAX_SAFE_INTEGER, min = 0) => {
+ if (max < min || max < 0 || min < 0) {
+ throw new RangeError('Invalid interval')
}
+ max = Math.floor(max)
+ if (min != null && min !== 0) {
+ min = Math.ceil(min)
+ return Math.floor(Math.random() * (max - min + 1)) + min
+ }
+ return Math.floor(Math.random() * (max + 1))
+}
- static jsonIntegerSerialization (n) {
- for (let i = 0; i < n; i++) {
- const o = {
- a: i
- }
- JSON.stringify(o)
+const jsonIntegerSerialization = n => {
+ for (let i = 0; i < n; i++) {
+ const o = {
+ a: i
}
+ JSON.stringify(o)
}
+}
- /**
- * Intentionally inefficient implementation.
- * @param {number} n - The number of fibonacci numbers to generate.
- * @returns {number} - The nth fibonacci number.
- */
- static fibonacci (n) {
- if (n <= 1) return n
- return TestUtils.fibonacci(n - 1) + TestUtils.fibonacci(n - 2)
- }
+/**
+ * Intentionally inefficient implementation.
+ * @param {number} n - The number of fibonacci numbers to generate.
+ * @returns {number} - The nth fibonacci number.
+ */
+const fibonacci = n => {
+ if (n <= 1) return n
+ return fibonacci(n - 1) + fibonacci(n - 2)
+}
- /**
- * Intentionally inefficient implementation.
- * @param {number} n - The number to calculate the factorial of.
- * @returns {number} - The factorial of n.
- */
- static factorial (n) {
- if (n === 0) {
- return 1
- }
- return TestUtils.factorial(n - 1) * n
+/**
+ * Intentionally inefficient implementation.
+ * @param {number} n - The number to calculate the factorial of.
+ * @returns {number} - The factorial of n.
+ */
+const factorial = n => {
+ if (n === 0) {
+ return 1
}
+ return factorial(n - 1) * n
+}
- static executeWorkerFunction (data) {
- switch (data.function) {
- case WorkerFunctions.jsonIntegerSerialization:
- return TestUtils.jsonIntegerSerialization(data.n || 100)
- case WorkerFunctions.fibonacci:
- return TestUtils.fibonacci(data.n || 25)
- case WorkerFunctions.factorial:
- return TestUtils.factorial(data.n || 100)
- default:
- throw new Error('Unknown worker function')
- }
+const executeWorkerFunction = data => {
+ switch (data.function) {
+ case WorkerFunctions.jsonIntegerSerialization:
+ return jsonIntegerSerialization(data.n || 100)
+ case WorkerFunctions.fibonacci:
+ return fibonacci(data.n || 25)
+ case WorkerFunctions.factorial:
+ return factorial(data.n || 100)
+ default:
+ throw new Error('Unknown worker function')
}
}
-module.exports = TestUtils
+module.exports = {
+ executeWorkerFunction,
+ factorial,
+ fibonacci,
+ generateRandomInteger,
+ jsonIntegerSerialization,
+ sleep,
+ sleepWorkerFunction,
+ waitWorkerEvents,
+ waitPoolEvents
+}
'use strict'
const { ClusterWorker, KillBehaviors } = require('../../../lib')
-const TestUtils = require('../../test-utils')
+const { sleepWorkerFunction } = require('../../test-utils')
async function error (data) {
- return TestUtils.sleepWorkerFunction(
+ return sleepWorkerFunction(
data,
2000,
true,
'use strict'
const { ClusterWorker, KillBehaviors } = require('../../../lib')
-const TestUtils = require('../../test-utils')
+const { sleepWorkerFunction } = require('../../test-utils')
async function sleep (data) {
- return TestUtils.sleepWorkerFunction(data, 2000)
+ return sleepWorkerFunction(data, 2000)
}
module.exports = new ClusterWorker(sleep, {
'use strict'
const { ClusterWorker, KillBehaviors } = require('../../../lib')
-const TestUtils = require('../../test-utils')
+const { sleepWorkerFunction } = require('../../test-utils')
async function sleep (data) {
- return TestUtils.sleepWorkerFunction(data, 50000)
+ return sleepWorkerFunction(data, 50000)
}
module.exports = new ClusterWorker(sleep, {
'use strict'
const { ClusterWorker } = require('../../../lib')
-const TestUtils = require('../../test-utils')
+const { sleepWorkerFunction } = require('../../test-utils')
async function sleep (data) {
- return TestUtils.sleepWorkerFunction(data, 50000)
+ return sleepWorkerFunction(data, 50000)
}
module.exports = new ClusterWorker(sleep, {
'use strict'
const { isMaster } = require('cluster')
const { ClusterWorker, KillBehaviors } = require('../../../lib')
-const TestUtils = require('../../test-utils')
+const { executeWorkerFunction } = require('../../test-utils')
const { WorkerFunctions } = require('../../test-types')
function test (data) {
data = data || {}
data.function = data.function || WorkerFunctions.jsonIntegerSerialization
- const result = TestUtils.executeWorkerFunction(data)
+ const result = executeWorkerFunction(data)
if (result == null) {
return isMaster
}
'use strict'
const { ThreadWorker, KillBehaviors } = require('../../../lib')
-const TestUtils = require('../../test-utils')
+const { sleepWorkerFunction } = require('../../test-utils')
async function error (data) {
- return TestUtils.sleepWorkerFunction(
+ return sleepWorkerFunction(
data,
2000,
true,
'use strict'
const { ThreadWorker, KillBehaviors } = require('../../../lib')
-const TestUtils = require('../../test-utils')
+const { sleepWorkerFunction } = require('../../test-utils')
async function sleep (data) {
- return TestUtils.sleepWorkerFunction(data, 2000)
+ return sleepWorkerFunction(data, 2000)
}
module.exports = new ThreadWorker(sleep, {
'use strict'
const { ThreadWorker, KillBehaviors } = require('../../../lib')
-const TestUtils = require('../../test-utils')
+const { sleepWorkerFunction } = require('../../test-utils')
async function sleep (data) {
- return TestUtils.sleepWorkerFunction(data, 50000)
+ return sleepWorkerFunction(data, 50000)
}
module.exports = new ThreadWorker(sleep, {
'use strict'
const { ThreadWorker } = require('../../../lib')
-const TestUtils = require('../../test-utils')
+const { sleepWorkerFunction } = require('../../test-utils')
async function sleep (data) {
- return TestUtils.sleepWorkerFunction(data, 50000)
+ return sleepWorkerFunction(data, 50000)
}
module.exports = new ThreadWorker(sleep, {
'use strict'
const { isMainThread } = require('worker_threads')
const { ThreadWorker, KillBehaviors } = require('../../../lib')
-const TestUtils = require('../../test-utils')
+const { executeWorkerFunction } = require('../../test-utils')
const { WorkerFunctions } = require('../../test-types')
function test (data) {
data = data || {}
data.function = data.function || WorkerFunctions.jsonIntegerSerialization
- const result = TestUtils.executeWorkerFunction(data)
+ const result = executeWorkerFunction(data)
if (result == null) {
return isMainThread
}