From f3a91bac2d5c34013f6394ebbe3576569b0cfcc0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 3 Sep 2023 11:07:57 +0200 Subject: [PATCH] feat: optimize worker choice strategies implementation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 4 + src/pools/abstract-pool.ts | 2 +- .../abstract-worker-choice-strategy.ts | 30 ++--- .../fair-share-worker-choice-strategy.ts | 90 +++++++------- ...hted-round-robin-worker-choice-strategy.ts | 18 +-- .../least-busy-worker-choice-strategy.ts | 26 ++-- .../least-elu-worker-choice-strategy.ts | 23 ++-- .../least-used-worker-choice-strategy.ts | 30 ++--- ...hted-round-robin-worker-choice-strategy.ts | 18 +-- src/pools/worker-node.ts | 3 + src/pools/worker.ts | 12 ++ .../selection-strategies.test.js | 113 ++++-------------- ...round-robin-worker-choice-strategy.test.js | 2 +- typedoc.json | 2 +- 14 files changed, 163 insertions(+), 210 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ad7fe48..fed363d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- Optimize worker choice strategies implementation. + ## [2.6.40] - 2023-09-01 ### Fixed diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index a9632d87..534cb490 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1372,6 +1372,7 @@ export abstract class AbstractPool< } const workerNodeKey = promiseResponse.workerNodeKey this.afterTaskExecutionHook(workerNodeKey, message) + this.workerChoiceStrategyContext.update(workerNodeKey) this.promiseResponseMap.delete(taskId as string) if ( this.opts.enableTasksQueue === true && @@ -1384,7 +1385,6 @@ export abstract class AbstractPool< this.dequeueTask(workerNodeKey) as Task ) } - this.workerChoiceStrategyContext.update(workerNodeKey) } } diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 3c41dd6f..999d9c86 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -153,45 +153,45 @@ export abstract class AbstractWorkerChoiceStrategy< } /** - * Gets the worker task runtime. + * Gets the worker node task runtime. * If the task statistics require the average runtime, the average runtime is returned. * If the task statistics require the median runtime , the median runtime is returned. * * @param workerNodeKey - The worker node key. - * @returns The worker task runtime. + * @returns The worker node task runtime. */ - protected getWorkerTaskRunTime (workerNodeKey: number): number { + protected getWorkerNodeTaskRunTime (workerNodeKey: number): number { return this.taskStatisticsRequirements.runTime.median - ? this.pool.workerNodes[workerNodeKey].usage.runTime?.median ?? 0 - : this.pool.workerNodes[workerNodeKey].usage.runTime?.average ?? 0 + ? this.pool.workerNodes[workerNodeKey]?.usage?.runTime?.median ?? 0 + : this.pool.workerNodes[workerNodeKey]?.usage?.runTime?.average ?? 0 } /** - * Gets the worker task wait time. + * Gets the worker node task wait time. * If the task statistics require the average wait time, the average wait time is returned. * If the task statistics require the median wait time, the median wait time is returned. * * @param workerNodeKey - The worker node key. - * @returns The worker task wait time. + * @returns The worker node task wait time. */ - protected getWorkerTaskWaitTime (workerNodeKey: number): number { + protected getWorkerNodeTaskWaitTime (workerNodeKey: number): number { return this.taskStatisticsRequirements.waitTime.median - ? this.pool.workerNodes[workerNodeKey].usage.waitTime?.median ?? 0 - : this.pool.workerNodes[workerNodeKey].usage.waitTime?.average ?? 0 + ? this.pool.workerNodes[workerNodeKey]?.usage?.waitTime?.median ?? 0 + : this.pool.workerNodes[workerNodeKey]?.usage?.waitTime?.average ?? 0 } /** - * Gets the worker task ELU. + * Gets the worker node task ELU. * If the task statistics require the average ELU, the average ELU is returned. * If the task statistics require the median ELU, the median ELU is returned. * * @param workerNodeKey - The worker node key. - * @returns The worker task ELU. + * @returns The worker node task ELU. */ - protected getWorkerTaskElu (workerNodeKey: number): number { + protected getWorkerNodeTaskElu (workerNodeKey: number): number { return this.taskStatisticsRequirements.elu.median - ? this.pool.workerNodes[workerNodeKey].usage.elu.active?.median ?? 0 - : this.pool.workerNodes[workerNodeKey].usage.elu.active?.average ?? 0 + ? this.pool.workerNodes[workerNodeKey]?.usage?.elu?.active?.median ?? 0 + : this.pool.workerNodes[workerNodeKey]?.usage?.elu?.active?.average ?? 0 } /** diff --git a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts index c4a0d30b..af3fcfa0 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -3,7 +3,7 @@ import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' import type { IPool } from '../pool' -import type { IWorker } from '../worker' +import type { IWorker, StrategyData } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import { type IWorkerChoiceStrategy, @@ -42,11 +42,6 @@ export class FairShareWorkerChoiceStrategy< } } - /** - * Workers' virtual task end execution timestamp. - */ - private workersVirtualTaskEndTimestamp: number[] = [] - /** @inheritDoc */ public constructor ( pool: IPool, @@ -58,13 +53,18 @@ export class FairShareWorkerChoiceStrategy< /** @inheritDoc */ public reset (): boolean { - this.workersVirtualTaskEndTimestamp = [] + for (const workerNode of this.pool.workerNodes) { + delete workerNode.strategyData?.virtualTaskEndTimestamp + } return true } /** @inheritDoc */ public update (workerNodeKey: number): boolean { - this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey) + this.pool.workerNodes[workerNodeKey].strategyData = { + virtualTaskEndTimestamp: + this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey) + } return true } @@ -76,56 +76,64 @@ export class FairShareWorkerChoiceStrategy< } /** @inheritDoc */ - public remove (workerNodeKey: number): boolean { - this.workersVirtualTaskEndTimestamp.splice(workerNodeKey, 1) + public remove (): boolean { return true } private fairShareNextWorkerNodeKey (): number | undefined { - let chosenWorkerNodeKey: number | undefined - let minWorkerVirtualTaskEndTimestamp = Infinity - for (const [workerNodeKey] of this.pool.workerNodes.entries()) { - if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) { - this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey) - } - const workerVirtualTaskEndTimestamp = - this.workersVirtualTaskEndTimestamp[workerNodeKey] - if (workerVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp) { - minWorkerVirtualTaskEndTimestamp = workerVirtualTaskEndTimestamp - chosenWorkerNodeKey = workerNodeKey - } - } - return chosenWorkerNodeKey + return this.pool.workerNodes.reduce( + (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { + if (workerNode.strategyData?.virtualTaskEndTimestamp == null) { + workerNode.strategyData = { + virtualTaskEndTimestamp: + this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey) + } + } + return (workerNode.strategyData.virtualTaskEndTimestamp as number) < + ((workerNodes[minWorkerNodeKey].strategyData as StrategyData) + .virtualTaskEndTimestamp as number) + ? workerNodeKey + : minWorkerNodeKey + }, + 0 + ) } /** * Computes the worker node key virtual task end timestamp. * * @param workerNodeKey - The worker node key. + * @returns The worker node key virtual task end timestamp. */ - private computeWorkerVirtualTaskEndTimestamp (workerNodeKey: number): void { - this.workersVirtualTaskEndTimestamp[workerNodeKey] = - this.getWorkerVirtualTaskEndTimestamp( - workerNodeKey, - this.getWorkerVirtualTaskStartTimestamp(workerNodeKey) - ) + private computeWorkerNodeVirtualTaskEndTimestamp ( + workerNodeKey: number + ): number { + return this.getWorkerNodeVirtualTaskEndTimestamp( + workerNodeKey, + this.getWorkerNodeVirtualTaskStartTimestamp(workerNodeKey) + ) } - private getWorkerVirtualTaskEndTimestamp ( + private getWorkerNodeVirtualTaskEndTimestamp ( workerNodeKey: number, - workerVirtualTaskStartTimestamp: number + workerNodeVirtualTaskStartTimestamp: number ): number { - const workerTaskRunTime = + const workerNodeTaskRunTime = this.opts.measurement === Measurements.elu - ? this.getWorkerTaskElu(workerNodeKey) - : this.getWorkerTaskRunTime(workerNodeKey) - return workerVirtualTaskStartTimestamp + workerTaskRunTime + ? this.getWorkerNodeTaskElu(workerNodeKey) + : this.getWorkerNodeTaskRunTime(workerNodeKey) + return workerNodeVirtualTaskStartTimestamp + workerNodeTaskRunTime } - private getWorkerVirtualTaskStartTimestamp (workerNodeKey: number): number { - return Math.max( - performance.now(), - this.workersVirtualTaskEndTimestamp[workerNodeKey] ?? -Infinity - ) + private getWorkerNodeVirtualTaskStartTimestamp ( + workerNodeKey: number + ): number { + const now = performance.now() + return now < + (this.pool.workerNodes[workerNodeKey]?.strategyData + ?.virtualTaskEndTimestamp ?? -Infinity) + ? (this.pool.workerNodes[workerNodeKey]?.strategyData + ?.virtualTaskEndTimestamp as number) + : now } } diff --git a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts index 7fba0f1b..b034cba1 100644 --- a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts @@ -53,9 +53,9 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< */ private workerNodeId: number = 0 /** - * Worker virtual task runtime. + * Worker node virtual task runtime. */ - private workerVirtualTaskRunTime: number = 0 + private workerNodeVirtualTaskRunTime: number = 0 /** @inheritDoc */ public constructor ( @@ -73,7 +73,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< this.resetWorkerNodeKeyProperties() this.roundId = 0 this.workerNodeId = 0 - this.workerVirtualTaskRunTime = 0 + this.workerNodeVirtualTaskRunTime = 0 return true } @@ -98,19 +98,19 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< this.workerNodeId = workerNodeKey if ( this.workerNodeId !== this.nextWorkerNodeKey && - this.workerVirtualTaskRunTime !== 0 + this.workerNodeVirtualTaskRunTime !== 0 ) { - this.workerVirtualTaskRunTime = 0 + this.workerNodeVirtualTaskRunTime = 0 } const workerWeight = this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight if ( workerWeight >= this.roundWeights[roundIndex] && - this.workerVirtualTaskRunTime < workerWeight + this.workerNodeVirtualTaskRunTime < workerWeight ) { - this.workerVirtualTaskRunTime = - this.workerVirtualTaskRunTime + - this.getWorkerTaskRunTime(workerNodeKey) + this.workerNodeVirtualTaskRunTime = + this.workerNodeVirtualTaskRunTime + + this.getWorkerNodeTaskRunTime(workerNodeKey) this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) this.nextWorkerNodeKey = workerNodeKey return this.nextWorkerNodeKey diff --git a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts index 0e8fdef3..91fbfc78 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -72,20 +72,16 @@ export class LeastBusyWorkerChoiceStrategy< } private leastBusyNextWorkerNodeKey (): number | undefined { - let chosenWorkerNodeKey: number | undefined - let minTime = Infinity - for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const workerTime = - (workerNode.usage.runTime?.aggregate ?? 0) + - (workerNode.usage.waitTime?.aggregate ?? 0) - if (workerTime === 0) { - chosenWorkerNodeKey = workerNodeKey - break - } else if (workerTime < minTime) { - minTime = workerTime - chosenWorkerNodeKey = workerNodeKey - } - } - return chosenWorkerNodeKey + return this.pool.workerNodes.reduce( + (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { + return (workerNode.usage.runTime?.aggregate ?? 0) + + (workerNode.usage.waitTime?.aggregate ?? 0) < + (workerNodes[minWorkerNodeKey].usage.runTime?.aggregate ?? 0) + + (workerNodes[minWorkerNodeKey].usage.waitTime?.aggregate ?? 0) + ? workerNodeKey + : minWorkerNodeKey + }, + 0 + ) } } diff --git a/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts b/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts index 2624e5b2..67a1fdf3 100644 --- a/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts @@ -68,19 +68,14 @@ export class LeastEluWorkerChoiceStrategy< } private leastEluNextWorkerNodeKey (): number | undefined { - let chosenWorkerNodeKey: number | undefined - let minWorkerElu = Infinity - for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const workerUsage = workerNode.usage - const workerElu = workerUsage.elu?.active?.aggregate ?? 0 - if (workerElu === 0) { - chosenWorkerNodeKey = workerNodeKey - break - } else if (workerElu < minWorkerElu) { - minWorkerElu = workerElu - chosenWorkerNodeKey = workerNodeKey - } - } - return chosenWorkerNodeKey + return this.pool.workerNodes.reduce( + (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { + return (workerNode.usage.elu?.active?.aggregate ?? 0) < + (workerNodes[minWorkerNodeKey].usage.elu?.active?.aggregate ?? 0) + ? workerNodeKey + : minWorkerNodeKey + }, + 0 + ) } } diff --git a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts index d20c3217..dc249e65 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -53,22 +53,18 @@ export class LeastUsedWorkerChoiceStrategy< } private leastUsedNextWorkerNodeKey (): number | undefined { - let chosenWorkerNodeKey: number | undefined - let minNumberOfTasks = Infinity - for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const workerTaskStatistics = workerNode.usage.tasks - const workerTasks = - workerTaskStatistics.executed + - workerTaskStatistics.executing + - workerTaskStatistics.queued - if (workerTasks === 0) { - chosenWorkerNodeKey = workerNodeKey - break - } else if (workerTasks < minNumberOfTasks) { - minNumberOfTasks = workerTasks - chosenWorkerNodeKey = workerNodeKey - } - } - return chosenWorkerNodeKey + return this.pool.workerNodes.reduce( + (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { + return workerNode.usage.tasks.executed + + workerNode.usage.tasks.executing + + workerNode.usage.tasks.queued < + workerNodes[minWorkerNodeKey].usage.tasks.executed + + workerNodes[minWorkerNodeKey].usage.tasks.executing + + workerNodes[minWorkerNodeKey].usage.tasks.queued + ? workerNodeKey + : minWorkerNodeKey + }, + 0 + ) } } diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index b023bfb0..b9b74b6c 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -42,9 +42,9 @@ export class WeightedRoundRobinWorkerChoiceStrategy< */ private readonly defaultWorkerWeight: number /** - * Worker virtual task runtime. + * Worker node virtual task runtime. */ - private workerVirtualTaskRunTime: number = 0 + private workerNodeVirtualTaskRunTime: number = 0 /** @inheritDoc */ public constructor ( @@ -59,7 +59,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public reset (): boolean { this.resetWorkerNodeKeyProperties() - this.workerVirtualTaskRunTime = 0 + this.workerNodeVirtualTaskRunTime = 0 return true } @@ -80,7 +80,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< this.reset() } if (this.nextWorkerNodeKey === workerNodeKey) { - this.workerVirtualTaskRunTime = 0 + this.workerNodeVirtualTaskRunTime = 0 if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) { this.nextWorkerNodeKey = this.pool.workerNodes.length - 1 } @@ -99,10 +99,10 @@ export class WeightedRoundRobinWorkerChoiceStrategy< this.opts.weights?.[ this.nextWorkerNodeKey ?? this.previousWorkerNodeKey ] ?? this.defaultWorkerWeight - if (this.workerVirtualTaskRunTime < workerWeight) { - this.workerVirtualTaskRunTime = - this.workerVirtualTaskRunTime + - this.getWorkerTaskRunTime( + if (this.workerNodeVirtualTaskRunTime < workerWeight) { + this.workerNodeVirtualTaskRunTime = + this.workerNodeVirtualTaskRunTime + + this.getWorkerNodeTaskRunTime( this.nextWorkerNodeKey ?? this.previousWorkerNodeKey ) } else { @@ -110,7 +110,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 ? 0 : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1 - this.workerVirtualTaskRunTime = 0 + this.workerNodeVirtualTaskRunTime = 0 } return this.nextWorkerNodeKey } diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 387d05f9..ca275ded 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -13,6 +13,7 @@ import { Deque } from '../deque' import { type IWorker, type IWorkerNode, + type StrategyData, type WorkerInfo, type WorkerNodeEventCallback, type WorkerType, @@ -35,6 +36,8 @@ implements IWorkerNode { /** @inheritdoc */ public usage: WorkerUsage /** @inheritdoc */ + public strategyData?: StrategyData + /** @inheritdoc */ public messageChannel?: MessageChannel /** @inheritdoc */ public tasksQueueBackPressureSize: number diff --git a/src/pools/worker.ts b/src/pools/worker.ts index 5cd58b28..456bf785 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -171,6 +171,13 @@ export interface WorkerUsage { readonly elu: EventLoopUtilizationMeasurementStatistics } +/** + * Worker strategy data. + */ +export interface StrategyData { + virtualTaskEndTimestamp?: number +} + /** * Worker interface. */ @@ -227,6 +234,11 @@ export interface IWorkerNode { * Worker usage statistics. */ readonly usage: WorkerUsage + /** + * Worker strategy data. + * This is used to store data that is specific to the worker choice strategy. + */ + strategyData?: StrategyData /** * Message channel (worker_threads only). */ diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index 51478a06..ab71e516 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -123,13 +123,7 @@ describe('Selection strategies test suite', () => { workerChoiceStrategy ).previousWorkerNodeKey ).toBe(0) - if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) { - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategy - ).workersVirtualTaskEndTimestamp - ).toStrictEqual([]) - } else if ( + if ( workerChoiceStrategy === WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN ) { expect( @@ -140,7 +134,7 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( workerChoiceStrategy - ).workerVirtualTaskRunTime + ).workerNodeVirtualTaskRunTime ).toBe(0) } else if ( workerChoiceStrategy === @@ -154,7 +148,7 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( workerChoiceStrategy - ).workerVirtualTaskRunTime + ).workerNodeVirtualTaskRunTime ).toBe(0) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( @@ -1265,11 +1259,6 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.workerChoiceStrategy ).previousWorkerNodeKey ).toEqual(expect.any(Number)) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - pool.workerChoiceStrategyContext.workerChoiceStrategy - ).workersVirtualTaskEndTimestamp.length - ).toBe(pool.workerNodes.length) // We need to clean up the resources after our test await pool.destroy() }) @@ -1354,11 +1343,6 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.workerChoiceStrategy ).previousWorkerNodeKey ).toEqual(expect.any(Number)) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - pool.workerChoiceStrategyContext.workerChoiceStrategy - ).workersVirtualTaskEndTimestamp.length - ).toBe(pool.workerNodes.length) // We need to clean up the resources after our test await pool.destroy() }) @@ -1448,11 +1432,6 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.workerChoiceStrategy ).previousWorkerNodeKey ).toEqual(expect.any(Number)) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - pool.workerChoiceStrategyContext.workerChoiceStrategy - ).workersVirtualTaskEndTimestamp.length - ).toBe(pool.workerNodes.length) // We need to clean up the resources after our test await pool.destroy() }) @@ -1463,70 +1442,30 @@ describe('Selection strategies test suite', () => { max, './tests/worker-files/thread/testWorker.js' ) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategy - ).workersVirtualTaskEndTimestamp - ).toBeInstanceOf(Array) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategy - ).workersVirtualTaskEndTimestamp.length - ).toBe(0) - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategy - ).workersVirtualTaskEndTimestamp[0] = performance.now() - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategy - ).workersVirtualTaskEndTimestamp.length - ).toBe(1) + for (const workerNode of pool.workerNodes) { + workerNode.strategyData = { + virtualTaskEndTimestamp: performance.now() + } + } pool.setWorkerChoiceStrategy(workerChoiceStrategy) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - pool.workerChoiceStrategyContext.workerChoiceStrategy - ).workersVirtualTaskEndTimestamp - ).toBeInstanceOf(Array) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - pool.workerChoiceStrategyContext.workerChoiceStrategy - ).workersVirtualTaskEndTimestamp.length - ).toBe(0) + for (const workerNode of pool.workerNodes) { + expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeUndefined() + } await pool.destroy() pool = new DynamicThreadPool( min, max, './tests/worker-files/thread/testWorker.js' ) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategy - ).workersVirtualTaskEndTimestamp - ).toBeInstanceOf(Array) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategy - ).workersVirtualTaskEndTimestamp.length - ).toBe(0) - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategy - ).workersVirtualTaskEndTimestamp[0] = performance.now() - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategy - ).workersVirtualTaskEndTimestamp.length - ).toBe(1) + for (const workerNode of pool.workerNodes) { + workerNode.strategyData = { + virtualTaskEndTimestamp: performance.now() + } + } pool.setWorkerChoiceStrategy(workerChoiceStrategy) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - pool.workerChoiceStrategyContext.workerChoiceStrategy - ).workersVirtualTaskEndTimestamp - ).toBeInstanceOf(Array) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - pool.workerChoiceStrategyContext.workerChoiceStrategy - ).workersVirtualTaskEndTimestamp.length - ).toBe(0) + for (const workerNode of pool.workerNodes) { + expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeUndefined() + } // We need to clean up the resources after our test await pool.destroy() }) @@ -1684,7 +1623,7 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( pool.workerChoiceStrategyContext.workerChoiceStrategy - ).workerVirtualTaskRunTime + ).workerNodeVirtualTaskRunTime ).toBeGreaterThanOrEqual(0) // We need to clean up the resources after our test await pool.destroy() @@ -1762,7 +1701,7 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( pool.workerChoiceStrategyContext.workerChoiceStrategy - ).workerVirtualTaskRunTime + ).workerNodeVirtualTaskRunTime ).toBeGreaterThanOrEqual(0) // We need to clean up the resources after our test await pool.destroy() @@ -1845,7 +1784,7 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( pool.workerChoiceStrategyContext.workerChoiceStrategy - ).workerVirtualTaskRunTime + ).workerNodeVirtualTaskRunTime ).toBeGreaterThanOrEqual(0) // We need to clean up the resources after our test await pool.destroy() @@ -1875,7 +1814,7 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( workerChoiceStrategy - ).workerVirtualTaskRunTime + ).workerNodeVirtualTaskRunTime ).toBeDefined() pool.setWorkerChoiceStrategy(workerChoiceStrategy) expect( @@ -1896,7 +1835,7 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( pool.workerChoiceStrategyContext.workerChoiceStrategy - ).workerVirtualTaskRunTime + ).workerNodeVirtualTaskRunTime ).toBe(0) await pool.destroy() pool = new DynamicThreadPool( @@ -1922,7 +1861,7 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( workerChoiceStrategy - ).workerVirtualTaskRunTime + ).workerNodeVirtualTaskRunTime ).toBeDefined() pool.setWorkerChoiceStrategy(workerChoiceStrategy) expect( @@ -1943,7 +1882,7 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( pool.workerChoiceStrategyContext.workerChoiceStrategy - ).workerVirtualTaskRunTime + ).workerNodeVirtualTaskRunTime ).toBe(0) // We need to clean up the resources after our test await pool.destroy() diff --git a/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js b/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js index 9a27479b..d774bce8 100644 --- a/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js +++ b/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js @@ -33,6 +33,6 @@ describe('Weighted round robin strategy worker choice strategy test suite', () = expect(strategy.reset()).toBe(true) expect(strategy.nextWorkerNodeKey).toBe(0) expect(strategy.previousWorkerNodeKey).toBe(0) - expect(strategy.workerVirtualTaskRunTime).toBe(0) + expect(strategy.workerNodeVirtualTaskRunTime).toBe(0) }) }) diff --git a/typedoc.json b/typedoc.json index 22e9b77b..75f653d8 100644 --- a/typedoc.json +++ b/typedoc.json @@ -1,6 +1,6 @@ { "$schema": "https://typedoc.org/schema.json", - "tsconfig": "tsconfig.production.json", + "tsconfig": "./tsconfig.production.json", "entryPoints": ["./src"], "out": "./docs", "readme": "none", -- 2.34.1