From 09a6305fb250c17cb2565f8cbe3d9afbb33f307c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 29 May 2023 22:49:20 +0200 Subject: [PATCH] feat: account tasks wait time more accurately 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 | 36 +++++++++++++++++------------------ src/utility-types.ts | 4 ++++ src/worker/abstract-worker.ts | 8 ++++++-- 3 files changed, 27 insertions(+), 21 deletions(-) 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 }) -- 2.34.1