benchmark_network/
unit.rs

1fn to_metric(size: f64) -> String {
2    const KILO: f64 = 1000 as f64;
3    const MEGA: f64 = KILO * KILO;
4    const GIGA: f64 = KILO * MEGA;
5    const TERA: f64 = KILO * GIGA;
6    const PETA: f64 = KILO * TERA;
7
8    let unit: String;
9    let formatted_size: f64;
10    if size >= PETA {
11        unit = String::from("P");
12        formatted_size = size / PETA;
13    } else if size >= TERA {
14        unit = String::from("T");
15        formatted_size = size / TERA;
16    } else if size >= GIGA {
17        unit = String::from("G");
18        formatted_size = size / GIGA;
19    } else if size >= MEGA {
20        unit = String::from("M");
21        formatted_size = size / MEGA;
22    } else if size >= KILO {
23        unit = String::from("K");
24        formatted_size = size / KILO;
25    } else {
26        unit = String::from("");
27        formatted_size = size;
28    }
29    format!("{formatted_size:.2}{unit}")
30}
31
32pub fn data_to_string(data_amount: f64) -> String {
33    format!(" {}B", to_metric(data_amount))
34}
35
36pub fn throughput_to_string(throughput: f64) -> String {
37    format!(" {}b/s", to_metric(throughput))
38}