feat: add median task run time statistic
[poolifier.git] / src / pools / selection-strategies / less-busy-worker-choice-strategy.ts
1 import type { IPoolWorker } from '../pool-worker'
2 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
3 import type {
4 IWorkerChoiceStrategy,
5 RequiredStatistics
6 } from './selection-strategies-types'
7
8 /**
9 * Selects the less busy worker.
10 *
11 * @typeParam Worker - Type of worker which manages the strategy.
12 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
13 * @typeParam Response - Type of response of execution. This can only be serializable data.
14 */
15 export class LessBusyWorkerChoiceStrategy<
16 Worker extends IPoolWorker,
17 Data = unknown,
18 Response = unknown
19 >
20 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
21 implements IWorkerChoiceStrategy {
22 /** @inheritDoc */
23 public readonly requiredStatistics: RequiredStatistics = {
24 runTime: true,
25 avgRunTime: false,
26 medRunTime: false
27 }
28
29 /** @inheritDoc */
30 public reset (): boolean {
31 return true
32 }
33
34 /** @inheritDoc */
35 public choose (): number {
36 const freeWorkerKey = this.pool.findFreeWorkerKey()
37 if (freeWorkerKey !== -1) {
38 return freeWorkerKey
39 }
40 let minRunTime = Infinity
41 let lessBusyWorkerKey!: number
42 for (const [index, workerItem] of this.pool.workers.entries()) {
43 const workerRunTime = workerItem.tasksUsage.runTime
44 if (workerRunTime === 0) {
45 return index
46 } else if (workerRunTime < minRunTime) {
47 minRunTime = workerRunTime
48 lessBusyWorkerKey = index
49 }
50 }
51 return lessBusyWorkerKey
52 }
53
54 /** @inheritDoc */
55 public remove (workerKey: number): boolean {
56 return true
57 }
58 }