Add average task wait time to worker pool metrics #65

Closed
opened 2026-03-11 17:39:10 +03:00 by NiXTheDev · 0 comments
NiXTheDev commented 2026-03-11 17:39:10 +03:00 (Migrated from github.com)

Overview

Track the average time tasks spend waiting in the queue before being assigned to a worker. This helps detect contention and informs scaling decisions (e.g., increasing WORKER_POOL_MAX_WORKERS).

Implementation

  1. Update workerPool.ts – Modify the run() method to:

    • Record a timestamp when a task is enqueued (enqueueTime).
    • When the task is assigned to a worker (in assignTaskToWorker()), calculate wait time = assignTime - enqueueTime.
  2. Maintain rolling average – Keep a simple moving average of wait times over a configurable window (e.g., last 100 tasks).

  3. Update getStats() and getWorkerDetails() – Include:

    • avgQueueWaitMs: number – Average wait time in milliseconds.
    • maxQueueWaitMs: number – Peak wait time since start (optional).
  4. Update metrics.ts – Display these in /metrics:

Worker Pool:
Total Workers: 8/8
Queued Tasks: 3
Avg Queue Wait: 124ms
Max Queue Wait: 567ms

Notes

  • If average wait time consistently exceeds a threshold (e.g., 500ms), it signals a bottleneck.
  • Stats are in-memory only (ephemeral).
  • Consider also tracking wait time percentiles (p95, p99) for deeper insight.
## Overview Track the average time tasks spend waiting in the queue before being assigned to a worker. This helps detect contention and informs scaling decisions (e.g., increasing `WORKER_POOL_MAX_WORKERS`). ## Implementation 1. **Update `workerPool.ts`** – Modify the `run()` method to: - Record a timestamp when a task is enqueued (`enqueueTime`). - When the task is assigned to a worker (in `assignTaskToWorker()`), calculate wait time = `assignTime - enqueueTime`. 2. **Maintain rolling average** – Keep a simple moving average of wait times over a configurable window (e.g., last 100 tasks). 3. **Update `getStats()` and `getWorkerDetails()`** – Include: - `avgQueueWaitMs: number` – Average wait time in milliseconds. - `maxQueueWaitMs: number` – Peak wait time since start (optional). 4. **Update `metrics.ts`** – Display these in `/metrics`: ``` Worker Pool: Total Workers: 8/8 Queued Tasks: 3 Avg Queue Wait: 124ms Max Queue Wait: 567ms ``` ## Notes - If average wait time consistently exceeds a threshold (e.g., 500ms), it signals a bottleneck. - Stats are in-memory only (ephemeral). - Consider also tracking wait time percentiles (p95, p99) for deeper insight.
Sign in to join this conversation.
No description provided.