From 932fc8be063cc15b543ad14c2ab6df0fa4224fba Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 9 Jun 2023 12:28:01 +0200 Subject: [PATCH] refactor: align worker choice strategy options namespace MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 1 + README.md | 5 +- src/pools/abstract-pool.ts | 28 +- .../abstract-worker-choice-strategy.ts | 70 +++-- .../fair-share-worker-choice-strategy.ts | 18 +- ...hted-round-robin-worker-choice-strategy.ts | 2 +- .../least-busy-worker-choice-strategy.ts | 22 +- .../least-used-worker-choice-strategy.ts | 2 +- .../round-robin-worker-choice-strategy.ts | 2 +- .../selection-strategies-types.ts | 58 ++-- ...hted-round-robin-worker-choice-strategy.ts | 18 +- src/pools/worker.ts | 4 +- src/utils.ts | 4 +- tests/pools/abstract/abstract-pool.test.js | 100 ++++--- .../selection-strategies.test.js | 276 ++++++++++-------- .../worker-choice-strategy-context.test.js | 28 +- 16 files changed, 371 insertions(+), 267 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01c1dab4..66762b0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Refactor pool worker node usage internals. +- Refactor worker choice strategy statistics requirements. ### Fixed diff --git a/README.md b/README.md index 5383863d..ca6d7aff 100644 --- a/README.md +++ b/README.md @@ -173,10 +173,11 @@ An object with these properties: - `workerChoiceStrategyOptions` (optional) - The worker choice strategy options object to use in this pool. Properties: - - `medRunTime` (optional) - Use the tasks median runtime instead of the tasks average runtime in worker choice strategies. + - `runTime` (optional) - Use the tasks median runtime instead of the tasks average runtime in worker choice strategies. + - `waitTime` (optional) - Use the tasks median wait time instead of the tasks average wait time in worker choice strategies. - `weights` (optional) - The worker weights to use in the weighted round robin worker choice strategy: `{ 0: 200, 1: 300, ..., n: 100 }` - Default: `{ medRunTime: false }` + Default: `{ runTime: { median: false }, waitTime: { median: false } }` - `restartWorkerOnError` (optional) - Restart worker on uncaught error in this pool. Default: `true` diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index ca660951..3c690fb8 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -502,19 +502,20 @@ export abstract class AbstractPool< ): void { if ( this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime + .aggregate ) { - workerUsage.runTime.aggregation += message.taskPerformance?.runTime ?? 0 + workerUsage.runTime.aggregate += message.taskPerformance?.runTime ?? 0 if ( - this.workerChoiceStrategyContext.getTaskStatisticsRequirements() - .avgRunTime && + this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime + .average && workerUsage.tasks.executed !== 0 ) { workerUsage.runTime.average = - workerUsage.runTime.aggregation / workerUsage.tasks.executed + workerUsage.runTime.aggregate / workerUsage.tasks.executed } if ( - this.workerChoiceStrategyContext.getTaskStatisticsRequirements() - .medRunTime && + this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime + .median && message.taskPerformance?.runTime != null ) { workerUsage.runTime.history.push(message.taskPerformance.runTime) @@ -531,19 +532,20 @@ export abstract class AbstractPool< const taskWaitTime = timestamp - (task.timestamp ?? timestamp) if ( this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime + .aggregate ) { - workerUsage.waitTime.aggregation += taskWaitTime ?? 0 + workerUsage.waitTime.aggregate += taskWaitTime ?? 0 if ( this.workerChoiceStrategyContext.getTaskStatisticsRequirements() - .avgWaitTime && + .waitTime.average && workerUsage.tasks.executed !== 0 ) { workerUsage.waitTime.average = - workerUsage.waitTime.aggregation / workerUsage.tasks.executed + workerUsage.waitTime.aggregate / workerUsage.tasks.executed } if ( this.workerChoiceStrategyContext.getTaskStatisticsRequirements() - .medWaitTime && + .waitTime.median && taskWaitTime != null ) { workerUsage.waitTime.history.push(taskWaitTime) @@ -825,7 +827,7 @@ export abstract class AbstractPool< statistics: { runTime: this.workerChoiceStrategyContext.getTaskStatisticsRequirements() - .runTime, + .runTime.aggregate, elu: this.workerChoiceStrategyContext.getTaskStatisticsRequirements() .elu } @@ -836,13 +838,13 @@ export abstract class AbstractPool< return { tasks: this.getTaskStatistics(worker), runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: new CircularArray() }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: new CircularArray() diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 41573ab4..18b47a1d 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -26,12 +26,16 @@ export abstract class AbstractWorkerChoiceStrategy< private toggleFindLastFreeWorkerNodeKey: boolean = false /** @inheritDoc */ public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { - runTime: false, - avgRunTime: false, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: false, + average: false, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false } @@ -48,34 +52,40 @@ export abstract class AbstractWorkerChoiceStrategy< this.choose = this.choose.bind(this) } - protected setTaskStatistics (opts: WorkerChoiceStrategyOptions): void { + protected setTaskStatisticsRequirements ( + opts: WorkerChoiceStrategyOptions + ): void { if ( - this.taskStatisticsRequirements.avgRunTime && - opts.medRunTime === true + this.taskStatisticsRequirements.runTime.average && + opts.runTime?.median === true ) { - this.taskStatisticsRequirements.avgRunTime = false - this.taskStatisticsRequirements.medRunTime = opts.medRunTime as boolean + this.taskStatisticsRequirements.runTime.average = false + this.taskStatisticsRequirements.runTime.median = opts.runTime + .median as boolean } if ( - this.taskStatisticsRequirements.medRunTime && - opts.medRunTime === false + this.taskStatisticsRequirements.runTime.median && + opts.runTime?.median === false ) { - this.taskStatisticsRequirements.avgRunTime = true - this.taskStatisticsRequirements.medRunTime = opts.medRunTime as boolean + this.taskStatisticsRequirements.runTime.average = true + this.taskStatisticsRequirements.runTime.median = opts.runTime + .median as boolean } if ( - this.taskStatisticsRequirements.avgWaitTime && - opts.medWaitTime === true + this.taskStatisticsRequirements.waitTime.average && + opts.waitTime?.median === true ) { - this.taskStatisticsRequirements.avgWaitTime = false - this.taskStatisticsRequirements.medWaitTime = opts.medWaitTime as boolean + this.taskStatisticsRequirements.waitTime.average = false + this.taskStatisticsRequirements.waitTime.median = opts.waitTime + .median as boolean } if ( - this.taskStatisticsRequirements.medWaitTime && - opts.medWaitTime === false + this.taskStatisticsRequirements.waitTime.median && + opts.waitTime?.median === false ) { - this.taskStatisticsRequirements.avgWaitTime = true - this.taskStatisticsRequirements.medWaitTime = opts.medWaitTime as boolean + this.taskStatisticsRequirements.waitTime.average = true + this.taskStatisticsRequirements.waitTime.median = opts.waitTime + .median as boolean } } @@ -94,7 +104,7 @@ export abstract class AbstractWorkerChoiceStrategy< /** @inheritDoc */ public setOptions (opts: WorkerChoiceStrategyOptions): void { opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS - this.setTaskStatistics(opts) + this.setTaskStatisticsRequirements(opts) this.opts = opts } @@ -114,28 +124,28 @@ export abstract class AbstractWorkerChoiceStrategy< /** * Gets the worker task runtime. - * If the task statistics require `avgRunTime`, the average runtime is returned. - * If the task statistics require `medRunTime`, the median runtime is returned. + * 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. */ protected getWorkerTaskRunTime (workerNodeKey: number): number { - return this.taskStatisticsRequirements.medRunTime + return this.taskStatisticsRequirements.runTime.median ? this.pool.workerNodes[workerNodeKey].workerUsage.runTime.median : this.pool.workerNodes[workerNodeKey].workerUsage.runTime.average } /** * Gets the worker task wait time. - * If the task statistics require `avgWaitTime`, the average wait time is returned. - * If the task statistics require `medWaitTime`, the median wait time is returned. + * 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. */ protected getWorkerWaitTime (workerNodeKey: number): number { - return this.taskStatisticsRequirements.medWaitTime + return this.taskStatisticsRequirements.waitTime.median ? this.pool.workerNodes[workerNodeKey].workerUsage.runTime.median : this.pool.workerNodes[workerNodeKey].workerUsage.runTime.average } 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 4462f6dd..4ea68d40 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -25,12 +25,16 @@ export class FairShareWorkerChoiceStrategy< implements IWorkerChoiceStrategy { /** @inheritDoc */ public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { - runTime: true, - avgRunTime: true, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: true, + average: true, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false } @@ -45,7 +49,7 @@ export class FairShareWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setTaskStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ 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 d7d78953..7d77a21d 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 @@ -45,7 +45,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setTaskStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) this.defaultWorkerWeight = this.computeDefaultWorkerWeight() this.roundWeights = this.getRoundWeights() } 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 3c085685..8afd2c36 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -24,12 +24,16 @@ export class LeastBusyWorkerChoiceStrategy< implements IWorkerChoiceStrategy { /** @inheritDoc */ public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { - runTime: true, - avgRunTime: false, - medRunTime: false, - waitTime: true, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: true, + average: false, + median: false + }, + waitTime: { + aggregate: true, + average: false, + median: false + }, elu: false } @@ -39,7 +43,7 @@ export class LeastBusyWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setTaskStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ @@ -58,8 +62,8 @@ export class LeastBusyWorkerChoiceStrategy< let leastBusyWorkerNodeKey!: number for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { const workerTime = - workerNode.workerUsage.runTime.aggregation + - workerNode.workerUsage.waitTime.aggregation + workerNode.workerUsage.runTime.aggregate + + workerNode.workerUsage.waitTime.aggregate if (workerTime === 0) { return workerNodeKey } else if (workerTime < minTime) { 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 528bca1d..4161a1d2 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -27,7 +27,7 @@ export class LeastUsedWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setTaskStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ diff --git a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts index afdff788..ea174552 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -32,7 +32,7 @@ export class RoundRobinWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setTaskStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ diff --git a/src/pools/selection-strategies/selection-strategies-types.ts b/src/pools/selection-strategies/selection-strategies-types.ts index 5723bd67..26fe436d 100644 --- a/src/pools/selection-strategies/selection-strategies-types.ts +++ b/src/pools/selection-strategies/selection-strategies-types.ts @@ -35,22 +35,32 @@ export const WorkerChoiceStrategies = Object.freeze({ */ export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies +/** + * Measurement options. + */ +interface MeasurementOptions { + /** + * Set measurement median. + */ + median: boolean +} + /** * Worker choice strategy options. */ export interface WorkerChoiceStrategyOptions { /** - * Use tasks median runtime instead of average runtime. + * Runtime options. * - * @defaultValue false + * @defaultValue \{ median: false \} */ - medRunTime?: boolean + runTime?: MeasurementOptions /** - * Use tasks median wait time instead of average runtime. + * Wait time options. * - * @defaultValue false + * @defaultValue \{ median: false \} */ - medWaitTime?: boolean + waitTime?: MeasurementOptions /** * Worker weights to use for weighted round robin worker selection strategy. * Weight is the tasks maximum average or median runtime in milliseconds. @@ -61,35 +71,39 @@ export interface WorkerChoiceStrategyOptions { } /** - * Pool worker node worker usage statistics requirements. + * Measurement statistics requirements. * * @internal */ -export interface TaskStatisticsRequirements { - /** - * Require tasks runtime. - */ - runTime: boolean +interface MeasurementStatisticsRequirements { /** - * Require tasks average runtime. + * Require measurement aggregate. */ - avgRunTime: boolean + aggregate: boolean /** - * Require tasks median runtime. + * Require measurement average. */ - medRunTime: boolean + average: boolean /** - * Require tasks wait time. + * Require measurement median. */ - waitTime: boolean + median: boolean +} + +/** + * Pool worker node worker usage statistics requirements. + * + * @internal + */ +export interface TaskStatisticsRequirements { /** - * Require tasks average wait time. + * Tasks runtime requirements. */ - avgWaitTime: boolean + runTime: MeasurementStatisticsRequirements /** - * Require tasks median wait time. + * Tasks wait time requirements. */ - medWaitTime: boolean + waitTime: MeasurementStatisticsRequirements /** * Event loop utilization. */ 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 75e278bc..4fcbeeda 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 @@ -25,12 +25,16 @@ export class WeightedRoundRobinWorkerChoiceStrategy< implements IWorkerChoiceStrategy { /** @inheritDoc */ public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { - runTime: true, - avgRunTime: true, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: true, + average: true, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false } @@ -53,7 +57,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setTaskStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) this.defaultWorkerWeight = this.computeDefaultWorkerWeight() } diff --git a/src/pools/worker.ts b/src/pools/worker.ts index 94f0ff73..70e4a7ed 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -63,9 +63,9 @@ export interface Task { */ export interface MeasurementStatistics { /** - * Measurement aggregation. + * Measurement aggregate. */ - aggregation: number + aggregate: number /** * Measurement average. */ diff --git a/src/utils.ts b/src/utils.ts index baaf1c1c..7b921ffa 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -12,8 +12,8 @@ export const EMPTY_FUNCTION: () => void = Object.freeze(() => { */ export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions = { - medRunTime: false, - medWaitTime: false + runTime: { median: false }, + waitTime: { median: false } } /** diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 45371a31..12237d93 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -93,8 +93,8 @@ describe('Abstract pool test suite', () => { WorkerChoiceStrategies.ROUND_ROBIN ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - medRunTime: false, - medWaitTime: false + runTime: { median: false }, + waitTime: { median: false } }) expect(pool.opts.messageHandler).toBeUndefined() expect(pool.opts.errorHandler).toBeUndefined() @@ -108,7 +108,7 @@ describe('Abstract pool test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED, workerChoiceStrategyOptions: { - medRunTime: true, + runTime: { median: true }, weights: { 0: 300, 1: 200 } }, enableEvents: false, @@ -130,7 +130,7 @@ describe('Abstract pool test suite', () => { WorkerChoiceStrategies.LEAST_USED ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - medRunTime: true, + runTime: { median: true }, weights: { 0: 300, 1: 200 } }) expect(pool.opts.messageHandler).toStrictEqual(testHandler) @@ -183,63 +183,79 @@ describe('Abstract pool test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - medRunTime: false, - medWaitTime: false + runTime: { median: false }, + waitTime: { median: false } }) for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ - medRunTime: false, - medWaitTime: false + runTime: { median: false }, + waitTime: { median: false } }) } expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: true, - avgRunTime: true, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: true, + average: true, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) - pool.setWorkerChoiceStrategyOptions({ medRunTime: true }) + pool.setWorkerChoiceStrategyOptions({ runTime: { median: true } }) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - medRunTime: true + runTime: { median: true } }) for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { - expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: true }) + expect(workerChoiceStrategy.opts).toStrictEqual({ + runTime: { median: true } + }) } expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: true, - avgRunTime: false, - medRunTime: true, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: true, + average: false, + median: true + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) - pool.setWorkerChoiceStrategyOptions({ medRunTime: false }) + pool.setWorkerChoiceStrategyOptions({ runTime: { median: false } }) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - medRunTime: false + runTime: { median: false } }) for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { - expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: false }) + expect(workerChoiceStrategy.opts).toStrictEqual({ + runTime: { median: false } + }) } expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: true, - avgRunTime: true, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: true, + average: true, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) await pool.destroy() @@ -350,13 +366,13 @@ describe('Abstract pool test suite', () => { failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) @@ -399,13 +415,13 @@ describe('Abstract pool test suite', () => { failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) @@ -423,13 +439,13 @@ describe('Abstract pool test suite', () => { failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) @@ -461,13 +477,13 @@ describe('Abstract pool test suite', () => { failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) @@ -489,13 +505,13 @@ describe('Abstract pool test suite', () => { failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index 809d5350..d43023b3 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -124,12 +124,16 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: false, - avgRunTime: false, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: false, + average: false, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) await pool.destroy() @@ -142,12 +146,16 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: false, - avgRunTime: false, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: false, + average: false, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) // We need to clean up the resources after our test @@ -176,13 +184,13 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) @@ -222,13 +230,13 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) @@ -321,12 +329,16 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: false, - avgRunTime: false, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: false, + average: false, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) await pool.destroy() @@ -339,12 +351,16 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: false, - avgRunTime: false, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: false, + average: false, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) // We need to clean up the resources after our test @@ -373,13 +389,13 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) @@ -414,13 +430,13 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) @@ -443,12 +459,16 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: true, - avgRunTime: false, - medRunTime: false, - waitTime: true, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: true, + average: false, + median: false + }, + waitTime: { + aggregate: true, + average: false, + median: false + }, elu: false }) await pool.destroy() @@ -461,12 +481,16 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: true, - avgRunTime: false, - medRunTime: false, - waitTime: true, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: true, + average: false, + median: false + }, + waitTime: { + aggregate: true, + average: false, + median: false + }, elu: false }) // We need to clean up the resources after our test @@ -495,13 +519,13 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: expect.any(Number), + aggregate: expect.any(Number), average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: expect.any(Number), + aggregate: expect.any(Number), average: 0, median: 0, history: expect.any(CircularArray) @@ -512,12 +536,10 @@ describe('Selection strategies test suite', () => { expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( max * maxMultiplier ) - expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThanOrEqual( + expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThanOrEqual(0) + expect(workerNode.workerUsage.waitTime.aggregate).toBeGreaterThanOrEqual( 0 ) - expect( - workerNode.workerUsage.waitTime.aggregation - ).toBeGreaterThanOrEqual(0) } // We need to clean up the resources after our test await pool.destroy() @@ -546,13 +568,13 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: expect.any(Number), + aggregate: expect.any(Number), average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: expect.any(Number), + aggregate: expect.any(Number), average: 0, median: 0, history: expect.any(CircularArray) @@ -563,8 +585,8 @@ describe('Selection strategies test suite', () => { expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( max * maxMultiplier ) - expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0) - expect(workerNode.workerUsage.waitTime.aggregation).toBeGreaterThan(0) + expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThan(0) + expect(workerNode.workerUsage.waitTime.aggregate).toBeGreaterThan(0) } // We need to clean up the resources after our test await pool.destroy() @@ -580,12 +602,16 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: true, - avgRunTime: true, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: true, + average: true, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) await pool.destroy() @@ -598,12 +624,16 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: true, - avgRunTime: true, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: true, + average: true, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) // We need to clean up the resources after our test @@ -632,20 +662,20 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: expect.any(Number), + aggregate: expect.any(Number), average: expect.any(Number), median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, elu: undefined }) - expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0) + expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThan(0) expect(workerNode.workerUsage.runTime.average).toBeGreaterThan(0) } expect( @@ -680,20 +710,20 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: expect.any(Number), + aggregate: expect.any(Number), average: expect.any(Number), median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, elu: undefined }) - expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0) + expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThan(0) expect(workerNode.workerUsage.runTime.average).toBeGreaterThan(0) } expect( @@ -713,7 +743,7 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE, workerChoiceStrategyOptions: { - medRunTime: true + runTime: { median: true } } } ) @@ -733,20 +763,20 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: expect.any(Number), + aggregate: expect.any(Number), average: 0, median: expect.any(Number), history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, elu: undefined }) - expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0) + expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThan(0) expect(workerNode.workerUsage.runTime.median).toBeGreaterThan(0) } expect( @@ -842,12 +872,16 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: true, - avgRunTime: true, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: true, + average: true, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) await pool.destroy() @@ -860,12 +894,16 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: true, - avgRunTime: true, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: true, + average: true, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) // We need to clean up the resources after our test @@ -894,13 +932,13 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: expect.any(Number), + aggregate: expect.any(Number), average: expect.any(Number), median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) @@ -911,9 +949,7 @@ describe('Selection strategies test suite', () => { expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( max * maxMultiplier ) - expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThanOrEqual( - 0 - ) + expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThanOrEqual(0) expect(workerNode.workerUsage.runTime.average).toBeGreaterThanOrEqual(0) } expect( @@ -953,13 +989,13 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: expect.any(Number), + aggregate: expect.any(Number), average: expect.any(Number), median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) @@ -970,7 +1006,7 @@ describe('Selection strategies test suite', () => { expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( max * maxMultiplier ) - expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0) + expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThan(0) expect(workerNode.workerUsage.runTime.average).toBeGreaterThan(0) } expect( @@ -995,7 +1031,7 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN, workerChoiceStrategyOptions: { - medRunTime: true + runTime: { median: true } } } ) @@ -1015,13 +1051,13 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: expect.any(Number), + aggregate: expect.any(Number), average: 0, median: expect.any(Number), history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) @@ -1032,7 +1068,7 @@ describe('Selection strategies test suite', () => { expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( max * maxMultiplier ) - expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0) + expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThan(0) expect(workerNode.workerUsage.runTime.median).toBeGreaterThan(0) } expect( @@ -1138,12 +1174,16 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: false, - avgRunTime: false, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: false, + average: false, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) await pool.destroy() @@ -1156,12 +1196,16 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() ).toStrictEqual({ - runTime: false, - avgRunTime: false, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, + runTime: { + aggregate: false, + average: false, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, elu: false }) // We need to clean up the resources after our test @@ -1193,13 +1237,13 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) @@ -1261,13 +1305,13 @@ describe('Selection strategies test suite', () => { failed: 0 }, runTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregation: 0, + aggregate: 0, average: 0, median: 0, history: expect.any(CircularArray) diff --git a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js index 2c5b7abb..e39b1484 100644 --- a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js +++ b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js @@ -365,54 +365,58 @@ describe('Worker choice strategy context test suite', () => { fixedPool, wwrWorkerChoiceStrategy, { - medRunTime: true + runTime: { median: true } } ) expect( - workerChoiceStrategyContext.getTaskStatisticsRequirements().avgRunTime + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime + .average ).toBe(false) expect( - workerChoiceStrategyContext.getTaskStatisticsRequirements().medRunTime + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median ).toBe(true) workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool, wwrWorkerChoiceStrategy, { - medRunTime: true + runTime: { median: true } } ) expect( - workerChoiceStrategyContext.getTaskStatisticsRequirements().avgRunTime + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime + .average ).toBe(false) expect( - workerChoiceStrategyContext.getTaskStatisticsRequirements().medRunTime + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median ).toBe(true) const fsWorkerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool, fsWorkerChoiceStrategy, { - medRunTime: true + runTime: { median: true } } ) expect( - workerChoiceStrategyContext.getTaskStatisticsRequirements().avgRunTime + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime + .average ).toBe(false) expect( - workerChoiceStrategyContext.getTaskStatisticsRequirements().medRunTime + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median ).toBe(true) workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool, fsWorkerChoiceStrategy, { - medRunTime: true + runTime: { median: true } } ) expect( - workerChoiceStrategyContext.getTaskStatisticsRequirements().avgRunTime + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime + .average ).toBe(false) expect( - workerChoiceStrategyContext.getTaskStatisticsRequirements().medRunTime + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median ).toBe(true) }) }) -- 2.34.1