import { isKillBehavior, KillBehaviors } from '../worker/worker-options'
import type { AbstractPoolWorker } from './abstract-pool-worker'
import type { PoolOptions } from './pool'
-import type { IPoolInternal } from './pool-internal'
+import type { IPoolInternal, TasksUsage } from './pool-internal'
import { PoolEmitter, PoolType } from './pool-internal'
import {
WorkerChoiceStrategies,
} from './selection-strategies/selection-strategies-types'
import { WorkerChoiceStrategyContext } from './selection-strategies/worker-choice-strategy-context'
+const WORKER_NOT_FOUND_TASKS_USAGE_MAP =
+ 'Worker could not be found in worker tasks usage map'
+
/**
* Base class containing some shared logic for all poolifier pools.
*
/** @inheritDoc */
public readonly workers: Worker[] = []
- /** @inheritDoc */
- public readonly tasks: Map<Worker, number> = new Map<Worker, number>()
+ /**
+ * The workers tasks usage map.
+ *
+ * `key`: The `Worker`
+ * `value`: Worker tasks usage statistics.
+ */
+ protected workersTasksUsage: Map<Worker, TasksUsage> = new Map<
+ Worker,
+ TasksUsage
+ >()
/** @inheritDoc */
public readonly emitter?: PoolEmitter
return this.promiseMap.size
}
+ /** @inheritDoc */
+ public getWorkerIndex (worker: Worker): number {
+ return this.workers.indexOf(worker)
+ }
+
/** @inheritDoc */
public getWorkerRunningTasks (worker: Worker): number | undefined {
- return this.tasks.get(worker)
+ return this.workersTasksUsage.get(worker)?.running
}
/** @inheritDoc */
- public getWorkerIndex (worker: Worker): number {
- return this.workers.indexOf(worker)
+ public getWorkerAverageTasksRunTime (worker: Worker): number | undefined {
+ return this.workersTasksUsage.get(worker)?.avgRunTime
}
/** @inheritDoc */
protected abstract isMain (): boolean
/**
- * Increase the number of tasks that the given worker has applied.
+ * Hook executed before the worker task promise resolution.
+ * Can be overridden.
*
- * @param worker Worker whose tasks are increased.
+ * @param worker The worker.
*/
- protected increaseWorkersTask (worker: Worker): void {
- this.stepWorkerNumberOfTasks(worker, 1)
+ protected beforePromiseWorkerResponseHook (worker: Worker): void {
+ this.increaseWorkerRunningTasks(worker)
}
/**
- * Decrease the number of tasks that the given worker has applied.
+ * Hook executed after the worker task promise resolution.
+ * Can be overridden.
*
- * @param worker Worker whose tasks are decreased.
+ * @param message The received message.
+ * @param promise The Promise response.
*/
- protected decreaseWorkersTasks (worker: Worker): void {
- this.stepWorkerNumberOfTasks(worker, -1)
- }
-
- /**
- * Step the number of tasks that the given worker has applied.
- *
- * @param worker Worker whose tasks are set.
- * @param step Worker number of tasks step.
- */
- private stepWorkerNumberOfTasks (worker: Worker, step: number): void {
- const numberOfTasksInProgress = this.tasks.get(worker)
- if (numberOfTasksInProgress !== undefined) {
- this.tasks.set(worker, numberOfTasksInProgress + step)
- } else {
- throw Error('Worker could not be found in tasks map')
- }
+ protected afterPromiseWorkerResponseHook (
+ message: MessageValue<Response>,
+ promise: PromiseWorkerResponseWrapper<Worker, Response>
+ ): void {
+ this.decreaseWorkerRunningTasks(promise.worker)
+ this.stepWorkerRunTasks(promise.worker, 1)
+ this.updateWorkerTasksRunTime(promise.worker, message.taskRunTime)
}
/**
protected removeWorker (worker: Worker): void {
// Clean worker from data structure
this.workers.splice(this.getWorkerIndex(worker), 1)
- this.tasks.delete(worker)
+ this.resetWorkerTasksUsage(worker)
}
/**
worker: Worker,
messageId: number
): Promise<Response> {
- this.increaseWorkersTask(worker)
+ this.beforePromiseWorkerResponseHook(worker)
return new Promise<Response>((resolve, reject) => {
this.promiseMap.set(messageId, { resolve, reject, worker })
})
this.workers.push(worker)
- // Init tasks map
- this.tasks.set(worker, 0)
+ // Init worker tasks usage map
+ this.workersTasksUsage.set(worker, {
+ run: 0,
+ running: 0,
+ runTime: 0,
+ avgRunTime: 0
+ })
this.afterWorkerSetup(worker)
if (message.id !== undefined) {
const promise = this.promiseMap.get(message.id)
if (promise !== undefined) {
- this.decreaseWorkersTasks(promise.worker)
+ this.afterPromiseWorkerResponseHook(message, promise)
if (message.error) promise.reject(message.error)
else promise.resolve(message.data as Response)
this.promiseMap.delete(message.id)
this.emitter?.emit('busy')
}
}
+
+ /**
+ * Increase the number of tasks that the given worker has applied.
+ *
+ * @param worker Worker which running tasks is increased.
+ */
+ private increaseWorkerRunningTasks (worker: Worker): void {
+ this.stepWorkerRunningTasks(worker, 1)
+ }
+
+ /**
+ * Decrease the number of tasks that the given worker has applied.
+ *
+ * @param worker Worker which running tasks is decreased.
+ */
+ private decreaseWorkerRunningTasks (worker: Worker): void {
+ this.stepWorkerRunningTasks(worker, -1)
+ }
+
+ /**
+ * Step the number of tasks that the given worker has applied.
+ *
+ * @param worker Worker which running tasks are stepped.
+ * @param step Number of running tasks step.
+ */
+ private stepWorkerRunningTasks (worker: Worker, step: number): void {
+ const tasksUsage = this.workersTasksUsage.get(worker)
+ if (tasksUsage !== undefined) {
+ tasksUsage.running = tasksUsage.running + step
+ this.workersTasksUsage.set(worker, tasksUsage)
+ } else {
+ throw new Error(WORKER_NOT_FOUND_TASKS_USAGE_MAP)
+ }
+ }
+
+ /**
+ * Step the number of tasks that the given worker has run.
+ *
+ * @param worker Worker which has run tasks.
+ * @param step Number of run tasks step.
+ */
+ private stepWorkerRunTasks (worker: Worker, step: number) {
+ const tasksUsage = this.workersTasksUsage.get(worker)
+ if (tasksUsage !== undefined) {
+ tasksUsage.run = tasksUsage.run + step
+ this.workersTasksUsage.set(worker, tasksUsage)
+ } else {
+ throw new Error(WORKER_NOT_FOUND_TASKS_USAGE_MAP)
+ }
+ }
+
+ /**
+ * Update tasks run time for the given worker.
+ *
+ * @param worker Worker which run the task.
+ * @param taskRunTime Worker task run time.
+ */
+ private updateWorkerTasksRunTime (
+ worker: Worker,
+ taskRunTime: number | undefined
+ ) {
+ const tasksUsage = this.workersTasksUsage.get(worker)
+ if (tasksUsage !== undefined && tasksUsage.run !== 0) {
+ tasksUsage.runTime += taskRunTime ?? 0
+ tasksUsage.avgRunTime = tasksUsage.runTime / tasksUsage.run
+ this.workersTasksUsage.set(worker, tasksUsage)
+ } else {
+ throw new Error(WORKER_NOT_FOUND_TASKS_USAGE_MAP)
+ }
+ }
+
+ /**
+ * Reset worker tasks usage statistics.
+ *
+ * @param worker The worker.
+ */
+ private resetWorkerTasksUsage (worker: Worker): void {
+ this.workersTasksUsage.delete(worker)
+ }
}
FixedThreadPool,
WorkerChoiceStrategies
} = require('../../../lib/index')
-const expectedError = new Error('Worker could not be found in tasks map')
const numberOfWorkers = 1
-class StubPoolWithTasksMapClear extends FixedThreadPool {
+const workerNotFoundInTasksUsageMapError = new Error(
+ 'Worker could not be found in worker tasks usage map'
+)
+
+class StubPoolWithWorkerTasksUsageMapClear extends FixedThreadPool {
removeAllWorker () {
- this.tasks.clear()
+ this.workersTasksUsage.clear()
}
}
}
describe('Abstract pool test suite', () => {
- it('Simulate worker not found during increaseWorkersTask', () => {
- const pool = new StubPoolWithTasksMapClear(
- numberOfWorkers,
- './tests/worker-files/thread/testWorker.js'
- )
- // Simulate worker not found.
- pool.removeAllWorker()
- expect(() => pool.increaseWorkersTask()).toThrowError(expectedError)
- pool.destroy()
- })
-
- it('Simulate worker not found during decreaseWorkersTasks', () => {
- const pool = new StubPoolWithTasksMapClear(
- numberOfWorkers,
- './tests/worker-files/thread/testWorker.js',
- {
- errorHandler: e => console.error(e)
- }
- )
- // Simulate worker not found.
- pool.removeAllWorker()
- expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError)
- pool.destroy()
- })
-
it('Simulate pool creation from a non main thread/process', () => {
expect(
() =>
pool.destroy()
})
+ it('Simulate worker not found during increaseWorkerRunningTasks', () => {
+ const pool = new StubPoolWithWorkerTasksUsageMapClear(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ // Simulate worker not found.
+ pool.removeAllWorker()
+ expect(() => pool.increaseWorkerRunningTasks()).toThrowError(
+ workerNotFoundInTasksUsageMapError
+ )
+ pool.destroy()
+ })
+
+ it('Simulate worker not found during decreaseWorkerRunningTasks', () => {
+ const pool = new StubPoolWithWorkerTasksUsageMapClear(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ // Simulate worker not found.
+ pool.removeAllWorker()
+ expect(() => pool.decreaseWorkerRunningTasks()).toThrowError(
+ workerNotFoundInTasksUsageMapError
+ )
+ pool.destroy()
+ })
+
+ it('Simulate worker not found during stepWorkerRunTasks', () => {
+ const pool = new StubPoolWithWorkerTasksUsageMapClear(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ // Simulate worker not found.
+ pool.removeAllWorker()
+ expect(() => pool.stepWorkerRunTasks()).toThrowError(
+ workerNotFoundInTasksUsageMapError
+ )
+ pool.destroy()
+ })
+
+ it('Simulate worker not found during updateWorkerTasksRunTime', () => {
+ const pool = new StubPoolWithWorkerTasksUsageMapClear(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ // Simulate worker not found.
+ pool.removeAllWorker()
+ expect(() => pool.updateWorkerTasksRunTime()).toThrowError(
+ workerNotFoundInTasksUsageMapError
+ )
+ pool.destroy()
+ })
+
+ it('Verify that worker pool tasks usage are initialized', () => {
+ const pool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ for (const tasksUsage of pool.workersTasksUsage.values()) {
+ expect(tasksUsage).toBeDefined()
+ expect(tasksUsage.run).toBe(0)
+ expect(tasksUsage.running).toBe(0)
+ expect(tasksUsage.runTime).toBe(0)
+ expect(tasksUsage.avgRunTime).toBe(0)
+ }
+ pool.destroy()
+ })
+
+ it('Verify that worker pool tasks usage are computed', async () => {
+ const pool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js'
+ )
+ const promises = []
+ for (let i = 0; i < numberOfWorkers * 2; i++) {
+ promises.push(pool.execute({ test: 'test' }))
+ }
+ for (const tasksUsage of pool.workersTasksUsage.values()) {
+ expect(tasksUsage).toBeDefined()
+ expect(tasksUsage.run).toBe(0)
+ expect(tasksUsage.running).toBe(numberOfWorkers * 2)
+ expect(tasksUsage.runTime).toBe(0)
+ expect(tasksUsage.avgRunTime).toBe(0)
+ }
+ await Promise.all(promises)
+ for (const tasksUsage of pool.workersTasksUsage.values()) {
+ expect(tasksUsage).toBeDefined()
+ expect(tasksUsage.run).toBe(numberOfWorkers * 2)
+ expect(tasksUsage.running).toBe(0)
+ expect(tasksUsage.runTime).toBeGreaterThanOrEqual(0)
+ expect(tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0)
+ }
+ pool.destroy()
+ })
+
it("Verify that pool event emitter 'busy' event can register a callback", async () => {
const pool = new FixedThreadPool(
numberOfWorkers,