diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 6f716b3f1..ae6f4c73f 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -6,8 +6,9 @@ publish = false edition = "2021" [dev-dependencies] +byte-unit = "5.1.4" yansi = "~0.5.1" -clap = "2.33" +clap = "4.5.3" fastrand = "1.4.0" futures = "~0.3.5" futures-lite = "1.11.1" @@ -16,7 +17,6 @@ glommio = { path = "../glommio" } # hyper and tokio for the hyper example. We just need the traits from Tokio hyper = { version = "1.2.0", features = ["full"] } num_cpus = "1.13.0" -pretty-bytes = "~0.2.2" sys-info = "~0.8.0" http-body-util = "0.1.0" serde_json = "1.0.114" diff --git a/examples/storage.rs b/examples/storage.rs index 285ec3a72..8beb8edd4 100644 --- a/examples/storage.rs +++ b/examples/storage.rs @@ -1,4 +1,5 @@ -use clap::{App, Arg}; +use byte_unit::{Byte, UnitType}; +use clap::{Arg, Command}; use futures_lite::{ stream::{self, StreamExt}, AsyncReadExt, AsyncWriteExt, @@ -11,7 +12,6 @@ use glommio::{ }, LocalExecutorBuilder, Placement, }; -use pretty_bytes::converter; use std::{ cell::Cell, fs, @@ -51,13 +51,17 @@ async fn stream_write>( let endw = Instant::now(); let time = start.elapsed(); - let bytes = converter::convert(file_size as _); - let rate = converter::convert((file_size as f64 / time.as_secs_f64()) as _); - println!("{name}: Wrote {bytes} in {time:#?}, {rate}/s"); + let bytes = Byte::from_u64(file_size).get_appropriate_unit(UnitType::Binary); + let rate = Byte::from_f64(file_size as f64 / time.as_secs_f64()) + .unwrap_or_default() + .get_appropriate_unit(UnitType::Binary); + println!("{name}: Wrote {bytes:.2} in {time:#?}, {rate:.2}/s"); stream.close().await.unwrap(); - let rate = converter::convert((file_size as f64 / start.elapsed().as_secs_f64()) as _); + let rate = Byte::from_f64(file_size as f64 / start.elapsed().as_secs_f64()) + .unwrap_or_default() + .get_appropriate_unit(UnitType::Binary); let time = endw.elapsed(); - println!("{name}: Closed in {time:#?}, Amortized total {rate}/s"); + println!("{name}: Closed in {time:#?}, Amortized total {rate:.2}/s"); } async fn stream_scan>( @@ -82,10 +86,12 @@ async fn stream_scan>( let time = start.elapsed(); let name = name.into(); - let bytes = converter::convert(bytes_read as _); - let rate = converter::convert((bytes_read as f64 / time.as_secs_f64()) as _); + let bytes = Byte::from_u64(bytes_read as _).get_appropriate_unit(UnitType::Binary); + let rate = Byte::from_f64(bytes_read as f64 / time.as_secs_f64()) + .unwrap_or_default() + .get_appropriate_unit(UnitType::Binary); println!( - "{name}: Scanned {bytes} in {time:#?}, {rate}/s, {} IOPS", + "{name}: Scanned {bytes:.2} in {time:#?}, {rate:.2}/s, {} IOPS", (ops as f64 / time.as_secs_f64()) as usize ); stream @@ -115,10 +121,12 @@ async fn stream_scan_alt_api>( let time = start.elapsed(); let name = name.into(); - let bytes = converter::convert(bytes_read as _); - let rate = converter::convert((bytes_read as f64 / time.as_secs_f64()) as _); + let bytes = Byte::from_u64(bytes_read as _).get_appropriate_unit(UnitType::Binary); + let rate = Byte::from_f64(bytes_read as f64 / time.as_secs_f64()) + .unwrap_or_default() + .get_appropriate_unit(UnitType::Binary); println!( - "{name}: Scanned {bytes} in {time:#?}, {rate}/s, {} IOPS", + "{name}: Scanned {bytes:.2} in {time:#?}, {rate:.2}/s, {} IOPS", (ops as f64 / time.as_secs_f64()) as usize ); stream.close().await.unwrap(); @@ -212,11 +220,11 @@ async fn random_read>( Ok(file) => file.close().await, }; - assert_eq!(finished, parallelism as _); - let bytes = converter::convert(random as _); + assert_eq!(finished, parallelism); + let bytes = Byte::from_u64(random).get_appropriate_unit(UnitType::Binary); let dur = time.elapsed(); println!( - "{name}: Random Read (uniform) size span of {bytes}, for {dur:#?}, {} IOPS", + "{name}: Random Read (uniform) size span of {bytes:.2}, for {dur:#?}, {} IOPS", (iops.get() as f64 / dur.as_secs_f64()) as usize ); } @@ -261,39 +269,39 @@ async fn random_many_read>( Ok(file) => file.close().await, }; - assert_eq!(finished, parallelism as _); - let bytes = converter::convert(random as _); - let max_merged = converter::convert(max_buffer_size as _); + assert_eq!(finished, parallelism); + let bytes = Byte::from_u64(random).get_appropriate_unit(UnitType::Binary); + let max_merged = Byte::from_u64(max_buffer_size as _).get_appropriate_unit(UnitType::Binary); let dur = time.elapsed(); println!( - "{name}: Random Bulk Read (uniform) size span of {bytes}, for {dur:#?} (max merged size \ - of {max_merged}), {} IOPS", + "{name}: Random Bulk Read (uniform) size span of {bytes:.2}, for {dur:#?} (max merged size \ + of {max_merged:.2}), {} IOPS", (iops.get() as f64 / dur.as_secs_f64()) as usize ); } fn main() { - let matches = App::new("storage example") + let matches = Command::new("storage example") .version("0.1.0") .author("Glauber Costa ") .about("demonstrate glommio's storage APIs") .arg( - Arg::with_name("storage_dir") + Arg::new("storage_dir") .long("dir") - .takes_value(true) + .action(clap::ArgAction::Set) .required(true) .help("The directory where to write and read file for this test"), ) .arg( - Arg::with_name("file_size") + Arg::new("file_size") .long("size-gb") - .takes_value(true) + .action(clap::ArgAction::Set) .required(false) .help("size of the file in GB (default: 2 * memory_size)"), ) .get_matches(); - let path = matches.value_of("storage_dir").unwrap(); + let path = matches.get_one::("storage_dir").unwrap(); let mut dir = PathBuf::from(path); assert!(dir.exists()); dir.push("benchfiles"); @@ -303,8 +311,8 @@ fn main() { let total_memory = sys_info::mem_info().unwrap().total << 10; let file_size = matches - .value_of("file_size") - .map(|s| s.parse::().unwrap() << 30) + .get_one::("file_size") + .map(|s| s << 30) .unwrap_or(total_memory * 2); let random = total_memory / 10;