Skip to content

Commit

Permalink
feat(testset): add first proper testset
Browse files Browse the repository at this point in the history
  • Loading branch information
PlexSheep committed Jan 9, 2025
1 parent 70c4b85 commit 8f04947
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ blake3 = "1.5.4"
serde_repr = "0.1.19"
sysinfo = "0.33.1"
plotters = { version = "0.3.7", optional = true }
rand = { version = "0.8.5", features = ["small_rng"] }
rand_xoshiro = "0.6.0"

[[bin]] # client
name = "netpulse"
Expand Down
3 changes: 2 additions & 1 deletion src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use self::outage::Outage;

pub mod graph;
pub mod outage;
pub mod testset;

/// Formatting rules for timestamps that are easily readable by humans.
///
Expand Down Expand Up @@ -224,7 +225,7 @@ pub fn outages_detailed(all: &[&Check], f: &mut String, dump: bool) -> Result<()
Ok(())
}

fn group_by_time<'check>(
pub fn group_by_time<'check>(
checks: impl IntoIterator<Item = &'check Check>,
) -> HashMap<i64, CheckGroup<'check>> {
let mut groups: HashMap<i64, CheckGroup<'check>> = HashMap::new();
Expand Down
1 change: 1 addition & 0 deletions src/analyze/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub fn draw_checks(checks: &[Check], file: impl AsRef<Path>) -> Result<(), Analy
reason: e.to_string(),
})?;

// FIXME: sometimes deeper div by 0 error
chart
.configure_mesh()
// .disable_x_mesh()
Expand Down
103 changes: 103 additions & 0 deletions src/analyze/testset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use blake3::Hash;
use chrono::{DateTime, Datelike, Local, TimeDelta, TimeZone, Timelike};
use flagset::FlagSet;
use rand::{rngs::SmallRng, Rng, SeedableRng};
use rand_xoshiro::Xoshiro128Plus;
use tracing::{debug, trace};

use crate::{
records::{Check, CheckFlag, TARGETS},
store::Store,
};

pub const N: usize = 30_000;
pub const DEFAULT_SEED: u64 = 1686429357;
pub const BLAKE3_HASH_OF_DEFAULT_DATASET: &str =
"01d95857943ce1b6c363d278b0303215d14290ccb5ea8c1b885f93a63cf19ce5";

pub fn base_time() -> DateTime<Local> {
let utc = DateTime::from_timestamp(1686429357, 0)
.unwrap()
.with_second(0)
.unwrap();
utc.into()
}

pub fn default_dataset() -> Store {
let a = generate_dataset(DEFAULT_SEED);
let hash = a.get_hash().to_string();
if hash != BLAKE3_HASH_OF_DEFAULT_DATASET {
panic!("the hash of the generated default dataset is wrong.\n{hash}\nis not\n{BLAKE3_HASH_OF_DEFAULT_DATASET}")
}
a
}

pub fn generate_dataset(seed: u64) -> Store {
let mut rng: Xoshiro128Plus = Xoshiro128Plus::seed_from_u64(seed);
let mut buf = Vec::new();
let base_time = base_time();
let mut r: u32 = rng.gen();
let mut time;
debug!("first r: {r}");
for idx in 0..N {
r = rng.gen();
time = base_time + TimeDelta::minutes(idx as i64);

r = rng.gen();
buf.push(Check::new(
time,
FlagSet::from(CheckFlag::TypeIcmp)
| if !success(r, idx) {
CheckFlag::Unreachable
} else {
CheckFlag::Success
},
Some((r % 100) as u16),
TARGETS[idx % TARGETS.len()].parse().unwrap(),
));
r = rng.gen();
buf.push(Check::new(
time,
FlagSet::from(CheckFlag::TypeIcmp)
| if !success(r, idx) {
CheckFlag::Unreachable
} else {
CheckFlag::Success
},
Some((r % 100) as u16),
TARGETS[idx % TARGETS.len()].parse().unwrap(),
));
r = rng.gen();
buf.push(Check::new(
time,
FlagSet::from(CheckFlag::TypeHTTP)
| if !success(r, idx) {
CheckFlag::Unreachable
} else {
CheckFlag::Success
},
Some((r % 100) as u16),
TARGETS[idx % TARGETS.len()].parse().unwrap(),
));
r = rng.gen();
buf.push(Check::new(
time,
FlagSet::from(CheckFlag::TypeHTTP)
| if !success(r, idx) {
CheckFlag::Unreachable
} else {
CheckFlag::Success
},
Some((r % 100) as u16),
TARGETS[idx % TARGETS.len()].parse().unwrap(),
));
}
debug!("last r: {r}");

buf.sort();
Store::from_raw_in_mem(buf)
}

fn success(r: u32, idx: usize) -> bool {
!(2020usize..2280).contains(&idx) && !(15020usize..15080).contains(&idx) && r % 4000 != 1
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! store.save().unwrap();
//! ```
#![warn(missing_docs)]
// TODO: reenable this #![warn(missing_docs)]

/// How long to wait until considering a connection as timed out, in milliseconds
pub const TIMEOUT_MS: u16 = 10_000;
Expand Down
7 changes: 7 additions & 0 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ impl Store {
}
}

pub fn from_raw_in_mem(checks: impl Into<Vec<Check>>) -> Self {
let mut this = Self::new();
this.set_readonly();
this.checks = checks.into();
this
}

/// Sets up the store directory with proper permissions.
///
/// This function must be called with root privileges before starting the daemon. It:
Expand Down

0 comments on commit 8f04947

Please sign in to comment.