## [Unreleased]
+### Added
+
+- Add pool `utilization` ratio to pool information.
+
## [2.6.6] - 2023-07-01
### Added
DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
EMPTY_FUNCTION,
isPlainObject,
- median
+ median,
+ round
} from '../utils'
import { KillBehaviors, isKillBehavior } from '../worker/worker-options'
import { CircularArray } from '../circular-array'
Response
>
+ /**
+ * The start timestamp of the pool.
+ */
+ private readonly startTimestamp
+
/**
* Constructs a new poolifier pool.
*
this.setupHook()
- for (let i = 1; i <= this.numberOfWorkers; i++) {
+ while (this.workerNodes.length < this.numberOfWorkers) {
this.createAndSetupWorker()
}
+
+ this.startTimestamp = performance.now()
}
private checkFilePath (filePath: string): void {
worker: this.worker,
minSize: this.minSize,
maxSize: this.maxSize,
+ utilization: round(this.utilization),
workerNodes: this.workerNodes.length,
idleWorkerNodes: this.workerNodes.reduce(
(accumulator, workerNode) =>
}
}
+ /**
+ * Gets the pool run time.
+ *
+ * @returns The pool run time in milliseconds.
+ */
+ private get runTime (): number {
+ return performance.now() - this.startTimestamp
+ }
+
+ /**
+ * Gets the approximate pool utilization.
+ *
+ * @returns The pool utilization.
+ */
+ private get utilization (): number {
+ const poolRunTimeCapacity = this.runTime * this.maxSize
+ const totalTasksRunTime = this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + workerNode.usage.runTime.aggregate,
+ 0
+ )
+ const totalTasksWaitTime = this.workerNodes.reduce(
+ (accumulator, workerNode) =>
+ accumulator + workerNode.usage.waitTime.aggregate,
+ 0
+ )
+ return (totalTasksRunTime + totalTasksWaitTime) / poolRunTimeCapacity
+ }
+
/**
* Pool type.
*
worker: WorkerType
minSize: number
maxSize: number
+ utilization: number
workerNodes: number
idleWorkerNodes: number
busyWorkerNodes: number
}
/**
- * Compute the median of the given data set.
+ * Computes the median of the given data set.
*
* @param dataSet - Data set.
* @returns The median of the given data set.
)
}
+/**
+ * Rounds the given number to the given scale.
+ *
+ * @param num - The number to round.
+ * @param scale - The scale to round to.
+ * @returns The rounded number.
+ */
+export const round = (num: number, scale = 2): number => {
+ const rounder = Math.pow(10, scale)
+ return Math.round(num * rounder * (1 + Number.EPSILON)) / rounder
+}
+
/**
* Is the given object a plain object?
*
worker: WorkerTypes.thread,
minSize: numberOfWorkers,
maxSize: numberOfWorkers,
+ utilization: 0,
workerNodes: numberOfWorkers,
idleWorkerNodes: numberOfWorkers,
busyWorkerNodes: 0,
worker: WorkerTypes.cluster,
minSize: numberOfWorkers,
maxSize: numberOfWorkers * 2,
+ utilization: 0,
workerNodes: numberOfWorkers,
idleWorkerNodes: numberOfWorkers,
busyWorkerNodes: 0,
worker: WorkerTypes.thread,
minSize: expect.any(Number),
maxSize: expect.any(Number),
+ utilization: 0,
workerNodes: expect.any(Number),
idleWorkerNodes: expect.any(Number),
busyWorkerNodes: expect.any(Number),
worker: WorkerTypes.thread,
minSize: expect.any(Number),
maxSize: expect.any(Number),
+ utilization: 0,
workerNodes: expect.any(Number),
idleWorkerNodes: expect.any(Number),
busyWorkerNodes: expect.any(Number),
const { expect } = require('expect')
-const { isPlainObject, median, availableParallelism } = require('../lib/utils')
+const {
+ availableParallelism,
+ isPlainObject,
+ median,
+ round
+} = require('../lib/utils')
const {
isKillBehavior,
KillBehaviors
} = require('../lib/worker/worker-options')
describe('Utils test suite', () => {
+ it('Verify availableParallelism() behavior', () => {
+ expect(typeof availableParallelism() === 'number').toBe(true)
+ })
+
it('Verify median() computation', () => {
expect(median([])).toBe(0)
expect(median([0.08])).toBe(0.08)
expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535)
})
+ it('Verify round() behavior', () => {
+ expect(round(0)).toBe(0)
+ expect(round(0.5, 0)).toBe(1)
+ expect(round(0.5)).toBe(0.5)
+ expect(round(-0.5, 0)).toBe(-1)
+ expect(round(-0.5)).toBe(-0.5)
+ expect(round(1.005)).toBe(1.01)
+ expect(round(2.175)).toBe(2.18)
+ expect(round(5.015)).toBe(5.02)
+ expect(round(-1.005)).toBe(-1.01)
+ expect(round(-2.175)).toBe(-2.18)
+ expect(round(-5.015)).toBe(-5.02)
+ })
+
it('Verify isPlainObject() behavior', () => {
expect(isPlainObject(null)).toBe(false)
expect(isPlainObject(undefined)).toBe(false)
expect(isKillBehavior(KillBehaviors.HARD, null)).toBe(false)
expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false)
})
-
- it('Verify availableParallelism() behavior', () => {
- expect(typeof availableParallelism() === 'number').toBe(true)
- })
})