type ThreadPoolOptions,
type ThreadWorkerWithMessageChannel
} from './pools/thread/fixed'
-export type { AbstractWorker, TaskPerformance } from './worker/abstract-worker'
+export type { AbstractWorker } from './worker/abstract-worker'
export { ClusterWorker } from './worker/cluster-worker'
export { ThreadWorker } from './worker/thread-worker'
export { KillBehaviors } from './worker/worker-options'
Draft,
MessageValue,
PromiseResponseWrapper,
+ TaskPerformance,
WorkerStatistics
} from './utility-types'
export type { CircularArray } from './circular-array'
message: MessageValue<Response>
): void {
if (this.workerChoiceStrategyContext.getTaskStatistics().runTime) {
- workerTasksUsage.runTime += message.runTime ?? 0
+ workerTasksUsage.runTime += message.taskPerformance?.runTime ?? 0
if (
this.workerChoiceStrategyContext.getTaskStatistics().avgRunTime &&
workerTasksUsage.ran !== 0
}
if (
this.workerChoiceStrategyContext.getTaskStatistics().medRunTime &&
- message.runTime != null
+ message.taskPerformance?.runTime != null
) {
- workerTasksUsage.runTimeHistory.push(message.runTime)
+ workerTasksUsage.runTimeHistory.push(message.taskPerformance.runTime)
workerTasksUsage.medRunTime = median(workerTasksUsage.runTimeHistory)
}
}
message: MessageValue<Response>
): void {
if (this.workerChoiceStrategyContext.getTaskStatistics().waitTime) {
- workerTasksUsage.waitTime += message.waitTime ?? 0
+ workerTasksUsage.waitTime += message.taskPerformance?.waitTime ?? 0
if (
this.workerChoiceStrategyContext.getTaskStatistics().avgWaitTime &&
workerTasksUsage.ran !== 0
}
if (
this.workerChoiceStrategyContext.getTaskStatistics().medWaitTime &&
- message.waitTime != null
+ message.taskPerformance?.waitTime != null
) {
- workerTasksUsage.waitTimeHistory.push(message.waitTime)
+ workerTasksUsage.waitTimeHistory.push(message.taskPerformance.waitTime)
workerTasksUsage.medWaitTime = median(workerTasksUsage.waitTimeHistory)
}
}
message: MessageValue<Response>
): void {
if (this.workerChoiceStrategyContext.getTaskStatistics().elu) {
- if (workerTasksUsage.elu != null && message.elu != null) {
+ if (
+ workerTasksUsage.elu != null &&
+ message.taskPerformance?.elu != null
+ ) {
workerTasksUsage.elu = {
- idle: workerTasksUsage.elu.idle + message.elu.idle,
- active: workerTasksUsage.elu.active + message.elu.active,
+ idle: workerTasksUsage.elu.idle + message.taskPerformance.elu.idle,
+ active:
+ workerTasksUsage.elu.active + message.taskPerformance.elu.active,
utilization:
- (workerTasksUsage.elu.utilization + message.elu.utilization) / 2
+ (workerTasksUsage.elu.utilization +
+ message.taskPerformance.elu.utilization) /
+ 2
}
- } else if (message.elu != null) {
- workerTasksUsage.elu = message.elu
+ } else if (message.taskPerformance?.elu != null) {
+ workerTasksUsage.elu = message.taskPerformance.elu
}
}
}
*/
export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
+/**
+ * Task performance.
+ */
+export interface TaskPerformance {
+ /**
+ * Task performance timestamp.
+ */
+ timestamp: number
+ /**
+ * Task runtime.
+ */
+ runTime?: number
+ /**
+ * Task wait time.
+ */
+ waitTime?: number
+ /**
+ * Task event loop utilization.
+ */
+ elu?: EventLoopUtilization
+}
+
/**
* Performance statistics computation.
*/
*/
readonly errorData?: unknown
/**
- * Runtime.
- */
- readonly runTime?: number
- /**
- * Wait time.
- */
- readonly waitTime?: number
- /**
- * Event loop utilization.
+ * Task performance.
*/
- readonly elu?: EventLoopUtilization
+ readonly taskPerformance?: TaskPerformance
/**
* Reference to main worker.
*/
import { AsyncResource } from 'node:async_hooks'
import type { Worker } from 'node:cluster'
import type { MessagePort } from 'node:worker_threads'
-import { type EventLoopUtilization, performance } from 'node:perf_hooks'
-import type { MessageValue, WorkerStatistics } from '../utility-types'
+import { performance } from 'node:perf_hooks'
+import type {
+ MessageValue,
+ TaskPerformance,
+ WorkerStatistics
+} from '../utility-types'
import { EMPTY_FUNCTION, isPlainObject } from '../utils'
import {
type KillBehavior,
const DEFAULT_MAX_INACTIVE_TIME = 60000
const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
-/**
- * Task performance.
- */
-export interface TaskPerformance {
- timestamp: number
- waitTime?: number
- runTime?: number
- elu?: EventLoopUtilization
-}
-
/**
* Base class that implements some shared logic for all poolifier workers.
*
message: MessageValue<Data>
): void {
try {
- const taskPerformance = this.beginTaskPerformance(message)
+ let taskPerformance = this.beginTaskPerformance(message)
const res = fn(message.data)
- const { runTime, waitTime, elu } =
- this.endTaskPerformance(taskPerformance)
+ taskPerformance = this.endTaskPerformance(taskPerformance)
this.sendToMainWorker({
data: res,
- runTime,
- waitTime,
- elu,
+ taskPerformance,
id: message.id
})
} catch (e) {
fn: WorkerAsyncFunction<Data, Response>,
message: MessageValue<Data>
): void {
- const taskPerformance = this.beginTaskPerformance(message)
+ let taskPerformance = this.beginTaskPerformance(message)
fn(message.data)
.then(res => {
- const { runTime, waitTime, elu } =
- this.endTaskPerformance(taskPerformance)
+ taskPerformance = this.endTaskPerformance(taskPerformance)
this.sendToMainWorker({
data: res,
- runTime,
- waitTime,
- elu,
+ taskPerformance,
id: message.id
})
return null