pub struct SpeedSmoother { /* private fields */ }Expand description
Speed smoother using Exponential Moving Average (EMA) algorithm.
This struct provides smoothed download/upload speed calculations that:
- Reduce noise from fluctuating network conditions
- React quickly to sustained speed changes
- Detect temporary bursts vs sustained speed changes
- Calculate accurate ETA estimates
§Algorithm
Uses EMA with configurable window size N:
alpha = 2 / (N + 1)
EMA_new = alpha * value + (1 - alpha) * EMA_oldLarger N values provide more smoothing but slower reaction to changes. Default N=10 provides good balance for typical download scenarios.
§Example Usage
use aria2_core::util::speed_smooth::SpeedSmoother;
let mut smoother = SpeedSmoother::new(10);
smoother.record_bytes(1024); // Record 1KB downloaded
// ... after some time ...
let speed = smoother.smoothed_speed();
let remaining_bytes = 50000u64;
let eta = smoother.eta_seconds(remaining_bytes);Implementations§
Source§impl SpeedSmoother
impl SpeedSmoother
Sourcepub fn new(window_size: usize) -> Self
pub fn new(window_size: usize) -> Self
Create a new SpeedSmoother with specified window size.
§Arguments
window_size- Number of samples for EMA window (default: 10)
The alpha smoothing factor is calculated as 2 / (N + 1) where
N is the window size. Larger values provide more smoothing.
Sourcepub fn with_default_window() -> Self
pub fn with_default_window() -> Self
Create a SpeedSmoother with default window size (N=10).
Sourcepub fn record_bytes(&mut self, bytes: u64)
pub fn record_bytes(&mut self, bytes: u64)
Record bytes transferred and potentially update EMA.
Bytes are accumulated until the sample interval (500ms) has elapsed, at which point the instantaneous speed is calculated and used to update the EMA value.
§Arguments
bytes- Number of bytes transferred since last call
Sourcepub fn smoothed_speed(&self) -> f64
pub fn smoothed_speed(&self) -> f64
Get the current EMA-smoothed speed in bytes per second.
Returns the smoothed speed value, clamped to non-negative. If no samples have been recorded yet, returns 0.0.
Sourcepub fn instant_speed(&self) -> f64
pub fn instant_speed(&self) -> f64
Get the instantaneous (raw) speed from current sample window.
Calculates speed based on bytes accumulated so far in the current sampling interval. Returns the last calculated instant speed if no data has been accumulated in the current window.
Sourcepub fn eta_seconds(&self, remaining: u64) -> Option<u64>
pub fn eta_seconds(&self, remaining: u64) -> Option<u64>
Sourcepub fn is_burst(&self) -> bool
pub fn is_burst(&self) -> bool
Check if current speed indicates a burst condition.
A burst is detected when the instantaneous speed exceeds BURST_THRESHOLD_MULTIPLIER (3x) times the smoothed EMA speed. This can indicate temporary buffer flushes or compression artifacts.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset all internal state to initial values.
Clears all accumulators, counters, and timestamps. Useful when starting a new download or after a pause/resume.
Sourcepub fn samples_count(&self) -> usize
pub fn samples_count(&self) -> usize
Get the number of samples processed so far.