From: Jérôme Benoit Date: Thu, 8 Jun 2023 17:54:34 +0000 (+0200) Subject: fix: fix type namespace collision X-Git-Tag: v2.6.0~14 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=87de9ff55a7ad494b9e9500208b9b7319c094ea6;p=poolifier.git fix: fix type namespace collision Signed-off-by: Jérôme Benoit --- diff --git a/src/index.ts b/src/index.ts index df1ca536..55355d2e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,16 +19,18 @@ export type { ErrorHandler, ExitHandler, IWorker, + MeasurementStatistics, MessageHandler, OnlineHandler, Task, - WorkerUsage, - WorkerNode + TaskStatistics, + WorkerNode, + WorkerUsage } from './pools/worker' export { WorkerChoiceStrategies } from './pools/selection-strategies/selection-strategies-types' export type { IWorkerChoiceStrategy, - TaskStatistics, + TaskStatisticsRequirements, WorkerChoiceStrategy, WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types' @@ -54,6 +56,7 @@ export type { Draft, MessageValue, PromiseResponseWrapper, + TaskError, TaskPerformance, WorkerStatistics } from './utility-types' diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index e4947cdc..e941fabb 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -514,17 +514,21 @@ export abstract class AbstractPool< workerUsage: WorkerUsage, message: MessageValue ): void { - if (this.workerChoiceStrategyContext.getTaskStatistics().runTime) { + if ( + this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime + ) { workerUsage.runTime.aggregation += message.taskPerformance?.runTime ?? 0 if ( - this.workerChoiceStrategyContext.getTaskStatistics().avgRunTime && + this.workerChoiceStrategyContext.getTaskStatisticsRequirements() + .avgRunTime && workerUsage.tasks.executed !== 0 ) { workerUsage.runTime.average = workerUsage.runTime.aggregation / workerUsage.tasks.executed } if ( - this.workerChoiceStrategyContext.getTaskStatistics().medRunTime && + this.workerChoiceStrategyContext.getTaskStatisticsRequirements() + .medRunTime && message.taskPerformance?.runTime != null ) { workerUsage.runTime.history.push(message.taskPerformance.runTime) @@ -537,17 +541,21 @@ export abstract class AbstractPool< workerUsage: WorkerUsage, message: MessageValue ): void { - if (this.workerChoiceStrategyContext.getTaskStatistics().waitTime) { + if ( + this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime + ) { workerUsage.waitTime.aggregation += message.taskPerformance?.waitTime ?? 0 if ( - this.workerChoiceStrategyContext.getTaskStatistics().avgWaitTime && + this.workerChoiceStrategyContext.getTaskStatisticsRequirements() + .avgWaitTime && workerUsage.tasks.executed !== 0 ) { workerUsage.waitTime.average = workerUsage.waitTime.aggregation / workerUsage.tasks.executed } if ( - this.workerChoiceStrategyContext.getTaskStatistics().medWaitTime && + this.workerChoiceStrategyContext.getTaskStatisticsRequirements() + .medWaitTime && message.taskPerformance?.waitTime != null ) { workerUsage.waitTime.history.push(message.taskPerformance.waitTime) @@ -560,7 +568,7 @@ export abstract class AbstractPool< workerTasksUsage: WorkerUsage, message: MessageValue ): void { - if (this.workerChoiceStrategyContext.getTaskStatistics().elu) { + if (this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu) { if ( workerTasksUsage.elu != null && message.taskPerformance?.elu != null @@ -848,9 +856,14 @@ export abstract class AbstractPool< private setWorkerStatistics (worker: Worker): void { this.sendToWorker(worker, { statistics: { - runTime: this.workerChoiceStrategyContext.getTaskStatistics().runTime, - waitTime: this.workerChoiceStrategyContext.getTaskStatistics().waitTime, - elu: this.workerChoiceStrategyContext.getTaskStatistics().elu + runTime: + this.workerChoiceStrategyContext.getTaskStatisticsRequirements() + .runTime, + waitTime: + this.workerChoiceStrategyContext.getTaskStatisticsRequirements() + .waitTime, + elu: this.workerChoiceStrategyContext.getTaskStatisticsRequirements() + .elu } }) } diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index f064c2e0..b5b77ff6 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -4,7 +4,7 @@ import type { IPool } from '../pool' import type { IWorker } from '../worker' import type { IWorkerChoiceStrategy, - TaskStatistics, + TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -25,7 +25,7 @@ export abstract class AbstractWorkerChoiceStrategy< */ private toggleFindLastFreeWorkerNodeKey: boolean = false /** @inheritDoc */ - public readonly taskStatistics: TaskStatistics = { + public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { runTime: false, avgRunTime: false, medRunTime: false, @@ -49,21 +49,33 @@ export abstract class AbstractWorkerChoiceStrategy< } protected setTaskStatistics (opts: WorkerChoiceStrategyOptions): void { - if (this.taskStatistics.avgRunTime && opts.medRunTime === true) { - this.taskStatistics.avgRunTime = false - this.taskStatistics.medRunTime = opts.medRunTime as boolean + if ( + this.taskStatisticsRequirements.avgRunTime && + opts.medRunTime === true + ) { + this.taskStatisticsRequirements.avgRunTime = false + this.taskStatisticsRequirements.medRunTime = opts.medRunTime as boolean } - if (this.taskStatistics.medRunTime && opts.medRunTime === false) { - this.taskStatistics.avgRunTime = true - this.taskStatistics.medRunTime = opts.medRunTime as boolean + if ( + this.taskStatisticsRequirements.medRunTime && + opts.medRunTime === false + ) { + this.taskStatisticsRequirements.avgRunTime = true + this.taskStatisticsRequirements.medRunTime = opts.medRunTime as boolean } - if (this.taskStatistics.avgWaitTime && opts.medWaitTime === true) { - this.taskStatistics.avgWaitTime = false - this.taskStatistics.medWaitTime = opts.medWaitTime as boolean + if ( + this.taskStatisticsRequirements.avgWaitTime && + opts.medWaitTime === true + ) { + this.taskStatisticsRequirements.avgWaitTime = false + this.taskStatisticsRequirements.medWaitTime = opts.medWaitTime as boolean } - if (this.taskStatistics.medWaitTime && opts.medWaitTime === false) { - this.taskStatistics.avgWaitTime = true - this.taskStatistics.medWaitTime = opts.medWaitTime as boolean + if ( + this.taskStatisticsRequirements.medWaitTime && + opts.medWaitTime === false + ) { + this.taskStatisticsRequirements.avgWaitTime = true + this.taskStatisticsRequirements.medWaitTime = opts.medWaitTime as boolean } } @@ -102,28 +114,28 @@ export abstract class AbstractWorkerChoiceStrategy< /** * Gets the worker task runtime. - * If the task statistics wants `avgRunTime`, the average runtime is returned. - * If the task statistics wants `medRunTime`, the median runtime is returned. + * If the task statistics require `avgRunTime`, the average runtime is returned. + * If the task statistics require `medRunTime`, the median runtime is returned. * * @param workerNodeKey - The worker node key. * @returns The worker task runtime. */ protected getWorkerTaskRunTime (workerNodeKey: number): number { - return this.taskStatistics.medRunTime + return this.taskStatisticsRequirements.medRunTime ? this.pool.workerNodes[workerNodeKey].workerUsage.runTime.median : this.pool.workerNodes[workerNodeKey].workerUsage.runTime.average } /** * Gets the worker task wait time. - * If the task statistics wants `avgWaitTime`, the average wait time is returned. - * If the task statistics wants `medWaitTime`, the median wait time is returned. + * If the task statistics require `avgWaitTime`, the average wait time is returned. + * If the task statistics require `medWaitTime`, 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.taskStatistics.medWaitTime + return this.taskStatisticsRequirements.medWaitTime ? 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 f6fdc4af..4462f6dd 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -4,7 +4,7 @@ import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, - TaskStatistics, + TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -24,7 +24,7 @@ export class FairShareWorkerChoiceStrategy< extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { /** @inheritDoc */ - public readonly taskStatistics: TaskStatistics = { + public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { runTime: true, avgRunTime: true, medRunTime: false, 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 de747089..f171ab5f 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -4,7 +4,7 @@ import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, - TaskStatistics, + TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -23,7 +23,7 @@ export class LeastBusyWorkerChoiceStrategy< extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { /** @inheritDoc */ - public readonly taskStatistics: TaskStatistics = { + public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { runTime: true, avgRunTime: false, medRunTime: false, diff --git a/src/pools/selection-strategies/selection-strategies-types.ts b/src/pools/selection-strategies/selection-strategies-types.ts index 9bf28ac6..5723bd67 100644 --- a/src/pools/selection-strategies/selection-strategies-types.ts +++ b/src/pools/selection-strategies/selection-strategies-types.ts @@ -65,7 +65,7 @@ export interface WorkerChoiceStrategyOptions { * * @internal */ -export interface TaskStatistics { +export interface TaskStatisticsRequirements { /** * Require tasks runtime. */ @@ -101,9 +101,9 @@ export interface TaskStatistics { */ export interface IWorkerChoiceStrategy { /** - * Required tasks statistics. + * Tasks statistics requirements. */ - readonly taskStatistics: TaskStatistics + readonly taskStatisticsRequirements: TaskStatisticsRequirements /** * Resets strategy internals. * 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 729dbe66..75e278bc 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 @@ -4,7 +4,7 @@ import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, - TaskStatistics, + TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -24,7 +24,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { /** @inheritDoc */ - public readonly taskStatistics: TaskStatistics = { + public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { runTime: true, avgRunTime: true, medRunTime: false, diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index 3d32dad6..f6e132c8 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -8,7 +8,7 @@ import { LeastUsedWorkerChoiceStrategy } from './least-used-worker-choice-strate import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy' import type { IWorkerChoiceStrategy, - TaskStatistics, + TaskStatisticsRequirements, WorkerChoiceStrategy, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -97,16 +97,16 @@ export class WorkerChoiceStrategyContext< } /** - * Gets the worker choice strategy task statistics in the context. + * Gets the worker choice strategy task statistics requirements in the context. * - * @returns The task statistics. + * @returns The task statistics requirements. */ - public getTaskStatistics (): TaskStatistics { + public getTaskStatisticsRequirements (): TaskStatisticsRequirements { return ( this.workerChoiceStrategies.get( this.workerChoiceStrategy ) as IWorkerChoiceStrategy - ).taskStatistics + ).taskStatisticsRequirements } /** diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index ead69d06..45371a31 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -193,7 +193,9 @@ describe('Abstract pool test suite', () => { medWaitTime: false }) } - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: true, avgRunTime: true, medRunTime: false, @@ -210,7 +212,9 @@ describe('Abstract pool test suite', () => { .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: true }) } - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: true, avgRunTime: false, medRunTime: true, @@ -227,7 +231,9 @@ describe('Abstract pool test suite', () => { .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: false }) } - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: true, avgRunTime: true, medRunTime: false, diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index 62e2e522..7a67f6c4 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -121,7 +121,9 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy } ) - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: false, avgRunTime: false, medRunTime: false, @@ -137,7 +139,9 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy } ) - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: false, avgRunTime: false, medRunTime: false, @@ -314,7 +318,9 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy } ) - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: false, avgRunTime: false, medRunTime: false, @@ -330,7 +336,9 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy } ) - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: false, avgRunTime: false, medRunTime: false, @@ -432,7 +440,9 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy } ) - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: true, avgRunTime: false, medRunTime: false, @@ -448,7 +458,9 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy } ) - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: true, avgRunTime: false, medRunTime: false, @@ -561,7 +573,9 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy } ) - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: true, avgRunTime: true, medRunTime: false, @@ -577,7 +591,9 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy } ) - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: true, avgRunTime: true, medRunTime: false, @@ -819,7 +835,9 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy } ) - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: true, avgRunTime: true, medRunTime: false, @@ -835,7 +853,9 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy } ) - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: true, avgRunTime: true, medRunTime: false, @@ -1111,7 +1131,9 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy } ) - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: false, avgRunTime: false, medRunTime: false, @@ -1127,7 +1149,9 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy } ) - expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({ + expect( + pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() + ).toStrictEqual({ runTime: false, avgRunTime: false, medRunTime: false, 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 5e202039..2c5b7abb 100644 --- a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js +++ b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js @@ -368,12 +368,12 @@ describe('Worker choice strategy context test suite', () => { medRunTime: true } ) - expect(workerChoiceStrategyContext.getTaskStatistics().avgRunTime).toBe( - false - ) - expect(workerChoiceStrategyContext.getTaskStatistics().medRunTime).toBe( - true - ) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().avgRunTime + ).toBe(false) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().medRunTime + ).toBe(true) workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool, wwrWorkerChoiceStrategy, @@ -381,12 +381,12 @@ describe('Worker choice strategy context test suite', () => { medRunTime: true } ) - expect(workerChoiceStrategyContext.getTaskStatistics().avgRunTime).toBe( - false - ) - expect(workerChoiceStrategyContext.getTaskStatistics().medRunTime).toBe( - true - ) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().avgRunTime + ).toBe(false) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().medRunTime + ).toBe(true) const fsWorkerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool, @@ -395,12 +395,12 @@ describe('Worker choice strategy context test suite', () => { medRunTime: true } ) - expect(workerChoiceStrategyContext.getTaskStatistics().avgRunTime).toBe( - false - ) - expect(workerChoiceStrategyContext.getTaskStatistics().medRunTime).toBe( - true - ) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().avgRunTime + ).toBe(false) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().medRunTime + ).toBe(true) workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool, fsWorkerChoiceStrategy, @@ -408,11 +408,11 @@ describe('Worker choice strategy context test suite', () => { medRunTime: true } ) - expect(workerChoiceStrategyContext.getTaskStatistics().avgRunTime).toBe( - false - ) - expect(workerChoiceStrategyContext.getTaskStatistics().medRunTime).toBe( - true - ) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().avgRunTime + ).toBe(false) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().medRunTime + ).toBe(true) }) })