@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
76 lines • 2.43 kB
TypeScript
/**
* Aggregates `getStats()` snapshots from one or more named sources (typically
* a `Transport`, `Channel`, or `NetworkPeer.channel_for(peer)`) and reports
* recent throughput rates.
*
* Per-source stats are queried via the source's `getStats()` method. A "sample"
* captures the current totals at a wall-clock moment; the meter retains a
* sliding window of samples and computes bytes-per-second / packets-per-second
* over the window.
*
* No timer of its own — caller invokes {@link sample} per tick (or whenever).
*
* @author Alex Goldring
* @copyright Company Named Limited (c) 2025
*/
export class BandwidthMeter {
/**
* @param {{ window_seconds?: number }} [options]
*/
constructor({ window_seconds }?: {
window_seconds?: number;
});
/** @readonly */
readonly window_seconds: number;
/**
* @param {string} name human-readable label
* @param {{ getStats(): {bytes_in: number, bytes_out: number, packets_in: number, packets_out: number} }} source
*/
add_source(name: string, source: {
getStats(): {
bytes_in: number;
bytes_out: number;
packets_in: number;
packets_out: number;
};
}): void;
/**
* Capture a fresh sample at the given wall-clock time (caller-supplied so
* tests can use a deterministic clock; in production typically `Date.now()`).
*
* @param {number} now_ms
*/
sample(now_ms: number): void;
/**
* Cumulative totals across all sources, right now (re-queries getStats()).
* Independent of the sample window.
*/
cumulative(): {
bytes_in: number;
bytes_out: number;
packets_in: number;
packets_out: number;
};
/**
* Bytes-per-second received, averaged over the sample window.
* Returns 0 if fewer than 2 samples have been taken.
*/
rate_bytes_in(): number;
rate_bytes_out(): number;
rate_packets_in(): number;
rate_packets_out(): number;
/**
* Per-source breakdown of cumulative totals (re-queries getStats()).
* @returns {Array<{name: string, totals: object}>}
*/
per_source(): Array<{
name: string;
totals: object;
}>;
/**
* Drop all retained samples. Source registrations are preserved.
*/
reset_samples(): void;
#private;
}
//# sourceMappingURL=BandwidthMeter.d.ts.map