1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) The Libra Core Contributors
// SPDX-License-Identifier: Apache-2.0

pub mod atomic_histogram;
pub mod aws;
pub mod cluster;
pub mod cluster_builder;
pub mod cluster_swarm;
pub mod effects;
pub mod experiments;
pub mod genesis_helper;
pub mod github;
pub mod health;
pub mod instance;
pub mod prometheus;
pub mod report;
pub mod slack;
pub mod stats;
pub mod suite;
pub mod tx_emitter;

pub mod util {

    pub fn human_readable_bytes_per_sec(bytes_per_sec: f64) -> String {
        if bytes_per_sec.round() < 1024.0 {
            return format!("{:.0} Bps", bytes_per_sec);
        }

        let kbytes_per_sec = bytes_per_sec / 1024.0;
        if kbytes_per_sec.round() < 1024.0 {
            return format!("{:.0} KBps", kbytes_per_sec);
        }

        let mbytes_per_sec = kbytes_per_sec / 1024.0;
        format!("{:.2} MBps", mbytes_per_sec)
    }

    #[cfg(test)]
    mod tests {
        use crate::util::human_readable_bytes_per_sec;

        #[test]
        fn test_human_readable_bytes_per_sec() {
            assert_eq!(&human_readable_bytes_per_sec(0.3), "0 Bps");
            assert_eq!(&human_readable_bytes_per_sec(0.7), "1 Bps");
            assert_eq!(&human_readable_bytes_per_sec(1.0), "1 Bps");
            assert_eq!(&human_readable_bytes_per_sec(1023.4), "1023 Bps");
            assert_eq!(&human_readable_bytes_per_sec(1023.5), "1 KBps");
            assert_eq!(&human_readable_bytes_per_sec(1024.0 * 3.5), "4 KBps");
            assert_eq!(&human_readable_bytes_per_sec(1024.0 * 1023.4), "1023 KBps");
            assert_eq!(&human_readable_bytes_per_sec(1024.0 * 1023.5), "1.00 MBps");
            assert_eq!(
                &human_readable_bytes_per_sec(1024.0 * 1024.0 * 2.28),
                "2.28 MBps"
            );
        }
    }
}