From a22d338831601fc4c2a8eeb2cf51dbd37660961b Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Sun, 10 Apr 2022 17:44:34 +0200 Subject: [PATCH] examples: rust fmt Signed-off-by: Thomas Lamprecht --- examples/cipherbench.rs | 38 +++++++++++------------------------ examples/completion.rs | 19 +++++++----------- examples/download-speed.rs | 18 ++++++++--------- examples/dynamic-files.rs | 11 +++++----- examples/test_chunk_size.rs | 35 ++++++++++++++++---------------- examples/test_chunk_speed.rs | 26 +++++++++++++----------- examples/test_chunk_speed2.rs | 22 +++++++++++++------- examples/upload-speed.rs | 19 +++++++++++++----- 8 files changed, 93 insertions(+), 95 deletions(-) diff --git a/examples/cipherbench.rs b/examples/cipherbench.rs index 2abb0aaf..2e4db501 100644 --- a/examples/cipherbench.rs +++ b/examples/cipherbench.rs @@ -3,7 +3,6 @@ use anyhow::Error; // chacha20-poly1305 fn rate_test(name: &str, bench: &dyn Fn() -> usize) { - print!("{:<20} ", name); let start = std::time::SystemTime::now(); @@ -14,20 +13,19 @@ fn rate_test(name: &str, bench: &dyn Fn() -> usize) { loop { bytes += bench(); let elapsed = start.elapsed().unwrap(); - if elapsed > duration { break; } + if elapsed > duration { + break; + } } let elapsed = start.elapsed().unwrap(); - let elapsed = (elapsed.as_secs() as f64) + - (elapsed.subsec_millis() as f64)/1000.0; + let elapsed = (elapsed.as_secs() as f64) + (elapsed.subsec_millis() as f64) / 1000.0; - println!("{:>8.1} MB/s", (bytes as f64)/(elapsed*1024.0*1024.0)); + println!("{:>8.1} MB/s", (bytes as f64) / (elapsed * 1024.0 * 1024.0)); } - fn main() -> Result<(), Error> { - - let input = proxmox_sys::linux::random_data(1024*1024)?; + let input = proxmox_sys::linux::random_data(1024 * 1024)?; rate_test("crc32", &|| { let mut crchasher = crc32fast::Hasher::new(); @@ -50,31 +48,19 @@ fn main() -> Result<(), Error> { let iv = proxmox_sys::linux::random_data(16)?; - let cipher = openssl::symm::Cipher::aes_256_gcm(); + let cipher = openssl::symm::Cipher::aes_256_gcm(); rate_test("aes-256-gcm", &|| { - let mut tag = [0u8;16]; - openssl::symm::encrypt_aead( - cipher, - &key, - Some(&iv), - b"", - &input, - &mut tag).unwrap(); + let mut tag = [0u8; 16]; + openssl::symm::encrypt_aead(cipher, &key, Some(&iv), b"", &input, &mut tag).unwrap(); input.len() }); - let cipher = openssl::symm::Cipher::chacha20_poly1305(); + let cipher = openssl::symm::Cipher::chacha20_poly1305(); rate_test("chacha20-poly1305", &|| { - let mut tag = [0u8;16]; - openssl::symm::encrypt_aead( - cipher, - &key, - Some(&iv[..12]), - b"", - &input, - &mut tag).unwrap(); + let mut tag = [0u8; 16]; + openssl::symm::encrypt_aead(cipher, &key, Some(&iv[..12]), b"", &input, &mut tag).unwrap(); input.len() }); diff --git a/examples/completion.rs b/examples/completion.rs index e2556c83..3c44cd8f 100644 --- a/examples/completion.rs +++ b/examples/completion.rs @@ -1,7 +1,7 @@ -use anyhow::{Error}; +use anyhow::Error; -use proxmox_schema::*; use proxmox_router::cli::*; +use proxmox_schema::*; #[api( input: { @@ -16,9 +16,7 @@ use proxmox_router::cli::*; /// Echo command. Print the passed text. /// /// Returns: nothing -fn echo_command( - text: String, -) -> Result<(), Error> { +fn echo_command(text: String) -> Result<(), Error> { println!("{}", text); Ok(()) } @@ -37,9 +35,7 @@ fn echo_command( /// Hello command. /// /// Returns: nothing -fn hello_command( - verbose: Option, -) -> Result<(), Error> { +fn hello_command(verbose: Option) -> Result<(), Error> { if verbose.unwrap_or(false) { println!("Hello, how are you!"); } else { @@ -54,7 +50,6 @@ fn hello_command( /// /// Returns: nothing fn quit_command() -> Result<(), Error> { - println!("Goodbye."); std::process::exit(0); @@ -64,8 +59,9 @@ fn cli_definition() -> CommandLineInterface { let cmd_def = CliCommandMap::new() .insert("quit", CliCommand::new(&API_METHOD_QUIT_COMMAND)) .insert("hello", CliCommand::new(&API_METHOD_HELLO_COMMAND)) - .insert("echo", CliCommand::new(&API_METHOD_ECHO_COMMAND) - .arg_param(&["text"]) + .insert( + "echo", + CliCommand::new(&API_METHOD_ECHO_COMMAND).arg_param(&["text"]), ) .insert_help(); @@ -73,7 +69,6 @@ fn cli_definition() -> CommandLineInterface { } fn main() -> Result<(), Error> { - let helper = CliHelper::new(cli_definition()); let mut rl = rustyline::Editor::::new(); diff --git a/examples/download-speed.rs b/examples/download-speed.rs index 75176db1..a685df9d 100644 --- a/examples/download-speed.rs +++ b/examples/download-speed.rs @@ -3,14 +3,13 @@ use std::io::Write; use anyhow::Error; use pbs_api_types::Authid; -use pbs_client::{HttpClient, HttpClientOptions, BackupReader}; +use pbs_client::{BackupReader, HttpClient, HttpClientOptions}; pub struct DummyWriter { bytes: usize, } impl Write for DummyWriter { - fn write(&mut self, data: &[u8]) -> Result { self.bytes += data.len(); Ok(data.len()) @@ -21,9 +20,7 @@ impl Write for DummyWriter { } } - async fn run() -> Result<(), Error> { - let host = "localhost"; let auth_id = Authid::root_auth_id(); @@ -36,8 +33,8 @@ async fn run() -> Result<(), Error> { let backup_time = proxmox_time::parse_rfc3339("2019-06-28T10:49:48Z")?; - let client = BackupReader::start(client, None, "store2", "host", "elsa", backup_time, true) - .await?; + let client = + BackupReader::start(client, None, "store2", "host", "elsa", backup_time, true).await?; let start = std::time::SystemTime::now(); @@ -50,10 +47,13 @@ async fn run() -> Result<(), Error> { } let elapsed = start.elapsed().unwrap(); - let elapsed = (elapsed.as_secs() as f64) + - (elapsed.subsec_millis() as f64)/1000.0; + let elapsed = (elapsed.as_secs() as f64) + (elapsed.subsec_millis() as f64) / 1000.0; - println!("Downloaded {} bytes, {} MB/s", bytes, (bytes as f64)/(elapsed*1024.0*1024.0)); + println!( + "Downloaded {} bytes, {} MB/s", + bytes, + (bytes as f64) / (elapsed * 1024.0 * 1024.0) + ); Ok(()) } diff --git a/examples/dynamic-files.rs b/examples/dynamic-files.rs index 777b95bf..c881eb31 100644 --- a/examples/dynamic-files.rs +++ b/examples/dynamic-files.rs @@ -1,6 +1,6 @@ -use std::thread; -use std::path::PathBuf; use std::io::Write; +use std::path::PathBuf; +use std::thread; use anyhow::{bail, Error}; @@ -19,15 +19,15 @@ use anyhow::{bail, Error}; // Error: detected shrunk file "./dyntest1/testfile0.dat" (22020096 < 12679380992) fn create_large_file(path: PathBuf) { - println!("TEST {:?}", path); let mut file = std::fs::OpenOptions::new() .write(true) .create_new(true) - .open(&path).unwrap(); + .open(&path) + .unwrap(); - let buffer = vec![0u8; 64*1024]; + let buffer = vec![0u8; 64 * 1024]; loop { for _ in 0..64 { @@ -40,7 +40,6 @@ fn create_large_file(path: PathBuf) { } fn main() -> Result<(), Error> { - let base = PathBuf::from("dyntest1"); let _ = std::fs::create_dir(&base); diff --git a/examples/test_chunk_size.rs b/examples/test_chunk_size.rs index 5a3a5398..a01a5e64 100644 --- a/examples/test_chunk_size.rs +++ b/examples/test_chunk_size.rs @@ -2,7 +2,7 @@ extern crate proxmox_backup; // also see https://www.johndcook.com/blog/standard_deviation/ -use anyhow::{Error}; +use anyhow::Error; use std::io::{Read, Write}; use pbs_datastore::Chunker; @@ -21,7 +21,6 @@ struct ChunkWriter { } impl ChunkWriter { - fn new(chunk_size: usize) -> Self { ChunkWriter { chunker: Chunker::new(chunk_size), @@ -37,7 +36,6 @@ impl ChunkWriter { } fn record_stat(&mut self, chunk_size: f64) { - self.chunk_count += 1; if self.chunk_count == 1 { @@ -45,28 +43,30 @@ impl ChunkWriter { self.m_new = chunk_size; self.s_old = 0.0; } else { - self.m_new = self.m_old + (chunk_size - self.m_old)/(self.chunk_count as f64); - self.s_new = self.s_old + - (chunk_size - self.m_old)*(chunk_size - self.m_new); + self.m_new = self.m_old + (chunk_size - self.m_old) / (self.chunk_count as f64); + self.s_new = self.s_old + (chunk_size - self.m_old) * (chunk_size - self.m_new); // set up for next iteration self.m_old = self.m_new; self.s_old = self.s_new; } let variance = if self.chunk_count > 1 { - self.s_new/((self.chunk_count -1)as f64) - } else { 0.0 }; + self.s_new / ((self.chunk_count - 1) as f64) + } else { + 0.0 + }; let std_deviation = variance.sqrt(); - let deviation_per = (std_deviation*100.0)/self.m_new; - println!("COUNT {:10} SIZE {:10} MEAN {:10} DEVIATION {:3}%", self.chunk_count, chunk_size, self.m_new as usize, deviation_per as usize); + let deviation_per = (std_deviation * 100.0) / self.m_new; + println!( + "COUNT {:10} SIZE {:10} MEAN {:10} DEVIATION {:3}%", + self.chunk_count, chunk_size, self.m_new as usize, deviation_per as usize + ); } } impl Write for ChunkWriter { - fn write(&mut self, data: &[u8]) -> std::result::Result { - let chunker = &mut self.chunker; let pos = chunker.scan(data); @@ -80,7 +80,6 @@ impl Write for ChunkWriter { self.last_chunk = self.chunk_offset; Ok(pos) - } else { self.chunk_offset += data.len(); Ok(data.len()) @@ -93,23 +92,23 @@ impl Write for ChunkWriter { } fn main() -> Result<(), Error> { - let mut file = std::fs::File::open("/dev/urandom")?; let mut bytes = 0; - let mut buffer = [0u8; 64*1024]; + let mut buffer = [0u8; 64 * 1024]; - let mut writer = ChunkWriter::new(4096*1024); + let mut writer = ChunkWriter::new(4096 * 1024); loop { - file.read_exact(&mut buffer)?; bytes += buffer.len(); writer.write_all(&buffer)?; - if bytes > 1024*1024*1024 { break; } + if bytes > 1024 * 1024 * 1024 { + break; + } } Ok(()) diff --git a/examples/test_chunk_speed.rs b/examples/test_chunk_speed.rs index f9fb0b0d..37e13e0d 100644 --- a/examples/test_chunk_speed.rs +++ b/examples/test_chunk_speed.rs @@ -3,24 +3,23 @@ extern crate proxmox_backup; use pbs_datastore::Chunker; fn main() { - let mut buffer = Vec::new(); - for i in 0..20*1024*1024 { + for i in 0..20 * 1024 * 1024 { for j in 0..4 { - let byte = ((i >> (j<<3))&0xff) as u8; + let byte = ((i >> (j << 3)) & 0xff) as u8; //println!("BYTE {}", byte); buffer.push(byte); } } - let mut chunker = Chunker::new(64*1024); + let mut chunker = Chunker::new(64 * 1024); let count = 5; let start = std::time::SystemTime::now(); let mut chunk_count = 0; - + for _i in 0..count { let mut pos = 0; let mut _last = 0; @@ -39,11 +38,14 @@ fn main() { } let elapsed = start.elapsed().unwrap(); - let elapsed = (elapsed.as_secs() as f64) + - (elapsed.subsec_millis() as f64)/1000.0; - - let mbytecount = ((count*buffer.len()) as f64) / (1024.0*1024.0); - let avg_chunk_size = mbytecount/(chunk_count as f64); - let mbytes_per_sec = mbytecount/elapsed; - println!("SPEED = {} MB/s, avg chunk size = {} KB", mbytes_per_sec, avg_chunk_size*1024.0); + let elapsed = (elapsed.as_secs() as f64) + (elapsed.subsec_millis() as f64) / 1000.0; + + let mbytecount = ((count * buffer.len()) as f64) / (1024.0 * 1024.0); + let avg_chunk_size = mbytecount / (chunk_count as f64); + let mbytes_per_sec = mbytecount / elapsed; + println!( + "SPEED = {} MB/s, avg chunk size = {} KB", + mbytes_per_sec, + avg_chunk_size * 1024.0 + ); } diff --git a/examples/test_chunk_speed2.rs b/examples/test_chunk_speed2.rs index 9274183a..3f69b436 100644 --- a/examples/test_chunk_speed2.rs +++ b/examples/test_chunk_speed2.rs @@ -1,4 +1,4 @@ -use anyhow::{Error}; +use anyhow::Error; use futures::*; extern crate proxmox_backup; @@ -19,7 +19,6 @@ fn main() { } async fn run() -> Result<(), Error> { - let file = tokio::fs::File::open("random-test.dat").await?; let stream = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new()) @@ -34,7 +33,7 @@ async fn run() -> Result<(), Error> { let mut repeat = 0; let mut stream_len = 0; while let Some(chunk) = chunk_stream.try_next().await? { - if chunk.len() > 16*1024*1024 { + if chunk.len() > 16 * 1024 * 1024 { panic!("Chunk too large {}", chunk.len()); } @@ -44,10 +43,19 @@ async fn run() -> Result<(), Error> { println!("Got chunk {}", chunk.len()); } - let speed = ((stream_len*1_000_000)/(1024*1024))/(start_time.elapsed().as_micros() as usize); - println!("Uploaded {} chunks in {} seconds ({} MB/s).", repeat, start_time.elapsed().as_secs(), speed); - println!("Average chunk size was {} bytes.", stream_len/repeat); - println!("time per request: {} microseconds.", (start_time.elapsed().as_micros())/(repeat as u128)); + let speed = + ((stream_len * 1_000_000) / (1024 * 1024)) / (start_time.elapsed().as_micros() as usize); + println!( + "Uploaded {} chunks in {} seconds ({} MB/s).", + repeat, + start_time.elapsed().as_secs(), + speed + ); + println!("Average chunk size was {} bytes.", stream_len / repeat); + println!( + "time per request: {} microseconds.", + (start_time.elapsed().as_micros()) / (repeat as u128) + ); Ok(()) } diff --git a/examples/upload-speed.rs b/examples/upload-speed.rs index 04e35124..7d1fb16b 100644 --- a/examples/upload-speed.rs +++ b/examples/upload-speed.rs @@ -1,10 +1,9 @@ -use anyhow::{Error}; +use anyhow::Error; -use pbs_client::{HttpClient, HttpClientOptions, BackupWriter}; use pbs_api_types::Authid; +use pbs_client::{BackupWriter, HttpClient, HttpClientOptions}; async fn upload_speed() -> Result { - let host = "localhost"; let datastore = "store2"; @@ -18,7 +17,17 @@ async fn upload_speed() -> Result { let backup_time = proxmox_time::epoch_i64(); - let client = BackupWriter::start(client, None, datastore, "host", "speedtest", backup_time, false, true).await?; + let client = BackupWriter::start( + client, + None, + datastore, + "host", + "speedtest", + backup_time, + false, + true, + ) + .await?; println!("start upload speed test"); let res = client.upload_speedtest(true).await?; @@ -26,7 +35,7 @@ async fn upload_speed() -> Result { Ok(res) } -fn main() { +fn main() { match proxmox_async::runtime::main(upload_speed()) { Ok(mbs) => { println!("average upload speed: {} MB/s", mbs);