use std::time::Duration; #[derive(Debug, Clone)] pub struct StressResult { ///How long did it take pub time: Duration, ///Did we get an answer pub ok: bool, pub rtype: ResType, } #[derive(Debug, Clone)] pub enum ResType { Tcp(u32), Http, } #[derive(Debug, Clone)] pub struct ListOfResults { pub results: Vec, pub restype: ResType, } impl ListOfResults { fn compute_ratio(&self) { let mut ok: u64 = 0; let mut nok: u64 = 0; for r in self.results.clone() { if r.ok == true { *&mut ok += 1; } else { *&mut nok += 1; } } let mut compute = ok + nok; println!("TOTAL {}\n\tOK {}\n\tNOK {}", compute, ok, nok); if compute < 1 { compute = 1; } compute = ok * 100 / compute; println!("Got {}% of OK\n", compute ); } ///Get the different TCP ports from the results fn get_ports(&self) -> Vec { let mut ports = vec![]; for r in &self.results { match r.rtype { ResType::Tcp(p) => { if !ports.contains(&p) { ports.push(p); } }, ResType::Http => (), } } ports } ///Show info, by port if we are in TCP mode pub fn show_info(&self) { println!("\n===== RESULTS ====="); match &self.restype { ResType::Tcp(_) => { let ports = self.get_ports(); for port in ports { let mut results = Vec::new(); for res in &self.results { match res.rtype { ResType::Tcp(v) => { if v == port { *&mut results.push(res.clone()); } }, _ => (), } } let list = ListOfResults { results: results, restype: ResType::Tcp(1) }; println!("=====> PORT {}", port); list.show_mean_time(); list.compute_ratio(); } }, ResType::Http => { self.show_mean_time(); self.compute_ratio(); }, } } fn show_mean_time(&self) { let mut total: u128 = 0; let mut acc: u128 = 0; for i in &self.results { if i.ok { *&mut total += 1; *&mut acc += i.time.as_millis(); } } if total == 0 { total += 1 }; let restime = acc / total; println!("Average response time : {:?}", Duration::from_millis(restime.try_into().unwrap())); } }