use std::collections::{BTreeMap, BTreeSet};
use std::time::{Duration, Instant};
pub(crate) fn percentile_us(latencies: &[u64], p: f64) -> u64 {
if latencies.is_empty() {
return 0;
}
let mut sorted = latencies.to_vec();
sorted.sort_unstable();
sorted_percentile_us(&sorted, p)
}
pub(crate) fn sorted_percentile_us(sorted: &[u64], p: f64) -> u64 {
if sorted.is_empty() {
return 0;
}
let idx = ((sorted.len() as f64 * p / 100.0) as usize).min(sorted.len() - 1);
sorted[idx]
}
pub(crate) fn percentiles_us(latencies: &[u64]) -> (u64, u64) {
(
percentile_us(latencies, 50.0),
percentile_us(latencies, 99.0),
)
}
pub(crate) fn mean_us(latencies: &[u64]) -> f64 {
match latencies.len() {
0 => 0.0,
n => latencies.iter().sum::<u64>() as f64 / n as f64,
}
}
pub(crate) fn stdev_us(latencies: &[u64]) -> f64 {
if latencies.len() < 2 {
return 0.0;
}
let mean = mean_us(latencies);
let var = latencies
.iter()
.map(|v| {
let d = *v as f64 - mean;
d * d
})
.sum::<f64>()
/ latencies.len() as f64;
var.sqrt()
}
pub(crate) fn rate_spread(rates: &[u64]) -> Option<(f64, f64, f64)> {
if rates.len() < 2 {
return None;
}
let mean = rates.iter().sum::<u64>() as f64 / rates.len() as f64;
let var = rates
.iter()
.map(|v| {
let d = *v as f64 - mean;
d * d
})
.sum::<f64>()
/ rates.len() as f64;
let stdev = var.sqrt();
let cv = match mean > 0.0 {
true => stdev / mean * 100.0,
false => 0.0,
};
Some((mean, stdev, cv))
}
pub(crate) const MIN_RATE_SAMPLES: usize = 3;
pub(crate) const MAX_BUCKET_ROWS: usize = 12;
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Buckets {
pub bounds: Vec<u64>,
pub counts: Vec<u64>,
}
fn nice_step(raw: f64) -> u64 {
if raw <= 1.0 {
return 1;
}
let mag = 10f64.powf(raw.log10().floor());
let norm = raw / mag;
let step = if norm <= 1.0 {
1.0
} else if norm <= 2.0 {
2.0
} else if norm <= 2.5 {
2.5
} else if norm <= 5.0 {
5.0
} else {
10.0
};
((step * mag).round() as u64).max(1)
}
fn two_sig_figs(v: f64) -> u64 {
if v <= 0.0 {
return 1;
}
let mag = 10f64.powf(v.log10().floor() - 1.0);
((v / mag).round() * mag).round() as u64
}
impl Buckets {
pub(crate) fn of(samples: &[u64], max_rows: usize) -> Buckets {
let max_rows = max_rows.max(3);
if samples.is_empty() {
return Buckets {
bounds: vec![u64::MAX],
counts: vec![0],
};
}
let mut sorted = samples.to_vec();
sorted.sort_unstable();
let lo = sorted_percentile_us(&sorted, 1.0).max(1);
let hi = sorted_percentile_us(&sorted, 99.0).max(lo + 1);
let bounds = match hi as f64 / lo as f64 >= 3.0 {
true => log_bounds(lo, hi, max_rows),
false => linear_bounds(lo, hi, max_rows),
};
Self::with_bounds(&sorted, bounds)
}
pub(crate) fn linear(samples: &[u64], max_rows: usize) -> Buckets {
let max_rows = max_rows.max(3);
if samples.is_empty() {
return Buckets {
bounds: vec![u64::MAX],
counts: vec![0],
};
}
let mut sorted = samples.to_vec();
sorted.sort_unstable();
let lo = *sorted.first().unwrap();
let hi = (*sorted.last().unwrap()).max(lo + 1);
Self::with_bounds(&sorted, linear_bounds(lo.max(1), hi, max_rows))
}
fn with_bounds(sorted: &[u64], mut bounds: Vec<u64>) -> Buckets {
bounds.push(u64::MAX);
let mut counts = vec![0u64; bounds.len()];
for v in sorted {
let i = bounds
.iter()
.position(|b| v <= b)
.unwrap_or(bounds.len() - 1);
counts[i] += 1;
}
Buckets { bounds, counts }
}
pub(crate) fn total(&self) -> u64 {
self.counts.iter().sum()
}
pub(crate) fn peak(&self) -> u64 {
self.counts.iter().copied().max().unwrap_or(0)
}
pub(crate) fn label(&self, i: usize, fmt: &dyn Fn(u64) -> String) -> String {
match self.bounds.get(i) {
Some(&b) if b != u64::MAX => format!("≤{}", fmt(b)),
_ => match i {
0 => "all".to_string(),
i => format!(">{}", fmt(self.bounds[i - 1])),
},
}
}
pub(crate) fn interesting(&self) -> std::ops::Range<usize> {
let first = self.counts.iter().position(|c| *c > 0);
let last = self.counts.iter().rposition(|c| *c > 0);
match (first, last) {
(Some(f), Some(l)) => f.saturating_sub(1)..(l + 2).min(self.counts.len()),
_ => 0..self.counts.len(),
}
}
}
fn linear_bounds(lo: u64, hi: u64, max_rows: usize) -> Vec<u64> {
let rows = max_rows.saturating_sub(1).max(2);
let step = nice_step((hi - lo) as f64 / rows as f64);
let start = (lo / step) * step;
let mut bounds = Vec::new();
let mut b = start + step;
while b <= hi + step && bounds.len() < rows {
bounds.push(b);
b += step;
}
if bounds.is_empty() {
bounds.push(hi);
}
bounds
}
fn log_bounds(lo: u64, hi: u64, max_rows: usize) -> Vec<u64> {
let decades = (hi as f64 / lo as f64).log10().max(0.05);
let per_decade = [8.0, 4.0, 2.0, 1.0]
.into_iter()
.find(|p| ((decades * p).ceil() as usize) < max_rows)
.unwrap_or(1.0);
let first = (lo as f64).log10() * per_decade;
let last = (hi as f64).log10() * per_decade;
let mut bounds = Vec::new();
let mut k = first.floor() + 1.0;
while k <= last.ceil() && bounds.len() + 1 < max_rows {
let v = two_sig_figs(10f64.powf(k / per_decade));
if bounds.last() != Some(&v) {
bounds.push(v);
}
k += 1.0;
}
if bounds.is_empty() {
bounds.push(hi);
}
bounds
}
#[derive(Debug, Clone, Default)]
pub(crate) struct RateRing {
per_second: Vec<u64>,
last_second: u64,
last_done: u64,
}
impl RateRing {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn sample(&mut self, done: u64, elapsed: Duration) {
let sec = elapsed.as_secs();
while self.last_second < sec {
self.per_second.push(done.saturating_sub(self.last_done));
self.last_done = done;
self.last_second += 1;
if self.last_second < sec {
self.per_second.pop();
self.per_second.push(0);
}
}
}
pub(crate) fn finish(&mut self, done: u64, elapsed: Duration) {
self.sample(done, elapsed);
let tail = elapsed.as_secs_f64() - self.last_second as f64;
if tail >= 0.5 {
let counted = done.saturating_sub(self.last_done);
self.per_second.push((counted as f64 / tail).round() as u64);
self.last_done = done;
}
}
pub(crate) fn per_second(&self) -> &[u64] {
&self.per_second
}
pub(crate) fn now(&self, window: usize, done: u64, elapsed: Duration) -> f64 {
let tail: Vec<u64> = self.per_second.iter().rev().take(window).copied().collect();
match tail.len() {
0 => done as f64 / elapsed.as_secs_f64().max(0.001),
n => tail.iter().sum::<u64>() as f64 / n as f64,
}
}
pub(crate) fn peak(&self) -> u64 {
self.per_second.iter().copied().max().unwrap_or(0)
}
pub(crate) fn dip(&self) -> Option<u64> {
match self.per_second.len() {
0..=2 => None,
_ => self.per_second[1..].iter().copied().min(),
}
}
pub(crate) fn as_f64(&self) -> Vec<f64> {
self.per_second.iter().map(|n| *n as f64).collect()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Placement {
Free,
Billed,
}
impl Placement {
pub(crate) fn as_str(&self) -> &'static str {
match self {
Placement::Free => "free",
Placement::Billed => "billed",
}
}
pub(crate) fn of(charged: f64) -> Placement {
match charged > 0.0 {
true => Placement::Billed,
false => Placement::Free,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub(crate) struct Expectation {
expected_ms_sum: f64,
expected_n: u64,
delivered_ms_sum: f64,
delivered_n: u64,
}
impl Expectation {
pub(crate) fn record(&mut self, expected_ms: Option<f64>, delivered_ms: f64) {
if let Some(e) = expected_ms {
self.expected_ms_sum += e;
self.expected_n += 1;
}
self.delivered_ms_sum += delivered_ms;
self.delivered_n += 1;
}
pub(crate) fn merge(&mut self, other: &Expectation) {
self.expected_ms_sum += other.expected_ms_sum;
self.expected_n += other.expected_n;
self.delivered_ms_sum += other.delivered_ms_sum;
self.delivered_n += other.delivered_n;
}
pub(crate) fn avg_expected_ms(&self) -> Option<f64> {
(self.expected_n > 0).then(|| self.expected_ms_sum / self.expected_n as f64)
}
pub(crate) fn avg_delivered_ms(&self) -> f64 {
match self.delivered_n {
0 => 0.0,
n => self.delivered_ms_sum / n as f64,
}
}
pub(crate) fn ratio(&self) -> Option<f64> {
let expected = self.avg_expected_ms()?;
let delivered = self.avg_delivered_ms();
(expected > 0.0 && delivered > 0.0).then_some(delivered / expected)
}
pub(crate) fn is_miscalibrated(&self) -> bool {
matches!(self.ratio(), Some(r) if !(0.5..=2.0).contains(&r))
}
}
#[derive(Debug, Clone, Default)]
pub(crate) struct NodeStat {
pub(crate) requests: u64,
pub(crate) charged: f64,
pub(crate) latencies_us: Vec<u64>,
pub(crate) expectation: Expectation,
pub(crate) workers: BTreeMap<String, u64>,
pub(crate) price_per_hour: Option<f64>,
pub(crate) transports: BTreeSet<String>,
}
impl NodeStat {
pub(crate) fn p50_us(&self) -> u64 {
percentile_us(&self.latencies_us, 50.0)
}
pub(crate) fn price(&self) -> String {
match self.price_per_hour {
None => "—".to_string(),
Some(p) if p <= 0.0 => "free".to_string(),
Some(p) => format!("{p} yen/h"),
}
}
pub(crate) fn priced_but_unbilled(&self) -> bool {
matches!(self.price_per_hour, Some(p) if p > 0.0) && self.charged == 0.0
}
pub(crate) fn via(&self) -> String {
match self.transports.len() {
0 => "—".to_string(),
_ => self
.transports
.iter()
.cloned()
.collect::<Vec<_>>()
.join("+"),
}
}
}
#[derive(Debug, Clone, Default)]
pub(crate) struct Ledger {
nodes: BTreeMap<(Placement, String), NodeStat>,
}
impl Ledger {
pub(crate) fn new() -> Self {
Self::default()
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn record(
&mut self,
worker: &str,
transport: Option<&str>,
price_per_hour: Option<f64>,
latency_us: u64,
charged: f64,
expected_ms: Option<f64>,
) {
let placement = Placement::of(charged);
let node = node_of(worker);
let stat = self.nodes.entry((placement, node)).or_default();
if let Some(t) = transport.map(|t| t.trim().to_ascii_lowercase()) {
if !t.is_empty() {
stat.transports.insert(t);
}
}
if let Some(p) = price_per_hour {
stat.price_per_hour = Some(p);
}
stat.requests += 1;
stat.charged += charged;
stat.latencies_us.push(latency_us);
stat.expectation
.record(expected_ms, latency_us as f64 / 1000.0);
*stat.workers.entry(worker.to_string()).or_insert(0) += 1;
}
pub(crate) fn is_empty(&self) -> bool {
self.nodes.is_empty()
}
pub(crate) fn rows(&self) -> Vec<(Placement, String, &NodeStat)> {
let mut v: Vec<(Placement, String, &NodeStat)> = self
.nodes
.iter()
.map(|((p, n), s)| (*p, n.clone(), s))
.collect();
v.sort_by(|a, b| {
a.0.cmp(&b.0)
.then_with(|| b.2.requests.cmp(&a.2.requests))
.then_with(|| a.1.cmp(&b.1))
});
v
}
pub(crate) fn nodes_touched(&self) -> Vec<(String, u64)> {
let mut by_node: BTreeMap<String, u64> = BTreeMap::new();
for ((_, node), stat) in &self.nodes {
*by_node.entry(node.clone()).or_default() += stat.requests;
}
let mut v: Vec<(String, u64)> = by_node.into_iter().collect();
v.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
v
}
pub(crate) fn totals(&self, placement: Placement) -> (u64, f64) {
self.nodes
.iter()
.filter(|((p, _), _)| *p == placement)
.fold((0, 0.0), |(n, c), (_, s)| (n + s.requests, c + s.charged))
}
pub(crate) fn latencies(&self, placement: Placement) -> Vec<u64> {
self.nodes
.iter()
.filter(|((p, _), _)| *p == placement)
.flat_map(|(_, s)| s.latencies_us.iter().copied())
.collect()
}
pub(crate) fn placed(&self) -> u64 {
self.nodes.values().map(|s| s.requests).sum()
}
pub(crate) fn charged(&self) -> f64 {
self.nodes.values().map(|s| s.charged).sum()
}
pub(crate) fn miscalibrated(&self) -> Vec<(String, Expectation)> {
self.rows()
.into_iter()
.filter(|(_, _, s)| s.expectation.is_miscalibrated())
.map(|(_, n, s)| (n, s.expectation))
.collect()
}
pub(crate) fn worker_names(&self) -> BTreeSet<String> {
self.nodes
.values()
.flat_map(|s| s.workers.keys().cloned())
.collect()
}
}
pub(crate) fn node_of(worker: &str) -> String {
if let Some(rest) = worker.strip_prefix("worker-zc-worker-") {
return rest
.rsplit_once('-')
.map_or(rest, |(node, _)| node)
.to_string();
}
if let Some((node, tail)) = worker.rsplit_once("-w") {
if tail.chars().all(|c| c.is_ascii_digit()) && !node.is_empty() {
return node.to_string();
}
}
worker.to_string()
}
pub(crate) fn parse_expected_ms(reason: &str) -> Option<f64> {
let bytes = reason.as_bytes();
if bytes.len() < 2 {
return None;
}
for i in 0..=bytes.len() - 2 {
if &bytes[i..i + 2] != b"ms" {
continue;
}
let mut j = i;
let mut seen_digit = false;
let mut seen_dot = false;
while j > 0 {
let c = bytes[j - 1];
if c.is_ascii_digit() {
seen_digit = true;
j -= 1;
} else if c == b'.' && !seen_dot {
seen_dot = true;
j -= 1;
} else {
break;
}
}
if seen_digit {
if let Ok(v) = reason[j..i].parse::<f64>() {
return Some(v);
}
}
}
None
}
pub(crate) fn since(start: Instant) -> Duration {
start.elapsed()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_run_too_short_to_close_two_seconds_has_no_spread_to_report() {
assert!(rate_spread(&[]).is_none());
assert!(rate_spread(&[40]).is_none());
let (mean, stdev, cv) = rate_spread(&[30, 50]).unwrap();
assert_eq!(mean, 40.0);
assert_eq!(stdev, 10.0);
assert_eq!(cv, 25.0);
}
#[test]
fn a_tight_spread_gets_bins_that_show_its_shape() {
let samples: Vec<u64> = (0..786).map(|i| 241_000 + (i * 137) % 161_000).collect();
let b = Buckets::of(&samples, MAX_BUCKET_ROWS);
let occupied = b.counts.iter().filter(|c| **c > 0).count();
assert!(
occupied >= 4,
"a 241-402ms spread must fill several rows, filled {occupied}"
);
assert_eq!(b.total(), 786, "every sample lands in exactly one bucket");
}
#[test]
fn a_wide_spread_gets_log_bins_and_still_fits_on_a_screen() {
let mut samples: Vec<u64> = (0..500).map(|i| 200 + i * 3).collect();
samples.extend((0..500).map(|i| 2_000_000 + i * 900));
let b = Buckets::of(&samples, MAX_BUCKET_ROWS);
assert!(
b.counts.len() <= MAX_BUCKET_ROWS,
"{} rows is more than a screen",
b.counts.len()
);
assert_eq!(b.total(), 1000);
}
#[test]
fn the_buckets_account_for_every_sample_once() {
for n in [1usize, 2, 17, 500] {
let samples: Vec<u64> = (0..n as u64).map(|i| 1000 + i * i).collect();
let b = Buckets::of(&samples, MAX_BUCKET_ROWS);
assert_eq!(b.total(), n as u64, "n={n}");
assert_eq!(b.bounds.len(), b.counts.len());
}
}
#[test]
fn no_samples_is_one_empty_row_not_a_panic() {
let b = Buckets::of(&[], MAX_BUCKET_ROWS);
assert_eq!(b.total(), 0);
assert_eq!(b.counts.len(), 1);
}
#[test]
fn identical_samples_do_not_divide_by_zero() {
let b = Buckets::of(&[5_000; 40], MAX_BUCKET_ROWS);
assert_eq!(b.total(), 40);
}
#[test]
fn bucket_edges_are_numbers_a_reader_can_hold() {
assert_eq!(nice_step(6_033.0), 10_000);
assert_eq!(nice_step(1.4), 2);
assert_eq!(nice_step(230.0), 250);
assert_eq!(nice_step(0.2), 1);
}
#[test]
fn the_last_row_is_the_overflow_and_says_so() {
let b = Buckets::of(&[1000, 2000, 3000], MAX_BUCKET_ROWS);
let last = b.counts.len() - 1;
assert_eq!(b.bounds[last], u64::MAX);
assert!(b.label(last, &|v| v.to_string()).starts_with('>'));
}
#[test]
fn one_second_of_samples_is_not_a_distribution() {
let rates = [2295];
assert!(
rates.len() < MIN_RATE_SAMPLES,
"one closed second must fall below the gate every view reads"
);
let b = Buckets::linear(&rates, MAX_BUCKET_ROWS);
assert_eq!(b.total(), 1, "it still buckets; the view is what declines");
}
#[test]
fn a_rate_is_bucketed_in_even_steps_not_orders_of_magnitude() {
let rates: Vec<u64> = vec![11, 19, 28, 37, 44, 51, 58, 63, 69, 74, 78, 81, 91];
let b = Buckets::linear(&rates, MAX_BUCKET_ROWS);
assert_eq!(b.total(), 13);
let widths: Vec<u64> = b.bounds[..b.bounds.len() - 1]
.windows(2)
.map(|w| w[1] - w[0])
.collect();
assert!(
widths.windows(2).all(|w| w[0] == w[1]),
"linear bins are evenly spaced, got {widths:?}"
);
}
#[test]
fn the_rate_ring_closes_one_bucket_per_second() {
let mut r = RateRing::new();
r.sample(10, Duration::from_millis(500));
assert!(r.per_second().is_empty(), "half a second closes nothing");
r.sample(40, Duration::from_millis(1_100));
assert_eq!(r.per_second(), &[40]);
r.sample(70, Duration::from_millis(2_050));
assert_eq!(r.per_second(), &[40, 30]);
}
#[test]
fn now_is_the_recent_rate_not_the_run_average() {
let mut r = RateRing::new();
let mut done = 0;
for sec in 1..=5 {
done += 100;
r.sample(done, Duration::from_secs(sec));
}
for sec in 6..=10 {
done += 2;
r.sample(done, Duration::from_secs(sec));
}
let now = r.now(5, done, Duration::from_secs(10));
assert!(now < 10.0, "recent rate was {now}, should be ~2");
assert_eq!(r.peak(), 100);
}
#[test]
fn a_sub_second_run_still_reports_the_rate_it_achieved() {
let mut r = RateRing::new();
r.finish(4000, Duration::from_millis(980));
assert_eq!(r.per_second().len(), 1);
let peak = r.peak();
assert!(
(4000..4200).contains(&peak),
"4000 requests in 0.98s is ~4082/s, got {peak}"
);
}
#[test]
fn a_run_too_short_to_measure_invents_nothing() {
let mut r = RateRing::new();
r.finish(60, Duration::from_millis(100));
assert!(r.per_second().is_empty(), "0.1s is not a measurement");
}
#[test]
fn finishing_does_not_double_count_the_last_closed_second() {
let mut r = RateRing::new();
r.sample(100, Duration::from_secs(1));
r.finish(180, Duration::from_millis(1_800));
assert_eq!(r.per_second().len(), 2);
assert_eq!(r.per_second()[0], 100);
assert_eq!(r.per_second()[1], 100, "80 requests in 0.8s is 100/s");
}
#[test]
fn a_stalled_sampler_does_not_count_one_second_with_ten() {
let mut r = RateRing::new();
r.sample(0, Duration::from_secs(1));
r.sample(1000, Duration::from_secs(5));
assert_eq!(r.per_second().len(), 5);
assert!(
r.peak() <= 1000,
"the skipped seconds must not all land on one"
);
}
#[test]
fn what_was_charged_decides_the_split() {
assert_eq!(Placement::of(0.0), Placement::Free);
assert_eq!(Placement::of(0.004), Placement::Billed);
}
#[test]
fn a_worker_on_another_machine_is_still_free_when_nothing_was_charged() {
let mut l = Ledger::new();
l.record(
"worker-zc-worker-i9-1",
Some("local"),
Some(3.6),
750_000,
0.0,
None,
);
assert_eq!(l.totals(Placement::Free), (1, 0.0));
let rows = l.rows();
assert_eq!(rows[0].1, "i9");
assert_eq!(rows[0].2.via(), "local", "the transport is still reported");
}
#[test]
fn a_node_that_advertises_a_price_and_billed_nothing_is_flagged() {
let mut l = Ledger::new();
l.record("i9-w0", Some("local"), Some(3.6), 750_000, 0.0, None);
assert!(l.rows()[0].2.priced_but_unbilled());
assert_eq!(l.rows()[0].2.price(), "3.6 yen/h");
}
#[test]
fn a_node_that_charged_what_it_advertises_is_not_flagged() {
let mut l = Ledger::new();
l.record("i9-w0", Some("quic"), Some(3.6), 750_000, 0.001, None);
assert!(!l.rows()[0].2.priced_but_unbilled());
}
#[test]
fn a_worker_that_prices_itself_at_zero_is_not_a_finding() {
let mut l = Ledger::new();
l.record("a-w0", Some("local"), Some(0.0), 1000, 0.0, None);
assert!(!l.rows()[0].2.priced_but_unbilled());
assert_eq!(l.rows()[0].2.price(), "free");
}
#[test]
fn a_price_the_broker_never_reported_is_not_a_finding_either() {
let mut l = Ledger::new();
l.record("a-w0", Some("local"), None, 1000, 0.0, None);
assert!(!l.rows()[0].2.priced_but_unbilled());
assert_eq!(l.rows()[0].2.price(), "—");
}
#[test]
fn a_node_reached_two_ways_reports_both() {
let mut l = Ledger::new();
l.record("i9-w0", Some("quic"), Some(3.6), 400_000, 0.001, None);
l.record("i9-w0", Some("http"), Some(3.6), 400_000, 0.001, None);
assert_eq!(l.rows()[0].2.via(), "http+quic");
}
#[test]
fn a_reply_with_no_transport_still_lands_on_the_right_side() {
let mut l = Ledger::new();
l.record("a-w0", None, None, 1000, 0.0, None);
l.record("b-w0", None, None, 1000, 0.004, None);
assert_eq!(l.totals(Placement::Free).0, 1);
assert_eq!(l.totals(Placement::Billed).0, 1);
}
fn ledger() -> Ledger {
let mut l = Ledger::new();
for _ in 0..7 {
l.record(
"dd715b7f-w0",
Some("local"),
Some(28.0),
260_000,
0.0,
Some(2500.0),
);
}
for _ in 0..3 {
l.record(
"dd715b7f-w1",
Some("local"),
Some(28.0),
270_000,
0.0,
Some(2500.0),
);
}
for _ in 0..4 {
l.record(
"worker-zc-worker-i9-1",
Some("quic"),
Some(3.6),
410_000,
0.0012,
Some(408.0),
);
}
l
}
#[test]
fn the_ledger_separates_what_was_free_from_what_was_billed() {
let l = ledger();
assert_eq!(l.totals(Placement::Free), (10, 0.0));
let (n, charged) = l.totals(Placement::Billed);
assert_eq!(n, 4);
assert!((charged - 0.0048).abs() < 1e-9, "charged {charged}");
assert_eq!(l.placed(), 14);
}
#[test]
fn workers_on_one_node_are_totalled_under_it() {
let l = ledger();
let rows = l.rows();
let local = rows
.iter()
.find(|(p, _, _)| *p == Placement::Free)
.expect("a free row");
assert_eq!(local.1, "dd715b7f");
assert_eq!(local.2.requests, 10);
assert_eq!(local.2.workers.len(), 2, "both workers under one node");
}
#[test]
fn a_node_the_router_badly_misjudged_is_flagged() {
let l = ledger();
let bad = l.miscalibrated();
assert_eq!(bad.len(), 1, "only the 2.5s-vs-264ms node");
assert_eq!(bad[0].0, "dd715b7f");
let ratio = bad[0].1.ratio().unwrap();
assert!(ratio < 0.2, "delivered/expected was {ratio}");
}
#[test]
fn a_node_the_router_judged_well_is_not_flagged() {
let mut l = Ledger::new();
l.record(
"i9-w0",
Some("quic"),
Some(3.6),
410_000,
0.001,
Some(408.0),
);
assert!(l.miscalibrated().is_empty());
}
#[test]
fn a_decision_that_named_no_latency_is_never_called_miscalibrated() {
let mut l = Ledger::new();
l.record("a-w0", Some("local"), Some(0.0), 900_000, 0.0, None);
assert!(l.miscalibrated().is_empty());
}
#[test]
fn an_empty_ledger_reports_nothing_rather_than_dividing_by_zero() {
let l = Ledger::new();
assert!(l.is_empty());
assert_eq!(l.placed(), 0);
assert_eq!(l.charged(), 0.0);
assert_eq!(l.totals(Placement::Billed), (0, 0.0));
}
#[test]
fn a_worker_is_attributed_to_its_node() {
assert_eq!(node_of("worker-zc-worker-i9-1"), "i9");
assert_eq!(node_of("dd715b7f-w0"), "dd715b7f");
assert_eq!(node_of("odd-name"), "odd-name");
}
#[test]
fn parse_expected_ms_reads_the_number_immediately_before_ms() {
assert_eq!(parse_expected_ms("Best latency: 12.4ms"), Some(12.4));
assert_eq!(parse_expected_ms("Adaptive: expected 254ms"), Some(254.0));
assert_eq!(parse_expected_ms("Best price: ¥0.001"), None);
assert_eq!(parse_expected_ms(""), None);
}
#[test]
fn percentiles_come_from_the_samples_so_far() {
let s: Vec<u64> = (1..=100).collect();
assert_eq!(percentile_us(&s, 50.0), 51);
assert_eq!(percentile_us(&s, 99.0), 100);
assert_eq!(percentile_us(&[], 50.0), 0);
}
#[test]
fn a_spread_is_summarised_without_dividing_by_zero() {
assert_eq!(stdev_us(&[]), 0.0);
assert_eq!(stdev_us(&[5]), 0.0);
assert!(stdev_us(&[1, 3]) > 0.0);
assert_eq!(mean_us(&[]), 0.0);
}
}