From e4f20deb9ad8a6e0113306f0ec19ba6d4676d24a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 18 Jul 2023 22:23:29 +0200 Subject: [PATCH] refactor: factor out measurement statistics update MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 132 ++++++++----------------------------- src/utils.ts | 37 +++++++++++ 2 files changed, 63 insertions(+), 106 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 87427ad7..6467f4eb 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -13,7 +13,8 @@ import { isKillBehavior, isPlainObject, median, - round + round, + updateMeasurementStatistics } from '../utils' import { KillBehaviors } from '../worker/worker-options' import { @@ -747,38 +748,12 @@ export abstract class AbstractPool< workerUsage: WorkerUsage, message: MessageValue ): void { - if ( - this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime - .aggregate - ) { - const taskRunTime = message.taskPerformance?.runTime ?? 0 - workerUsage.runTime.aggregate = - (workerUsage.runTime.aggregate ?? 0) + taskRunTime - workerUsage.runTime.minimum = Math.min( - taskRunTime, - workerUsage.runTime?.minimum ?? Infinity - ) - workerUsage.runTime.maximum = Math.max( - taskRunTime, - workerUsage.runTime?.maximum ?? -Infinity - ) - if ( - this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime - .average && - workerUsage.tasks.executed !== 0 - ) { - workerUsage.runTime.average = - workerUsage.runTime.aggregate / workerUsage.tasks.executed - } - if ( - this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime - .median && - message.taskPerformance?.runTime != null - ) { - workerUsage.runTime.history.push(message.taskPerformance.runTime) - workerUsage.runTime.median = median(workerUsage.runTime.history) - } - } + updateMeasurementStatistics( + workerUsage.runTime, + this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime, + message.taskPerformance?.runTime ?? 0, + workerUsage.tasks.executed + ) } private updateWaitTimeWorkerUsage ( @@ -787,53 +762,35 @@ export abstract class AbstractPool< ): void { const timestamp = performance.now() const taskWaitTime = timestamp - (task.timestamp ?? timestamp) - if ( - this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime - .aggregate - ) { - workerUsage.waitTime.aggregate = - (workerUsage.waitTime?.aggregate ?? 0) + taskWaitTime - workerUsage.waitTime.minimum = Math.min( - taskWaitTime, - workerUsage.waitTime?.minimum ?? Infinity - ) - workerUsage.waitTime.maximum = Math.max( - taskWaitTime, - workerUsage.waitTime?.maximum ?? -Infinity - ) - if ( - this.workerChoiceStrategyContext.getTaskStatisticsRequirements() - .waitTime.average && - workerUsage.tasks.executed !== 0 - ) { - workerUsage.waitTime.average = - workerUsage.waitTime.aggregate / workerUsage.tasks.executed - } - if ( - this.workerChoiceStrategyContext.getTaskStatisticsRequirements() - .waitTime.median - ) { - workerUsage.waitTime.history.push(taskWaitTime) - workerUsage.waitTime.median = median(workerUsage.waitTime.history) - } - } + updateMeasurementStatistics( + workerUsage.waitTime, + this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime, + taskWaitTime, + workerUsage.tasks.executed + ) } private updateEluWorkerUsage ( workerUsage: WorkerUsage, message: MessageValue ): void { + updateMeasurementStatistics( + workerUsage.elu.active, + this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu, + message.taskPerformance?.elu?.active ?? 0, + workerUsage.tasks.executed + ) + updateMeasurementStatistics( + workerUsage.elu.idle, + this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu, + message.taskPerformance?.elu?.idle ?? 0, + workerUsage.tasks.executed + ) if ( this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu .aggregate ) { if (message.taskPerformance?.elu != null) { - workerUsage.elu.idle.aggregate = - (workerUsage.elu.idle?.aggregate ?? 0) + - message.taskPerformance.elu.idle - workerUsage.elu.active.aggregate = - (workerUsage.elu.active?.aggregate ?? 0) + - message.taskPerformance.elu.active if (workerUsage.elu.utilization != null) { workerUsage.elu.utilization = (workerUsage.elu.utilization + @@ -842,43 +799,6 @@ export abstract class AbstractPool< } else { workerUsage.elu.utilization = message.taskPerformance.elu.utilization } - workerUsage.elu.idle.minimum = Math.min( - message.taskPerformance.elu.idle, - workerUsage.elu.idle?.minimum ?? Infinity - ) - workerUsage.elu.idle.maximum = Math.max( - message.taskPerformance.elu.idle, - workerUsage.elu.idle?.maximum ?? -Infinity - ) - workerUsage.elu.active.minimum = Math.min( - message.taskPerformance.elu.active, - workerUsage.elu.active?.minimum ?? Infinity - ) - workerUsage.elu.active.maximum = Math.max( - message.taskPerformance.elu.active, - workerUsage.elu.active?.maximum ?? -Infinity - ) - if ( - this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu - .average && - workerUsage.tasks.executed !== 0 - ) { - workerUsage.elu.idle.average = - workerUsage.elu.idle.aggregate / workerUsage.tasks.executed - workerUsage.elu.active.average = - workerUsage.elu.active.aggregate / workerUsage.tasks.executed - } - if ( - this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu - .median - ) { - workerUsage.elu.idle.history.push(message.taskPerformance.elu.idle) - workerUsage.elu.active.history.push( - message.taskPerformance.elu.active - ) - workerUsage.elu.idle.median = median(workerUsage.elu.idle.history) - workerUsage.elu.active.median = median(workerUsage.elu.active.history) - } } } } diff --git a/src/utils.ts b/src/utils.ts index 0becc5ed..cc71491a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,6 +4,7 @@ import type { WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types' import type { KillBehavior } from './worker/worker-options' +import type { MeasurementStatistics } from './pools/worker' /** * Default task name. @@ -128,3 +129,39 @@ export const isAsyncFunction = ( ): fn is (...args: unknown[]) => Promise => { return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction' } + +/** + * Updates the given measurement statistics. + * + * @param measurementStatistics - The measurement statistics to update. + * @param measurementRequirements - The measurement statistics requirements. + * @param measurementValue - The measurement value. + * @param tasksExecuted - The number of tasks executed. + */ +export const updateMeasurementStatistics = ( + measurementStatistics: MeasurementStatistics, + measurementRequirements: MeasurementStatisticsRequirements, + measurementValue: number, + tasksExecuted: number +): void => { + if (measurementRequirements.aggregate) { + measurementStatistics.aggregate = + (measurementStatistics.aggregate ?? 0) + measurementValue + measurementStatistics.minimum = Math.min( + measurementValue, + measurementStatistics.minimum ?? Infinity + ) + measurementStatistics.maximum = Math.max( + measurementValue, + measurementStatistics.maximum ?? -Infinity + ) + if (measurementRequirements.average && tasksExecuted !== 0) { + measurementStatistics.average = + measurementStatistics.aggregate / tasksExecuted + } + if (measurementRequirements.median && measurementValue != null) { + measurementStatistics.history.push(measurementValue) + measurementStatistics.median = median(measurementStatistics.history) + } + } +} -- 2.34.1