From b1838604f990179cb74ba0421eab7f5bb15abd99 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 27 Aug 2023 23:13:41 +0200 Subject: [PATCH] refactor: factor out task stolen statistics handling 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 | 56 ++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 41b23a8c..5aadac81 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1228,6 +1228,25 @@ export abstract class AbstractPool< } } + private updateTaskStolenStatisticsWorkerUsage ( + workerNodeKey: number, + workerNode: IWorkerNode, + taskName: string + ): void { + if (workerNode?.usage != null) { + ++workerNode.usage.tasks.stolen + } + if ( + this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && + workerNode.getTaskFunctionWorkerUsage(taskName) != null + ) { + const taskFunctionWorkerUsage = workerNode.getTaskFunctionWorkerUsage( + taskName + ) as WorkerUsage + ++taskFunctionWorkerUsage.tasks.stolen + } + } + private taskStealingOnEmptyQueue (workerId: number): void { const destinationWorkerNodeKey = this.getWorkerNodeKeyByWorkerId(workerId) const destinationWorkerNode = this.workerNodes[destinationWorkerNodeKey] @@ -1255,21 +1274,11 @@ export abstract class AbstractPool< } else { this.enqueueTask(destinationWorkerNodeKey, task) } - if (destinationWorkerNode?.usage != null) { - ++destinationWorkerNode.usage.tasks.stolen - } - if ( - this.shallUpdateTaskFunctionWorkerUsage(destinationWorkerNodeKey) && - destinationWorkerNode.getTaskFunctionWorkerUsage( - task.name as string - ) != null - ) { - const taskFunctionWorkerUsage = - destinationWorkerNode.getTaskFunctionWorkerUsage( - task.name as string - ) as WorkerUsage - ++taskFunctionWorkerUsage.tasks.stolen - } + this.updateTaskStolenStatisticsWorkerUsage( + destinationWorkerNodeKey, + destinationWorkerNode, + task.name as string + ) break } } @@ -1304,18 +1313,11 @@ export abstract class AbstractPool< } else { this.enqueueTask(workerNodeKey, task) } - if (workerNode?.usage != null) { - ++workerNode.usage.tasks.stolen - } - if ( - this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && - workerNode.getTaskFunctionWorkerUsage(task.name as string) != null - ) { - const taskFunctionWorkerUsage = workerNode.getTaskFunctionWorkerUsage( - task.name as string - ) as WorkerUsage - ++taskFunctionWorkerUsage.tasks.stolen - } + this.updateTaskStolenStatisticsWorkerUsage( + workerNodeKey, + workerNode, + task.name as string + ) } } } -- 2.34.1