X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-busy-worker-choice-strategy.ts;h=7e9d581421319919e865c809df85d06f3c6e305c;hb=3c93feb918b47943d801029fad82a61469e40127;hp=6e413825d666025022acf6018dfe47c77e8345fe;hpb=802983837716dc38305c13f905a1977688365293;p=poolifier.git 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 6e413825..7e9d5814 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -1,10 +1,13 @@ -import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' +import { + DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, + DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS +} from '../../utils' import type { IPool } from '../pool' import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, - RequiredStatistics, + TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -12,8 +15,8 @@ import type { * Selects the least busy worker. * * @typeParam Worker - Type of worker which manages the strategy. - * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of execution response. This can only be serializable data. + * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. + * @typeParam Response - Type of execution response. This can only be structured-cloneable data. */ export class LeastBusyWorkerChoiceStrategy< Worker extends IWorker, @@ -23,13 +26,18 @@ export class LeastBusyWorkerChoiceStrategy< extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { /** @inheritDoc */ - public readonly requiredStatistics: RequiredStatistics = { - runTime: true, - avgRunTime: false, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false + public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { + runTime: { + aggregate: true, + average: false, + median: false + }, + waitTime: { + aggregate: true, + average: false, + median: false + }, + elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } /** @inheritDoc */ @@ -38,7 +46,7 @@ export class LeastBusyWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setRequiredStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ @@ -53,22 +61,20 @@ export class LeastBusyWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number { - const freeWorkerNodeKey = this.findFreeWorkerNodeKey() - if (freeWorkerNodeKey !== -1) { - return freeWorkerNodeKey - } - let minRunTime = Infinity - let leastBusyWorkerNodeKey!: number + let minTime = Infinity for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const workerRunTime = workerNode.tasksUsage.runTime - if (workerRunTime === 0) { - return workerNodeKey - } else if (workerRunTime < minRunTime) { - minRunTime = workerRunTime - leastBusyWorkerNodeKey = workerNodeKey + const workerTime = + workerNode.workerUsage.runTime.aggregate + + workerNode.workerUsage.waitTime.aggregate + if (workerTime === 0) { + this.nextWorkerNodeId = workerNodeKey + break + } else if (workerTime < minTime) { + minTime = workerTime + this.nextWorkerNodeId = workerNodeKey } } - return leastBusyWorkerNodeKey + return this.nextWorkerNodeId } /** @inheritDoc */