Skip to content

Commit

Permalink
Tweak clock synchronization and idle latency measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Sep 23, 2024
1 parent 65b9cce commit 8cd3778
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 28 additions & 7 deletions src/crusader-lib/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -430,6 +430,7 @@ pub(crate) async fn measure_latency(
setup_start: Instant,
) -> Result<
(
Duration,
Duration,
u64,
FramedRead<OwnedReadHalf, LengthDelimitedCodec>,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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::<u128>()
/ (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(
Expand Down
2 changes: 1 addition & 1 deletion src/crusader-lib/src/latency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/crusader-lib/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/crusader-lib/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8cd3778

Please sign in to comment.