diff --git a/simlib/blendnet-sims/src/main.rs b/simlib/blendnet-sims/src/main.rs index f13626e..9738768 100644 --- a/simlib/blendnet-sims/src/main.rs +++ b/simlib/blendnet-sims/src/main.rs @@ -85,13 +85,26 @@ impl SimulationApp { &mut rng, &settings.simulation_settings.network_settings, ); + log!( + "Regions", + regions + .iter() + .map(|(region, node_ids)| (region, node_ids.len())) + .collect::>() + ); + log!("NumRegions", regions.len()); + let behaviours = create_behaviours(&settings.simulation_settings.network_settings); let regions_data = RegionsData::new(regions, behaviours); - let network = Arc::new(Mutex::new(Network::::new(regions_data, seed))); + let network = Arc::new(Mutex::new(Network::::new( + regions_data.clone(), + seed, + ))); let topology = Topology::new(&node_ids, settings.connected_peers_count, &mut rng); log_topology(&topology); + log_conn_latency_distribution(&topology.conn_latency_distribution(®ions_data)); let nodes: Vec<_> = node_ids .iter() @@ -261,6 +274,25 @@ struct TopologyLog { diameter: usize, } +fn log_conn_latency_distribution(distribution: &HashMap) { + log!( + "ConnLatencyDistribution", + ConnLatencyDistributionLog { + num_links: distribution.values().sum(), + distribution: distribution + .iter() + .map(|(latency, count)| (latency.as_millis(), *count)) + .collect::>(), + } + ); +} + +#[derive(Debug, Serialize, Deserialize)] +struct ConnLatencyDistributionLog { + num_links: usize, + distribution: HashMap, +} + fn main() -> anyhow::Result<()> { let app: SimulationApp = SimulationApp::parse(); let maybe_guard = log::config_tracing(app.log_format, &app.log_to, app.with_metrics); diff --git a/simlib/blendnet-sims/src/node/blend/topology.rs b/simlib/blendnet-sims/src/node/blend/topology.rs index d86d40e..4dd3574 100644 --- a/simlib/blendnet-sims/src/node/blend/topology.rs +++ b/simlib/blendnet-sims/src/node/blend/topology.rs @@ -1,6 +1,12 @@ -use std::collections::{HashMap, HashSet}; +use std::{ + collections::{HashMap, HashSet}, + time::Duration, +}; -use netrunner::node::{NodeId, NodeIdExt}; +use netrunner::{ + network::regions::RegionsData, + node::{NodeId, NodeIdExt}, +}; use rand::{seq::SliceRandom, RngCore}; #[derive(Clone)] @@ -121,6 +127,28 @@ impl Topology { hop_count } + pub fn conn_latency_distribution( + &self, + regions_data: &RegionsData, + ) -> HashMap { + // Initialize a distribution + let distribution = regions_data + .region_network_behaviour + .values() + .map(|behaviour| (behaviour.delay(), 0)) + .collect(); + // Populate the distribution + self.0.iter().fold(distribution, |mut acc, (node, peers)| { + let region_a = regions_data.node_region(*node); + peers.iter().for_each(|peer| { + let region_b = regions_data.node_region(*peer); + let behaviour = regions_data.network_behaviour_between_regions(region_a, region_b); + acc.entry(behaviour.delay()).and_modify(|count| *count += 1); + }); + acc + }) + } + pub fn get(&self, node: &NodeId) -> Option<&HashSet> { self.0.get(node) } diff --git a/simlib/netrunner/src/network/regions.rs b/simlib/netrunner/src/network/regions.rs index 45d1ff1..55852d8 100644 --- a/simlib/netrunner/src/network/regions.rs +++ b/simlib/netrunner/src/network/regions.rs @@ -121,6 +121,14 @@ impl RegionsData { pub fn network_behaviour(&self, node_a: NodeId, node_b: NodeId) -> &NetworkBehaviour { let region_a = self.node_region[&node_a]; let region_b = self.node_region[&node_b]; + self.network_behaviour_between_regions(region_a, region_b) + } + + pub fn network_behaviour_between_regions( + &self, + region_a: Region, + region_b: Region, + ) -> &NetworkBehaviour { let k = NetworkBehaviourKey::new(region_a, region_b); let k_rev = NetworkBehaviourKey::new(region_b, region_a); self.region_network_behaviour