RaftEx.LeakyIntegrator (raft_ex v0.1.0)

View Source

Exponential-decay leaky integrator for rate estimation.

Useful for computing a smoothed throughput figure (e.g. commands/second) without keeping a full sliding window. The integrator decays toward zero over decay_time_ms milliseconds.

Usage

iex> li = RaftEx.LeakyIntegrator.new(1_000)   # 1-second half-life
iex> li = RaftEx.LeakyIntegrator.update(10, li)
iex> RaftEx.LeakyIntegrator.rate(li)
# → ~10 commands/second immediately after the update

Summary

Functions

Create a new integrator with the given decay time in milliseconds.

Estimated rate in units/second using the current clock.

Estimated rate in units/second at timestamp ts.

Read the current decayed value using the current monotonic clock.

Read the current decayed value at timestamp ts.

Reset the integrator to zero.

Add amount to the integrator using the current monotonic clock.

Add amount to the integrator at the given timestamp ts.

Types

t()

@type t() :: %RaftEx.LeakyIntegrator{
  decay_time_ms: pos_integer(),
  last_update: integer() | nil,
  value: float()
}

Functions

new(decay_time_ms)

@spec new(pos_integer()) :: t()

Create a new integrator with the given decay time in milliseconds.

rate(state)

@spec rate(t()) :: float()

Estimated rate in units/second using the current clock.

rate(ts, state)

@spec rate(integer(), t()) :: float()

Estimated rate in units/second at timestamp ts.

read(state)

@spec read(t()) :: float()

Read the current decayed value using the current monotonic clock.

read(ts, map)

@spec read(integer(), t()) :: float()

Read the current decayed value at timestamp ts.

reset(state)

@spec reset(t()) :: t()

Reset the integrator to zero.

update(amount, state)

@spec update(number(), t()) :: t()

Add amount to the integrator using the current monotonic clock.

update(amount, ts, state)

@spec update(number(), integer(), t()) :: t()

Add amount to the integrator at the given timestamp ts.