Some checks failed
rust-ci / checks (push) Has been cancelled
- Rust workspace: 7 library crates, icenetd CLI, icenet-city (Slint GUI) - Sim: fix false Delivered after a route cut, derive traffic class from payload size, per-link airtime slots (link capacity, queueing) - Docs: single docs/STATUS.md for all figures, remove v0.2/egui leftovers, drop unimplemented seed claims, uncheck store-and-forward - License: LGPL-3.0-or-later (LICENSE, LICENSES/GPL-3.0.txt)
114 lines
3.8 KiB
Rust
114 lines
3.8 KiB
Rust
//! Deterministic metrics and export formats for offline experiments.
|
|
use icenet_core::{LinkKind, NodeId, TrafficClass};
|
|
use icenet_sim::{City, DeliveryState, metrics};
|
|
|
|
#[test]
|
|
fn summary_counts_states_and_excludes_expired_from_delivery_ratio() {
|
|
let mut city = City::demo();
|
|
for index in [2, 3, 4] {
|
|
assert!(city.set_link_enabled(index, false));
|
|
}
|
|
city.send_with_ttl(NodeId(1), NodeId(3), TrafficClass::Bulk, 5_000_000, 1_000);
|
|
city.send(NodeId(1), NodeId(3), TrafficClass::Text, 128);
|
|
city.tick(5_000);
|
|
let summary = metrics::summarize(&city);
|
|
assert_eq!(summary.created, 2);
|
|
assert_eq!(summary.delivered, 1);
|
|
assert_eq!(summary.expired, 1);
|
|
assert_eq!(summary.queued, 0);
|
|
assert!((summary.delivery_ratio() - 0.5).abs() < f64::EPSILON);
|
|
assert!(summary.first_route_p50_ms.is_some());
|
|
assert!(summary.first_route_p95_ms.is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn nearest_rank_percentiles_are_deterministic() {
|
|
let mut samples = [40, 10, 30, 20];
|
|
assert_eq!(metrics::percentile(&mut samples, 50), Some(20));
|
|
assert_eq!(metrics::percentile(&mut samples, 95), Some(40));
|
|
assert_eq!(metrics::percentile(&mut [], 50), None);
|
|
assert_eq!(metrics::percentile(&mut [7], 99), Some(7));
|
|
}
|
|
|
|
#[test]
|
|
fn control_airtime_is_counted_from_the_last_route() {
|
|
let mut city = City::demo();
|
|
let id = city.send(NodeId(1), NodeId(3), TrafficClass::Control, 100);
|
|
city.tick(60_000);
|
|
assert_eq!(status_of(&city, id), DeliveryState::Delivered);
|
|
assert!(metrics::summarize(&city).control_airtime_ms > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn csv_export_has_stable_header_and_one_row_per_message() {
|
|
let mut city = City::demo();
|
|
city.send(NodeId(1), NodeId(3), TrafficClass::Text, 128);
|
|
let csv = metrics::messages_csv(&city);
|
|
let mut lines = csv.lines();
|
|
assert_eq!(
|
|
lines.next(),
|
|
Some("id,from,to,class,bytes,status,created_ms,expires_ms,first_route_ms,due_ms,route")
|
|
);
|
|
assert_eq!(lines.clone().count(), 1);
|
|
assert!(lines.next().is_some_and(|row| row.contains(",Text,")));
|
|
}
|
|
|
|
#[test]
|
|
fn json_scenario_is_deterministic_and_escapes_labels() {
|
|
let mut first = City::demo();
|
|
first.devices[0].label = "A \"quoted\" \\ path".to_owned();
|
|
first.send(NodeId(1), NodeId(3), TrafficClass::Text, 128);
|
|
let mut second = first.clone();
|
|
second.tick(1_000);
|
|
|
|
let first_json = metrics::scenario_json(&first);
|
|
let first_again = metrics::scenario_json(&first);
|
|
assert_eq!(first_json, first_again);
|
|
assert!(first_json.contains(r#""label":"A \"quoted\" \\ path""#));
|
|
assert!(first_json.contains(r#""status":"InFlight""#));
|
|
assert!(second_json_after_tick(&second).contains("Delivered"));
|
|
}
|
|
|
|
#[test]
|
|
fn json_escapes_control_characters() {
|
|
let mut city = City::demo();
|
|
city.devices[0].label = "a\u{1}b".to_owned();
|
|
let json = metrics::scenario_json(&city);
|
|
assert!(json.contains(r#""label":"a\u0001b""#), "json: {json}");
|
|
}
|
|
|
|
#[test]
|
|
fn json_uses_null_for_non_finite_coordinates() {
|
|
let mut city = City::demo();
|
|
city.devices[0].x = f32::NAN;
|
|
city.devices[0].y = f32::INFINITY;
|
|
let json = metrics::scenario_json(&city);
|
|
assert!(json.contains("\"x\":null,\"y\":null"), "json: {json}");
|
|
}
|
|
|
|
#[test]
|
|
fn json_encodes_every_supported_link_kind() {
|
|
for kind in [
|
|
LinkKind::SlowRadio,
|
|
LinkKind::FastWireless,
|
|
LinkKind::Wired,
|
|
LinkKind::Internet,
|
|
] {
|
|
let mut city = City::demo();
|
|
city.links[0].kind = kind;
|
|
let json = metrics::scenario_json(&city);
|
|
assert!(json.contains(&format!("\"kind\":\"{kind:?}\"")));
|
|
}
|
|
}
|
|
|
|
fn status_of(city: &City, id: u64) -> DeliveryState {
|
|
city.messages
|
|
.iter()
|
|
.find(|message| message.id == id)
|
|
.expect("recorded message")
|
|
.status
|
|
}
|
|
|
|
fn second_json_after_tick(city: &City) -> String {
|
|
metrics::scenario_json(city)
|
|
}
|