-import { cpus } from 'node:os'
import type { IWorker } from '../worker'
import type { IPool } from '../pool'
import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
import type {
IWorkerChoiceStrategy,
- RequiredStatistics,
+ TaskStatistics,
WorkerChoiceStrategyOptions
} from './selection-strategies-types'
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
implements IWorkerChoiceStrategy {
/** @inheritDoc */
- public readonly requiredStatistics: RequiredStatistics = {
+ public readonly taskStatistics: TaskStatistics = {
runTime: true,
avgRunTime: true,
- medRunTime: false
+ medRunTime: false,
+ waitTime: false,
+ avgWaitTime: false,
+ medWaitTime: false,
+ elu: false
}
/**
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
- this.checkOptions(this.opts)
+ this.setTaskStatistics(this.opts)
this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
}
return true
}
+ /** @inheritDoc */
+ public update (): boolean {
+ return true
+ }
+
/** @inheritDoc */
public choose (): number {
const chosenWorkerNodeKey = this.currentWorkerNodeId
- const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime ?? 0
- const workerTaskWeight =
+ const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
+ const workerWeight =
this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight
- if (workerVirtualTaskRunTime < workerTaskWeight) {
+ if (workerVirtualTaskRunTime < workerWeight) {
this.workerVirtualTaskRunTime =
workerVirtualTaskRunTime +
- (this.getWorkerVirtualTaskRunTime(chosenWorkerNodeKey) ?? 0)
+ this.getWorkerTaskRunTime(chosenWorkerNodeKey)
} else {
this.currentWorkerNodeId =
this.currentWorkerNodeId === this.pool.workerNodes.length - 1
if (this.currentWorkerNodeId === workerNodeKey) {
if (this.pool.workerNodes.length === 0) {
this.currentWorkerNodeId = 0
- } else {
- this.currentWorkerNodeId =
- this.currentWorkerNodeId > this.pool.workerNodes.length - 1
- ? this.pool.workerNodes.length - 1
- : this.currentWorkerNodeId
+ } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) {
+ this.currentWorkerNodeId = this.pool.workerNodes.length - 1
}
this.workerVirtualTaskRunTime = 0
}
return true
}
-
- private getWorkerVirtualTaskRunTime (workerNodeKey: number): number {
- return this.requiredStatistics.medRunTime
- ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
- : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
- }
-
- private computeDefaultWorkerWeight (): number {
- let cpusCycleTimeWeight = 0
- for (const cpu of cpus()) {
- // CPU estimated cycle time
- const numberOfDigits = cpu.speed.toString().length - 1
- const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
- cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
- }
- return Math.round(cpusCycleTimeWeight / cpus().length)
- }
}