drop format_size code in favor of number_prefix crate

This commit is contained in:
Ted Mielczarek 2016-05-12 15:46:22 -04:00
Родитель fdf70251cc
Коммит 3f9ee125d1
4 изменённых файлов: 87 добавлений и 44 удалений

80
Cargo.lock сгенерированный
Просмотреть файл

@ -7,6 +7,7 @@ dependencies = [
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"number_prefix 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"protobuf 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
"retry 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
@ -142,6 +143,80 @@ dependencies = [
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-bigint 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-complex 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-rational 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-bigint"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-complex"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-integer"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-iter"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-rational"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-bigint 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-traits"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "number_prefix"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "protobuf"
version = "1.0.20"
@ -180,6 +255,11 @@ dependencies = [
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rustc-serialize"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "slab"
version = "0.1.3"

Просмотреть файл

@ -9,6 +9,7 @@ env_logger = "0.3.3"
libc = "0.2.10"
log = "0.3.6"
mio = "0.5"
number_prefix = "0.2.5"
protobuf = "1.0.18"
retry = "0.4.0"
tempdir = "0.3.4"

Просмотреть файл

@ -22,6 +22,7 @@ use mock_command::{
CommandCreatorSync,
ProcessCommandCreator,
};
use number_prefix::{binary_prefix, Standalone, Prefixed};
use protobuf::RepeatedField;
use protocol::{
CacheStats,
@ -129,29 +130,6 @@ pub fn request_shutdown(mut conn : ServerConnection) -> io::Result<CacheStats> {
}
}
/// Format `size_in_bytes` as a size in sensible units.
///
/// e.g. format_size(3 * 1024 * 1024 * 1024) == "3 GB"
fn format_size(size_in_bytes : u64) -> String {
let mut size = size_in_bytes;
let mut remainder = 0;
for suffix in ["bytes", "kB", "MB", "GB", "TB"].iter() {
if size < 1024 {
let frac = if remainder > 0 {
let rem = (100.0 * remainder as f32 / 1024.0).trunc() as i32;
format!(".{}", if rem % 10 == 0 { rem / 10 } else { rem })
} else {
"".to_owned()
};
return format!("{}{} {}", size, frac, suffix);
}
remainder = size % 1024;
size = size / 1024;
}
//TODO: handle this more gracefully
return format!("{} {}", size, "PB");
}
/// Print `stats` to stdout.
fn print_stats(stats : CacheStats) -> io::Result<()> {
for stat in stats.get_stats().iter() {
@ -162,7 +140,10 @@ fn print_stats(stats : CacheStats) -> io::Result<()> {
} else if stat.has_str() {
print!("{}", stat.get_str());
} else if stat.has_size() {
print!("{}", format_size(stat.get_size()));
match binary_prefix(stat.get_size() as f64) {
Standalone(bytes) => print!("{} bytes", bytes),
Prefixed(prefix, n) => print!("{:.0} {}B", n, prefix),
}
}
print!("\n");
}
@ -321,23 +302,3 @@ pub fn run_command(cmd : Command) -> i32 {
},
}
}
#[cfg(test)]
mod test {
use super::format_size;
#[test]
fn test_format_size() {
assert_eq!("10 bytes", format_size(10));
assert_eq!("1023 bytes", format_size(1023));
assert_eq!("1 kB", format_size(1024));
assert_eq!("1.5 kB", format_size(1024 + 512));
assert_eq!("1023.99 kB", format_size(1024 * 1024 - 1));
assert_eq!("1 MB", format_size(1024 * 1024));
assert_eq!("1 GB", format_size(1024 * 1024 * 1024));
assert_eq!("1.25 GB", format_size(1024 * 1024 * (1024 + 256)));
assert_eq!("3 GB", format_size(1024 * 1024 * 1024 * 3));
assert_eq!("1 TB", format_size(1024 * 1024 * 1024 * 1024));
assert_eq!("1 PB", format_size(1024 * 1024 * 1024 * 1024 * 1024));
}
}

Просмотреть файл

@ -17,6 +17,7 @@ extern crate env_logger;
#[macro_use] extern crate log;
extern crate libc;
extern crate mio;
extern crate number_prefix;
extern crate protobuf;
extern crate retry;
extern crate tempdir;