-
Notifications
You must be signed in to change notification settings - Fork 892
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use std::fmt::{self, Display}; | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
pub enum Unit { | ||
Bps, | ||
IOPS, | ||
} | ||
|
||
/// Human readable size (some units) | ||
pub enum Size { | ||
Bps(usize), | ||
IOPS(usize), | ||
} | ||
|
||
impl Size { | ||
pub fn new(size: usize, unit: Unit) -> Self { | ||
match unit { | ||
Unit::Bps => Self::Bps(size), | ||
Unit::IOPS => Self::IOPS(size), | ||
} | ||
} | ||
} | ||
|
||
impl Display for Size { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Size::Bps(size) => { | ||
const KI: f64 = 1024.0; | ||
const MI: f64 = KI * KI; | ||
const GI: f64 = KI * KI * KI; | ||
let size = *size as f64; | ||
|
||
if size >= GI { | ||
write!(f, "{:5.1} GiB/s", size / GI) | ||
} else if size >= MI { | ||
write!(f, "{:5.1} MiB/s", size / MI) | ||
} else if size >= KI { | ||
write!(f, "{:5.1} KiB/s", size / KI) | ||
} else { | ||
write!(f, "{:3.0} B/s", size) | ||
} | ||
} | ||
Size::IOPS(size) => { | ||
const K: f64 = 1000.0; | ||
const M: f64 = K * K; | ||
const G: f64 = K * K * K; | ||
let size = *size as f64; | ||
|
||
if size >= G { | ||
write!(f, "{:5.1} giga-IOPS", size / G) | ||
} else if size >= M { | ||
write!(f, "{:5.1} mega-IOPS", size / M) | ||
} else if size >= K { | ||
write!(f, "{:5.1} kilo-IOPS", size / K) | ||
} else { | ||
write!(f, "{:3.0} IOPS", size) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn unit_formatter_test() { | ||
use crate::utils::units::{Size, Unit}; | ||
|
||
// Test Byte units | ||
assert_eq!(format!("{}", Size::new(1 as usize, Unit::Bps)), " 1 B/s"); | ||
assert_eq!( | ||
format!("{}", Size::new(1024 as usize, Unit::Bps)), | ||
" 1.0 KiB/s" | ||
); | ||
assert_eq!( | ||
format!("{}", Size::new(1024usize.pow(2), Unit::Bps)), | ||
" 1.0 MiB/s" | ||
); | ||
assert_eq!( | ||
format!("{}", Size::new(1024usize.pow(3), Unit::Bps)), | ||
" 1.0 GiB/s" | ||
); | ||
|
||
//Test IOPS | ||
assert_eq!(format!("{}", Size::new(1 as usize, Unit::IOPS)), " 1 IOPS"); | ||
assert_eq!( | ||
format!("{}", Size::new(1000 as usize, Unit::IOPS)), | ||
" 1.0 kilo-IOPS" | ||
); | ||
assert_eq!( | ||
format!("{}", Size::new(1000usize.pow(2), Unit::IOPS)), | ||
" 1.0 mega-IOPS" | ||
); | ||
assert_eq!( | ||
format!("{}", Size::new(1000usize.pow(3), Unit::IOPS)), | ||
" 1.0 giga-IOPS" | ||
); | ||
} | ||
} |