*/
protected lastTaskTimestamp!: number
/**
- * Performance statistics computation.
+ * Performance statistics computation requirements.
*/
protected statistics!: WorkerStatistics
/**
} else if (message.parent != null) {
// Main worker reference message received
this.mainWorker = message.parent
+ } else if (message.statistics != null) {
+ // Statistics message received
+ this.statistics = message.statistics
} else if (message.kill != null) {
// Kill message received
this.aliveInterval != null && clearInterval(this.aliveInterval)
this.emitDestroy()
- } else if (message.statistics != null) {
- // Statistics message received
- this.statistics = message.statistics
}
}
message: MessageValue<Data>
): void {
try {
- let taskPerformance = this.beginTaskPerformance(message)
+ let taskPerformance = this.beginTaskPerformance()
const res = fn(message.data)
taskPerformance = this.endTaskPerformance(taskPerformance)
this.sendToMainWorker({
fn: WorkerAsyncFunction<Data, Response>,
message: MessageValue<Data>
): void {
- let taskPerformance = this.beginTaskPerformance(message)
+ let taskPerformance = this.beginTaskPerformance()
fn(message.data)
.then(res => {
taskPerformance = this.endTaskPerformance(taskPerformance)
return fn
}
- private beginTaskPerformance (message: MessageValue<Data>): TaskPerformance {
- const timestamp = performance.now()
+ private beginTaskPerformance (): TaskPerformance {
+ this.checkStatistics()
return {
- timestamp,
- ...(this.statistics.waitTime && {
- waitTime: timestamp - (message.timestamp ?? timestamp)
- }),
+ timestamp: performance.now(),
...(this.statistics.elu && { elu: performance.eventLoopUtilization() })
}
}
private endTaskPerformance (
taskPerformance: TaskPerformance
): TaskPerformance {
+ this.checkStatistics()
return {
...taskPerformance,
...(this.statistics.runTime && {
})
}
}
+
+ private checkStatistics (): void {
+ if (this.statistics == null) {
+ throw new Error('Performance statistics computation requirements not set')
+ }
+ }
}