And increase coverage a bit.
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
Before to jump into each poolifier pool type, let highlight that **Node.js comes with a thread pool already**, the libuv thread pool where some particular tasks already run by default.
Please take a look at [which tasks run on the libuv thread pool](https://nodejs.org/en/docs/guides/dont-block-the-event-loop/#what-code-runs-on-the-worker-pool).
-Now **if your task runs on libuv thread pool**, you can try to:
+**If your task runs on libuv thread pool**, you can try to:
- Tune the libuv thread pool size setting the [UV_THREADPOOL_SIZE](https://nodejs.org/api/cli.html#cli_uv_threadpool_size_size)
const workers = generateWorkersArray(60)
-let nextWorkerIndex = 0
+let nextWorkerIndex
function chooseWorkerTernaryOffByOne () {
nextWorkerIndex =
nextWorkerIndex = 0
chooseWorkerTernaryWithNegation()
})
- .add('Ternary with PreChoosing', function () {
+ .add('Ternary with pre-choosing', function () {
nextWorkerIndex = 0
chooseWorkerTernaryWithPreChoosing()
})
const { runPoolifierTest } = require('../benchmark-utils')
const size = 30
+const numberOfTasks = 1
const dynamicPool = new DynamicClusterPool(
size / 2,
)
async function dynamicClusterTest (
- { tasks, workerData } = { tasks: 1, workerData: { proof: 'ok' } }
+ { tasks, workerData } = { tasks: numberOfTasks, workerData: { proof: 'ok' } }
) {
return runPoolifierTest(dynamicPool, { tasks, workerData })
}
async function dynamicClusterTestLessRecentlyUsed (
- { tasks, workerData } = { tasks: 1, workerData: { proof: 'ok' } }
+ { tasks, workerData } = { tasks: numberOfTasks, workerData: { proof: 'ok' } }
) {
return runPoolifierTest(dynamicPoolLessRecentlyUsed, { tasks, workerData })
}
const { runPoolifierTest } = require('../benchmark-utils')
const size = 30
+const numberOfTasks = 1
const fixedPool = new FixedClusterPool(
size,
)
async function fixedClusterTest (
- { tasks, workerData } = { tasks: 1, workerData: { proof: 'ok' } }
+ { tasks, workerData } = { tasks: numberOfTasks, workerData: { proof: 'ok' } }
) {
return runPoolifierTest(fixedPool, { tasks, workerData })
}
async function fixedClusterTestLessRecentlyUsed (
- { tasks, workerData } = { tasks: 1, workerData: { proof: 'ok' } }
+ { tasks, workerData } = { tasks: numberOfTasks, workerData: { proof: 'ok' } }
) {
return runPoolifierTest(fixedPoolLessRecentlyUsed, { tasks, workerData })
}
const { runPoolifierTest } = require('../benchmark-utils')
const size = 30
+const numberOfTasks = 1
const dynamicPool = new DynamicThreadPool(
size / 2,
)
async function dynamicThreadTest (
- { tasks, workerData } = { tasks: 1, workerData: { proof: 'ok' } }
+ { tasks, workerData } = { tasks: numberOfTasks, workerData: { proof: 'ok' } }
) {
return runPoolifierTest(dynamicPool, { tasks, workerData })
}
async function dynamicThreadTestLessRecentlyUsed (
- { tasks, workerData } = { tasks: 1, workerData: { proof: 'ok' } }
+ { tasks, workerData } = { tasks: numberOfTasks, workerData: { proof: 'ok' } }
) {
return runPoolifierTest(dynamicPoolLessRecentlyUsed, { tasks, workerData })
}
const { runPoolifierTest } = require('../benchmark-utils')
const size = 30
+const numberOfTasks = 1
const fixedPool = new FixedThreadPool(
size,
)
async function fixedThreadTest (
- { tasks, workerData } = { tasks: 1, workerData: { proof: 'ok' } }
+ { tasks, workerData } = { tasks: numberOfTasks, workerData: { proof: 'ok' } }
) {
return runPoolifierTest(fixedPool, { tasks, workerData })
}
async function fixedThreadTestLessRecentlyUsed (
- { tasks, workerData } = { tasks: 1, workerData: { proof: 'ok' } }
+ { tasks, workerData } = { tasks: numberOfTasks, workerData: { proof: 'ok' } }
) {
return runPoolifierTest(fixedPoolLessRecentlyUsed, { tasks, workerData })
}
})
return workerCreated
},
- opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
+ this.opts.workerChoiceStrategy
)
}
}
private checkPoolOptions (opts: PoolOptions<Worker>): void {
+ this.opts.workerChoiceStrategy =
+ opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
this.opts.enableEvents = opts.enableEvents ?? true
}
const expect = require('expect')
-const { FixedClusterPool, FixedThreadPool } = require('../../../lib/index')
+const {
+ FixedClusterPool,
+ FixedThreadPool,
+ WorkerChoiceStrategies
+} = require('../../../lib/index')
const expectedError = new Error('Worker could not be found in tasks map')
const numberOfWorkers = 1
)
expect(pool.opts.enableEvents).toEqual(true)
expect(pool.emitter).toBeDefined()
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
pool.destroy()
pool = new FixedThreadPool(
numberOfWorkers,
'./tests/worker-files/thread/testWorker.js',
{
+ workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED,
enableEvents: false
}
)
expect(pool.opts.enableEvents).toEqual(false)
expect(pool.emitter).toBeUndefined()
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.LESS_RECENTLY_USED
+ )
pool.destroy()
})
expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED')
})
+ it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
+ const min = 0
+ const max = 3
+ const pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
+ it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
+ const min = 0
+ const max = 3
+ const pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js'
+ )
+ pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
+ expect(pool.opts.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
+ // We need to clean up the resources after our test
+ await pool.destroy()
+ })
+
it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
const max = 3
const pool = new FixedThreadPool(
promises.push(pool.execute({ test: 'test' }))
}
await Promise.all(promises)
-
// We need to clean up the resources after our test
await pool.destroy()
})
promises.push(pool.execute({ test: 'test' }))
}
await Promise.all(promises)
-
// We need to clean up the resources after our test
await pool.destroy()
})