feat: account tasks wait time more accurately
[poolifier.git] / src / worker / abstract-worker.ts
index 163736696b8ec7080ee17556a52e18aa40e07466..176a5893caeeec7cb9d82095840b97498b3822b0 100644 (file)
@@ -87,7 +87,7 @@ export abstract class AbstractWorker<
     this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
     this.opts.maxInactiveTime =
       opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
-    this.opts.async = opts.async ?? false
+    delete this.opts.async
   }
 
   /**
@@ -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<Data>
   ): 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
       })