From 8cd377816391c58c2f5bb532971498cfc5414f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Mon, 23 Sep 2024 14:45:38 +0200 Subject: [PATCH] Tweak clock synchronization and idle latency measurement --- CHANGELOG.md | 3 +++ src/crusader-lib/src/common.rs | 35 ++++++++++++++++++++++++++------- src/crusader-lib/src/latency.rs | 2 +- src/crusader-lib/src/peer.rs | 2 +- src/crusader-lib/src/test.rs | 2 +- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca4eb5f..f5924b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ This file lists the changes that have occurred since January 2024 in the project ## Unreleased +* Increase samples used for clock synchronization and idle latency measurement +* Clock synchronization now uses the average of the lowest 1/3rd of samples + ## 0.3 - 2024-09-16 * Show throughput, latency, and packet loss summaries in plots and with the `test` command diff --git a/src/crusader-lib/src/common.rs b/src/crusader-lib/src/common.rs index 3734f67..386a122 100644 --- a/src/crusader-lib/src/common.rs +++ b/src/crusader-lib/src/common.rs @@ -362,7 +362,7 @@ async fn ping_measure_send( let mut storage = Vec::with_capacity(samples as usize); let mut buf = [0; 64]; - let mut interval = time::interval(Duration::from_millis(10)); + let mut interval = time::interval(Duration::from_millis(5)); for _ in 0..samples { interval.tick().await; @@ -393,7 +393,7 @@ async fn ping_measure_recv( let mut storage = Vec::with_capacity(samples as usize); let mut buf = [0; 64]; - let end = time::sleep(Duration::from_millis(10) * samples + Duration::from_millis(1000)).fuse(); + let end = time::sleep(Duration::from_millis(5) * samples + Duration::from_millis(1000)).fuse(); pin_mut!(end); loop { @@ -430,6 +430,7 @@ pub(crate) async fn measure_latency( setup_start: Instant, ) -> Result< ( + Duration, Duration, u64, FramedRead, @@ -459,7 +460,7 @@ pub(crate) async fn measure_latency( udp_socket.connect(server).await?; let udp_socket2 = udp_socket.clone(); - let samples = 50; + let samples = 100; let ping_start_index = *ping_index; let ping_send = tokio::spawn(ping_measure_send( @@ -504,16 +505,36 @@ pub(crate) async fn measure_latency( if pings.is_empty() { bail!("Unable to measure latency to server"); } + if pings.len() < (samples / 2) as usize { + bail!("Unable get enough latency samples from server"); + } pings.sort_by_key(|d| d.1); - let (sent, latency, server_time) = pings[pings.len() / 2]; + let latency = pings.get(pings.len() / 2).unwrap().1; + + let threshold = pings.get(pings.len() / 3).unwrap().1; + + let pings: Vec<_> = pings + .get(0..=(pings.len() / 3)) + .unwrap() + .iter() + .map(|&(sent, latency, server_time)| { + let server_pong = sent + latency / 2; - let server_pong = sent + latency / 2; + let server_offset = (server_pong.as_micros() as u64).wrapping_sub(server_time); + + (server_pong, latency, server_offset) + }) + .collect(); - let server_offset = (server_pong.as_micros() as u64).wrapping_sub(server_time); + let server_offset = pings + .iter() + .map(|&(_, _, offset)| offset as u128) + .sum::() + / (pings.len() as u128); - Ok((latency, server_offset, control_rx)) + Ok((latency, threshold, server_offset as u64, control_rx)) } pub(crate) async fn ping_send( diff --git a/src/crusader-lib/src/latency.rs b/src/crusader-lib/src/latency.rs index b4476b0..1cd054d 100644 --- a/src/crusader-lib/src/latency.rs +++ b/src/crusader-lib/src/latency.rs @@ -133,7 +133,7 @@ async fn test_async( let mut ping_index = 0; - let (latency, mut server_time_offset, mut control_rx) = measure_latency( + let (_, latency, mut server_time_offset, mut control_rx) = measure_latency( id, &mut ping_index, &mut control_tx, diff --git a/src/crusader-lib/src/peer.rs b/src/crusader-lib/src/peer.rs index b4d6445..6b02936 100644 --- a/src/crusader-lib/src/peer.rs +++ b/src/crusader-lib/src/peer.rs @@ -174,7 +174,7 @@ pub async fn run_peer( let mut ping_index = 0; - let (latency, server_time_offset, mut control_rx) = measure_latency( + let (latency, _, server_time_offset, mut control_rx) = measure_latency( id, &mut ping_index, &mut control_tx, diff --git a/src/crusader-lib/src/test.rs b/src/crusader-lib/src/test.rs index ede733c..5ffa98b 100644 --- a/src/crusader-lib/src/test.rs +++ b/src/crusader-lib/src/test.rs @@ -155,7 +155,7 @@ pub(crate) async fn test_async( let mut ping_index = 0; - let (latency, server_time_offset, mut control_rx) = measure_latency( + let (latency, _, server_time_offset, mut control_rx) = measure_latency( id, &mut ping_index, &mut control_tx,