//! Ping-pong with a target and report the round-trip times. use sausage_actor::{json, messages, reply, send}; use std::time::Instant; fn main() { let mut inbox = messages(); while let Some(message) = inbox.next() { if message.kind() != "bench" { continue; } let target = message.body["target"].as_str().unwrap_or("echo"); let n = message.body["n"].as_u64().unwrap_or(1000); let mut times = Vec::new(); let started = Instant::now(); for i in 0..n { let t = Instant::now(); let id = send(target, json!({"type": "ping", "i": i})); loop { let Some(reply) = inbox.next() else { return }; if reply.reply_to.as_deref() == Some(id.as_str()) { break; } } times.push(t.elapsed().as_micros() as u64); } let total = started.elapsed().as_secs_f64(); times.sort(); let p = |q: f64| times[((times.len() as f64 - 1.0) * q) as usize]; let _ = reply( &message, json!({ "type": "bench_result", "n": n, "total_s": total, "msgs_per_s": (n as f64 / total).round(), "p50_us": p(0.5), "p90_us": p(0.9), "p99_us": p(0.99), "max_us": p(1.0) }), ); } }