UNPKG

npaw-plugin-nwf

Version:
50 lines (49 loc) 1.99 kB
/** * @class * @description Tracks per-CDN time-at-each-concurrency-level. Mirrors the * Android `StatsCollector` concurrency tracking: * * - L0 = fraction of time with zero in-flight requests (CDN idle). * - L1, L2, ... = fraction with 1, 2, ... concurrent requests. * - `snapshotEmaRatio()` returns the EMA fractions (tau = 60_000ms). * - `snapshotDeltaSinceLastTick()` returns the histogram of the last * tick window AND folds it into the EMA (resets the per-tick accumulator). * * Typical caller pattern: every 5s do `delta = snapshotDeltaSinceLastTick()` * (which also updates the EMA), then `ema = snapshotEmaRatio()`. * * `getL0()` is the convenience used by the dynamic-trial-timeout (PR4). * @exports ConcurrencyEma */ export default class ConcurrencyEma { static readonly TAU_MS = 60000; private readonly nowFn; private currentLevel; private lastTransitionAt; private lastEmaUpdateAt; private startedAt; private deltaTimePerLevel; private emaRatio; constructor(now?: () => number); /** A new in-flight request started on this CDN. */ recordStart(): void; /** An in-flight request on this CDN ended (success, fail or abort). */ recordEnd(): void; /** * Snapshot the per-level fraction-of-time histogram for the last tick * window, fold it into the EMA, and reset the per-tick accumulator. * Returns the pre-reset fractions (sum to 1.0 when there is data). */ snapshotDeltaSinceLastTick(): Map<number, number>; /** Return a copy of the current EMA fractions. */ snapshotEmaRatio(): Map<number, number>; /** Convenience: current EMA fraction-of-time idle (0 in-flight). */ getL0(): number; /** Total elapsed time since this collector was created. */ getSessionMs(): number; /** Current in-flight count (mostly for debugging). */ getCurrentLevel(): number; private accrueElapsed; private foldIntoEma; private static sumValues; }