| 195 | } |
| 196 | |
| 197 | fn format_stats(times: Vec<u32>) -> String { |
| 198 | let p50 = times.len() as f64 * 0.5; |
| 199 | let p95 = times.len() as f64 * 0.95; |
| 200 | let p99 = times.len() as f64 * 0.99; |
| 201 | |
| 202 | // Convert nanoseconds to appropriate time units for better readability |
| 203 | let format_duration = |ns: u32| -> String { |
| 204 | if ns < 1_000 { |
| 205 | format!("{} ns", ns) |
| 206 | } else if ns < 1_000_000 { |
| 207 | format!("{:.2} µs", ns as f64 / 1_000.0) |
| 208 | } else if ns < 1_000_000_000 { |
| 209 | format!("{:.2} ms", ns as f64 / 1_000_000.0) |
| 210 | } else { |
| 211 | format!("{:.2} s", ns as f64 / 1_000_000_000.0) |
| 212 | } |
| 213 | }; |
| 214 | |
| 215 | format!( |
| 216 | "{}:\n p50: {}\n p95: {}\n p99: {}\n max: {}", |
| 217 | crate::t!("ping-time-percentiles"), |
| 218 | format_duration(times[p50 as usize]), |
| 219 | format_duration(times[p95 as usize]), |
| 220 | format_duration(times[p99 as usize]), |
| 221 | format_duration(*times.last().unwrap()), |
| 222 | ) |
| 223 | } |