'esm',
'fibonacci',
'fs',
- 'hrtime',
'inheritDoc',
'jsdoc',
'microjob',
`WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN` and `WorkerChoiceStrategies.FAIR_SHARE` strategies are targeted to heavy and long tasks
Default: `WorkerChoiceStrategies.ROUND_ROBIN`
+- `workerChoiceStrategyOptions` (optional) - The worker choice strategy options object to use in this pool.
+ Properties:
+
+ - `medRunTime` (optional) - Use the tasks median run time instead of the tasks average run time in worker choice strategies.
+ Default: { medRunTime: false }
+
- `enableEvents` (optional) - Events emission enablement in this pool. Default: true
- `enableTasksQueue` (optional, experimental) - Tasks queue per worker enablement in this pool. Default: false
Worker,
Data,
Response
- >(this, this.opts.workerChoiceStrategy)
+ >(
+ this,
+ this.opts.workerChoiceStrategy,
+ this.opts.workerChoiceStrategyOptions
+ )
}
private checkFilePath (filePath: string): void {
this.opts.workerChoiceStrategy =
opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy)
+ this.opts.workerChoiceStrategyOptions =
+ opts.workerChoiceStrategyOptions ?? { medRunTime: false }
this.opts.enableEvents = opts.enableEvents ?? true
this.opts.enableTasksQueue = opts.enableTasksQueue ?? false
}
MessageHandler,
OnlineHandler
} from './worker'
-import type { WorkerChoiceStrategy } from './selection-strategies/selection-strategies-types'
+import type {
+ WorkerChoiceStrategy,
+ WorkerChoiceStrategyOptions
+} from './selection-strategies/selection-strategies-types'
/**
* Pool events emitter.
* The worker choice strategy to use in this pool.
*/
workerChoiceStrategy?: WorkerChoiceStrategy
+ /**
+ * The worker choice strategy options.
+ */
+ workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
/**
* Pool events emission.
*
import type { IWorker } from '../worker'
import type {
IWorkerChoiceStrategy,
- RequiredStatistics
+ RequiredStatistics,
+ WorkerChoiceStrategyOptions
} from './selection-strategies-types'
/**
/** @inheritDoc */
protected readonly isDynamicPool: boolean
/** @inheritDoc */
- public requiredStatistics: RequiredStatistics = {
+ public readonly requiredStatistics: RequiredStatistics = {
runTime: false,
avgRunTime: false,
medRunTime: false
* Constructs a worker choice strategy bound to the pool.
*
* @param pool - The pool instance.
+ * @param opts - The worker choice strategy options.
*/
public constructor (
- protected readonly pool: IPoolInternal<Worker, Data, Response>
+ protected readonly pool: IPoolInternal<Worker, Data, Response>,
+ protected readonly opts: WorkerChoiceStrategyOptions = { medRunTime: false }
) {
+ this.checkOptions()
this.isDynamicPool = this.pool.type === PoolType.DYNAMIC
this.choose.bind(this)
}
+ private checkOptions (): void {
+ if (this.requiredStatistics.avgRunTime && this.opts.medRunTime === true) {
+ this.requiredStatistics.medRunTime = true
+ }
+ }
+
/** @inheritDoc */
public abstract reset (): boolean
performance.now(),
this.workerLastVirtualTaskTimestamp.get(workerNodeKey)?.end ?? -Infinity
)
+ const workerVirtualTaskTRunTime = this.requiredStatistics.medRunTime
+ ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
+ : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
this.workerLastVirtualTaskTimestamp.set(workerNodeKey, {
start: workerVirtualTaskStartTimestamp,
- end:
- workerVirtualTaskStartTimestamp +
- (this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime ?? 0)
+ end: workerVirtualTaskStartTimestamp + (workerVirtualTaskTRunTime ?? 0)
})
}
}
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
import type {
IWorkerChoiceStrategy,
- RequiredStatistics
+ RequiredStatistics,
+ WorkerChoiceStrategyOptions
} from './selection-strategies-types'
/**
* Constructs a worker choice strategy that selects with a weighted round robin scheduling algorithm.
*
* @param pool - The pool instance.
+ * @param opts - The worker choice strategy options.
*/
- public constructor (pool: IPoolInternal<Worker, Data, Response>) {
- super(pool)
+ public constructor (
+ pool: IPoolInternal<Worker, Data, Response>,
+ opts?: WorkerChoiceStrategyOptions
+ ) {
+ super(pool, opts)
this.defaultWorkerWeight = this.computeWorkerWeight()
this.initWorkersTaskRunTime()
}
}
private getWorkerVirtualTaskRunTime (workerNodeKey: number): number {
- return this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
+ return this.requiredStatistics.medRunTime
+ ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
+ : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
}
private computeWorkerWeight (): number {
import type {
IWorkerChoiceStrategy,
RequiredStatistics,
- WorkerChoiceStrategy
+ WorkerChoiceStrategy,
+ WorkerChoiceStrategyOptions
} from './selection-strategies-types'
import { WorkerChoiceStrategies } from './selection-strategies-types'
import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy'
*
* @param pool - The pool instance.
* @param workerChoiceStrategyType - The worker choice strategy.
+ * @param opts - The worker choice strategy options.
*/
public constructor (
pool: IPoolInternal<Worker, Data, Response>,
- private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
+ private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN,
+ opts: WorkerChoiceStrategyOptions = { medRunTime: false }
) {
this.execute.bind(this)
this.workerChoiceStrategies = new Map<
>([
[
WorkerChoiceStrategies.ROUND_ROBIN,
- new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
+ new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
],
[
WorkerChoiceStrategies.LESS_USED,
- new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool)
+ new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
],
[
WorkerChoiceStrategies.LESS_BUSY,
- new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool)
+ new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
],
[
WorkerChoiceStrategies.FAIR_SHARE,
- new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool)
+ new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool, opts)
],
[
WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
- new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
+ new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>(
+ pool,
+ opts
+ )
]
])
}
expect(pool.opts.workerChoiceStrategy).toBe(
WorkerChoiceStrategies.ROUND_ROBIN
)
+ expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ medRunTime: false
+ })
expect(pool.opts.messageHandler).toBeUndefined()
expect(pool.opts.errorHandler).toBeUndefined()
expect(pool.opts.onlineHandler).toBeUndefined()
'./tests/worker-files/thread/testWorker.js',
{
workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED,
+ workerChoiceStrategyOptions: { medRunTime: true },
enableEvents: false,
enableTasksQueue: true,
messageHandler: testHandler,
expect(pool.opts.workerChoiceStrategy).toBe(
WorkerChoiceStrategies.LESS_USED
)
+ expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ medRunTime: true
+ })
expect(pool.opts.messageHandler).toStrictEqual(testHandler)
expect(pool.opts.errorHandler).toStrictEqual(testHandler)
expect(pool.opts.onlineHandler).toStrictEqual(testHandler)