Skip to content

Commit

Permalink
Change API: Take only one interval at init
Browse files Browse the repository at this point in the history
  • Loading branch information
akiradeveloper committed Mar 2, 2024
1 parent cb7576e commit 7613920
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 41 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ keywords = ["distributed"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1"
thiserror = "1"

[dev-dependencies]
proptest = "1.4"
tokio = { version = "1", features = ["full"] }
55 changes: 18 additions & 37 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
//! use phi_detector::PingWindow;
//! use std::time::Duration;
//!
//! // Create a window with intervals [100ms, 150ms].
//! let initial_intervals = [Duration::from_millis(100), Duration::from_millis(150)];
//! let mut window = PingWindow::new(&initial_intervals).unwrap();
//! // Create a window with intervals 100ms.
//! let mut window = PingWindow::new(Duration::from_millis(100));
//!
//! window.add_ping(Duration::from_millis(150));
//! window.add_ping(Duration::from_millis(80));
//! // Now the window has intervals [100ms, 150ms, 80ms]. Average is 110ms.
//!
Expand All @@ -28,13 +28,7 @@
//! }
//! ```

use std::time::{Duration, Instant};

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("No interval exists. At least one interval is required")]
NoIntervals,
}
use std::time::Duration;

fn phi_from_prob(x: f64) -> f64 {
-f64::log10(x)
Expand All @@ -47,28 +41,17 @@ pub struct PingWindow {
sum2: f64,
}
impl PingWindow {
/// Create a new instance.
/// `intervals` is the set of recent N ping intervals where N > 0.
pub fn new(intervals: &[Duration]) -> Result<Self, Error> {
let n = intervals.len();
if n == 0 {
return Err(Error::NoIntervals);
}

let mut sum = 0.;
for &x in intervals {
sum += x.as_millis() as f64;
/// Create a new instance with one reference interval.
pub fn new(interval: Duration) -> Self {
let sum = interval.as_millis() as f64;
Self {
n: 1,
sum,
sum2: 0.,
}
let mean = sum / n as f64;
let mut sum2 = 0.;
for &x in intervals {
let d = x.as_millis() as f64 - mean;
sum2 += d * d;
}
Ok(Self { n: 1, sum, sum2 })
}

/// Add new ping to the window.
/// Add a new ping to the window.
pub fn add_ping(&mut self, du: Duration) {
// Window size too large is found meaningless in experiment.
// not only that, may harm by counting in old values. (e.g. latency change, overflow)
Expand Down Expand Up @@ -142,9 +125,11 @@ impl NormalDist {
mod tests {
use super::*;

use std::time::Instant;

#[tokio::test]
async fn test_phi_detector() -> anyhow::Result<()> {
let mut window = PingWindow::new(&[Duration::from_secs(5)])?;
async fn test_phi_detector() {
let mut window = PingWindow::new(Duration::from_secs(5));
for _ in 0..100 {
window.add_ping(Duration::from_millis(100));
}
Expand All @@ -159,18 +144,14 @@ mod tests {
}
tokio::time::sleep(Duration::from_millis(10)).await;
}

Ok(())
}

#[test]
fn test_values() -> anyhow::Result<()> {
let window = PingWindow::new(&[Duration::from_secs(5)])?;
fn test_values() {
let window = PingWindow::new(Duration::from_secs(5));
let dist = window.normal_dist();
dbg!(dist.mu());
dbg!(dist.sigma());

Ok(())
}

use proptest::prelude::*;
Expand Down

0 comments on commit 7613920

Please sign in to comment.