From: Jérôme Benoit Date: Mon, 29 May 2023 20:49:20 +0000 (+0200) Subject: feat: account tasks wait time more accurately X-Git-Tag: v2.5.0~9^2^2~16 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=09a6305fb250c17cb2565f8cbe3d9afbb33f307c;p=poolifier.git feat: account tasks wait time more accurately Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index e3967446..81012fa0 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -419,18 +419,7 @@ export abstract class AbstractPool< workerNodeKey: number, task: Task ): void { - const workerTasksUsage = this.workerNodes[workerNodeKey].tasksUsage - ++workerTasksUsage.running - if (this.workerChoiceStrategyContext.getRequiredStatistics().waitTime) { - const waitTime = performance.now() - (task.submissionTimestamp ?? 0) - workerTasksUsage.waitTime += waitTime - if ( - this.workerChoiceStrategyContext.getRequiredStatistics().medWaitTime - ) { - workerTasksUsage.waitTimeHistory.push(waitTime) - workerTasksUsage.medWaitTime = median(workerTasksUsage.waitTimeHistory) - } - } + ++this.workerNodes[workerNodeKey].tasksUsage.running } /** @@ -468,13 +457,22 @@ export abstract class AbstractPool< workerTasksUsage.medRunTime = median(workerTasksUsage.runTimeHistory) } } - if ( - this.workerChoiceStrategyContext.getRequiredStatistics().waitTime && - this.workerChoiceStrategyContext.getRequiredStatistics().avgWaitTime && - workerTasksUsage.run !== 0 - ) { - workerTasksUsage.avgWaitTime = - workerTasksUsage.waitTime / workerTasksUsage.run + if (this.workerChoiceStrategyContext.getRequiredStatistics().waitTime) { + workerTasksUsage.waitTime += message.waitTime ?? 0 + if ( + this.workerChoiceStrategyContext.getRequiredStatistics().avgWaitTime && + workerTasksUsage.run !== 0 + ) { + workerTasksUsage.avgWaitTime = + workerTasksUsage.waitTime / workerTasksUsage.run + } + if ( + this.workerChoiceStrategyContext.getRequiredStatistics().medWaitTime && + message.waitTime != null + ) { + workerTasksUsage.waitTimeHistory.push(message.waitTime) + workerTasksUsage.medWaitTime = median(workerTasksUsage.waitTimeHistory) + } } } diff --git a/src/utility-types.ts b/src/utility-types.ts index 08861ebd..cc04f2cb 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -33,6 +33,10 @@ export interface MessageValue< * Runtime. */ readonly runTime?: number + /** + * Wait time. + */ + readonly waitTime?: number /** * Reference to main worker. */ diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 5d0c9492..176a5893 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -207,12 +207,14 @@ export abstract class AbstractWorker< ): void { try { const startTimestamp = performance.now() + const waitTime = startTimestamp - (message.submissionTimestamp ?? 0) const res = fn(message.data) const runTime = performance.now() - startTimestamp this.sendToMainWorker({ data: res, id: message.id, - runTime + runTime, + waitTime }) } catch (e) { const err = this.handleError(e as Error) @@ -233,13 +235,15 @@ export abstract class AbstractWorker< message: MessageValue ): void { const startTimestamp = performance.now() + const waitTime = startTimestamp - (message.submissionTimestamp ?? 0) fn(message.data) .then(res => { const runTime = performance.now() - startTimestamp this.sendToMainWorker({ data: res, id: message.id, - runTime + runTime, + waitTime }) return null })