1use byte_unit::{AdjustedByte, Byte, UnitType};
6
7pub mod clock;
8pub mod simtime;
9
10#[must_use]
12pub fn compute_adjusted_value_and_rate(
13 time_now_ns: f64,
14 num_bytes: usize,
15) -> (AdjustedByte, AdjustedByte) {
16 let time_now_s = time_now_ns / (1000.0 * 1000.0 * 1000.0);
17 let count = Byte::from_u64(num_bytes as u64).get_appropriate_unit(UnitType::Binary);
18 let per_second = if time_now_s == 0.0 {
19 Byte::from_f64(0.0).unwrap()
20 } else {
21 Byte::from_f64(num_bytes as f64 / time_now_s).unwrap()
22 };
23 let count_per_second = per_second.get_appropriate_unit(UnitType::Binary);
24 (count, count_per_second)
25}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30
31 #[test]
32 fn adjusted_value_and_rate_handles_zero_and_elapsed_time() {
33 let (count, rate) = compute_adjusted_value_and_rate(0.0, 1024);
34 assert_eq!(count.get_value(), 1.0);
35 assert_eq!(rate.get_value(), 0.0);
36
37 let (count, rate) = compute_adjusted_value_and_rate(1_000_000_000.0, 2048);
38 assert_eq!(count.get_value(), 2.0);
39 assert_eq!(rate.get_value(), 2.0);
40 }
41}