X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-busy-worker-choice-strategy.ts;h=de71167ee8d307bf8c9b20f5f2df6fca7d9374c6;hb=aa1e0433fcfdc47175d6144524bcda57ab2947a1;hp=3c0856856189ab7d7af4c4f2a625ae9058ac9029;hpb=8d1e1b3b417d37460c091ec8db3ac862201a244e;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 3c085685..de71167e 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -1,4 +1,7 @@ -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' @@ -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, @@ -24,13 +27,17 @@ export class LeastBusyWorkerChoiceStrategy< implements IWorkerChoiceStrategy { /** @inheritDoc */ public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { - runTime: true, - avgRunTime: false, - medRunTime: false, - waitTime: true, - avgWaitTime: false, - medWaitTime: false, - elu: false + runTime: { + aggregate: true, + average: false, + median: false + }, + waitTime: { + aggregate: true, + average: false, + median: false + }, + elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } /** @inheritDoc */ @@ -39,7 +46,7 @@ export class LeastBusyWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setTaskStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ @@ -53,25 +60,31 @@ export class LeastBusyWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { + public choose (): number | undefined { + this.nextWorkerNodeKey = this.leastBusyNextWorkerNodeKey() + return this.nextWorkerNodeKey + } + + /** @inheritDoc */ + public remove (): boolean { + return true + } + + private leastBusyNextWorkerNodeKey (): number | undefined { let minTime = Infinity - let leastBusyWorkerNodeKey!: number + let chosenWorkerNodeKey: number | undefined for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { const workerTime = - workerNode.workerUsage.runTime.aggregation + - workerNode.workerUsage.waitTime.aggregation + (workerNode.usage.runTime?.aggregate ?? 0) + + (workerNode.usage.waitTime?.aggregate ?? 0) if (workerTime === 0) { - return workerNodeKey + chosenWorkerNodeKey = workerNodeKey + break } else if (workerTime < minTime) { minTime = workerTime - leastBusyWorkerNodeKey = workerNodeKey + chosenWorkerNodeKey = workerNodeKey } } - return leastBusyWorkerNodeKey - } - - /** @inheritDoc */ - public remove (): boolean { - return true + return chosenWorkerNodeKey } }