| 79 | } |
| 80 | |
| 81 | pub async fn ping_test(endpoint: ServerEndpoint, bare: bool) -> Result<String> { |
| 82 | debug!("Running ping test on {}", endpoint); |
| 83 | let addr = format!("{}:{}", endpoint.remote_addr, endpoint.remote_port); |
| 84 | |
| 85 | let _stop_tx = start(endpoint.client.as_ref().unwrap().local_port as u16) |
| 86 | .await |
| 87 | .context("Failed to start ping service")?; |
| 88 | |
| 89 | // Wait for the server to start |
| 90 | sleep(Duration::from_millis(100)).await; |
| 91 | |
| 92 | let settings = Settings { |
| 93 | warm_up_count: 1, |
| 94 | msg_count: 10, |
| 95 | msg_size: 48, |
| 96 | sleep_time: 0, |
| 97 | }; |
| 98 | |
| 99 | let client = TcpStream::connect(&addr) |
| 100 | .await |
| 101 | .context(format!("Failed to connect to {}", addr))?; |
| 102 | let mut times = ping_tcp(client, &settings).await; |
| 103 | |
| 104 | if times.is_empty() { |
| 105 | return Ok(crate::t!("error-measurement")); |
| 106 | } |
| 107 | |
| 108 | times.sort(); |
| 109 | |
| 110 | if bare { |
| 111 | let p50 = times.len() as f64 * 0.5; |
| 112 | Ok(format!("{}", times[p50 as usize] / 1_000)) |
| 113 | } else { |
| 114 | Ok(format_stats(times)) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // TCP implementation |
| 119 | async fn ping_tcp(mut client: TcpStream, settings: &Settings) -> Vec<u32> { |