Skip to main content

SpeedSmoother

Struct SpeedSmoother 

Source
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_old

Larger 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

Source

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.

Source

pub fn with_default_window() -> Self

Create a SpeedSmoother with default window size (N=10).

Source

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
Source

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.

Source

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.

Source

pub fn eta_seconds(&self, remaining: u64) -> Option<u64>

Calculate ETA in seconds for remaining bytes at current smoothed speed.

§Arguments
  • remaining - Number of bytes still to download/upload
§Returns
  • Some(seconds) - Estimated time remaining if speed > 0
  • None - Cannot calculate (speed is zero or negative)
Source

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.

Source

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.

Source

pub fn samples_count(&self) -> usize

Get the number of samples processed so far.

Source

pub fn alpha(&self) -> f64

Get the current alpha smoothing factor.

Trait Implementations§

Source§

impl Default for SpeedSmoother

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more