examples: rust fmt

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2022-04-10 17:44:34 +02:00
parent 1e724828b4
commit a22d338831
8 changed files with 93 additions and 95 deletions

View File

@ -3,7 +3,6 @@ use anyhow::Error;
// chacha20-poly1305 // chacha20-poly1305
fn rate_test(name: &str, bench: &dyn Fn() -> usize) { fn rate_test(name: &str, bench: &dyn Fn() -> usize) {
print!("{:<20} ", name); print!("{:<20} ", name);
let start = std::time::SystemTime::now(); let start = std::time::SystemTime::now();
@ -14,19 +13,18 @@ fn rate_test(name: &str, bench: &dyn Fn() -> usize) {
loop { loop {
bytes += bench(); bytes += bench();
let elapsed = start.elapsed().unwrap(); let elapsed = start.elapsed().unwrap();
if elapsed > duration { break; } if elapsed > duration {
break;
}
} }
let elapsed = start.elapsed().unwrap(); let elapsed = start.elapsed().unwrap();
let elapsed = (elapsed.as_secs() as f64) + let elapsed = (elapsed.as_secs() as f64) + (elapsed.subsec_millis() as f64) / 1000.0;
(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> { 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", &|| { rate_test("crc32", &|| {
@ -54,13 +52,7 @@ fn main() -> Result<(), Error> {
rate_test("aes-256-gcm", &|| { rate_test("aes-256-gcm", &|| {
let mut tag = [0u8; 16]; let mut tag = [0u8; 16];
openssl::symm::encrypt_aead( openssl::symm::encrypt_aead(cipher, &key, Some(&iv), b"", &input, &mut tag).unwrap();
cipher,
&key,
Some(&iv),
b"",
&input,
&mut tag).unwrap();
input.len() input.len()
}); });
@ -68,13 +60,7 @@ fn main() -> Result<(), Error> {
rate_test("chacha20-poly1305", &|| { rate_test("chacha20-poly1305", &|| {
let mut tag = [0u8; 16]; let mut tag = [0u8; 16];
openssl::symm::encrypt_aead( openssl::symm::encrypt_aead(cipher, &key, Some(&iv[..12]), b"", &input, &mut tag).unwrap();
cipher,
&key,
Some(&iv[..12]),
b"",
&input,
&mut tag).unwrap();
input.len() input.len()
}); });

View File

@ -1,7 +1,7 @@
use anyhow::{Error}; use anyhow::Error;
use proxmox_schema::*;
use proxmox_router::cli::*; use proxmox_router::cli::*;
use proxmox_schema::*;
#[api( #[api(
input: { input: {
@ -16,9 +16,7 @@ use proxmox_router::cli::*;
/// Echo command. Print the passed text. /// Echo command. Print the passed text.
/// ///
/// Returns: nothing /// Returns: nothing
fn echo_command( fn echo_command(text: String) -> Result<(), Error> {
text: String,
) -> Result<(), Error> {
println!("{}", text); println!("{}", text);
Ok(()) Ok(())
} }
@ -37,9 +35,7 @@ fn echo_command(
/// Hello command. /// Hello command.
/// ///
/// Returns: nothing /// Returns: nothing
fn hello_command( fn hello_command(verbose: Option<bool>) -> Result<(), Error> {
verbose: Option<bool>,
) -> Result<(), Error> {
if verbose.unwrap_or(false) { if verbose.unwrap_or(false) {
println!("Hello, how are you!"); println!("Hello, how are you!");
} else { } else {
@ -54,7 +50,6 @@ fn hello_command(
/// ///
/// Returns: nothing /// Returns: nothing
fn quit_command() -> Result<(), Error> { fn quit_command() -> Result<(), Error> {
println!("Goodbye."); println!("Goodbye.");
std::process::exit(0); std::process::exit(0);
@ -64,8 +59,9 @@ fn cli_definition() -> CommandLineInterface {
let cmd_def = CliCommandMap::new() let cmd_def = CliCommandMap::new()
.insert("quit", CliCommand::new(&API_METHOD_QUIT_COMMAND)) .insert("quit", CliCommand::new(&API_METHOD_QUIT_COMMAND))
.insert("hello", CliCommand::new(&API_METHOD_HELLO_COMMAND)) .insert("hello", CliCommand::new(&API_METHOD_HELLO_COMMAND))
.insert("echo", CliCommand::new(&API_METHOD_ECHO_COMMAND) .insert(
.arg_param(&["text"]) "echo",
CliCommand::new(&API_METHOD_ECHO_COMMAND).arg_param(&["text"]),
) )
.insert_help(); .insert_help();
@ -73,7 +69,6 @@ fn cli_definition() -> CommandLineInterface {
} }
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
let helper = CliHelper::new(cli_definition()); let helper = CliHelper::new(cli_definition());
let mut rl = rustyline::Editor::<CliHelper>::new(); let mut rl = rustyline::Editor::<CliHelper>::new();

View File

@ -3,14 +3,13 @@ use std::io::Write;
use anyhow::Error; use anyhow::Error;
use pbs_api_types::Authid; use pbs_api_types::Authid;
use pbs_client::{HttpClient, HttpClientOptions, BackupReader}; use pbs_client::{BackupReader, HttpClient, HttpClientOptions};
pub struct DummyWriter { pub struct DummyWriter {
bytes: usize, bytes: usize,
} }
impl Write for DummyWriter { impl Write for DummyWriter {
fn write(&mut self, data: &[u8]) -> Result<usize, std::io::Error> { fn write(&mut self, data: &[u8]) -> Result<usize, std::io::Error> {
self.bytes += data.len(); self.bytes += data.len();
Ok(data.len()) Ok(data.len())
@ -21,9 +20,7 @@ impl Write for DummyWriter {
} }
} }
async fn run() -> Result<(), Error> { async fn run() -> Result<(), Error> {
let host = "localhost"; let host = "localhost";
let auth_id = Authid::root_auth_id(); 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 backup_time = proxmox_time::parse_rfc3339("2019-06-28T10:49:48Z")?;
let client = BackupReader::start(client, None, "store2", "host", "elsa", backup_time, true) let client =
.await?; BackupReader::start(client, None, "store2", "host", "elsa", backup_time, true).await?;
let start = std::time::SystemTime::now(); let start = std::time::SystemTime::now();
@ -50,10 +47,13 @@ async fn run() -> Result<(), Error> {
} }
let elapsed = start.elapsed().unwrap(); let elapsed = start.elapsed().unwrap();
let elapsed = (elapsed.as_secs() as f64) + let elapsed = (elapsed.as_secs() as f64) + (elapsed.subsec_millis() as f64) / 1000.0;
(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(()) Ok(())
} }

View File

@ -1,6 +1,6 @@
use std::thread;
use std::path::PathBuf;
use std::io::Write; use std::io::Write;
use std::path::PathBuf;
use std::thread;
use anyhow::{bail, Error}; use anyhow::{bail, Error};
@ -19,13 +19,13 @@ use anyhow::{bail, Error};
// Error: detected shrunk file "./dyntest1/testfile0.dat" (22020096 < 12679380992) // Error: detected shrunk file "./dyntest1/testfile0.dat" (22020096 < 12679380992)
fn create_large_file(path: PathBuf) { fn create_large_file(path: PathBuf) {
println!("TEST {:?}", path); println!("TEST {:?}", path);
let mut file = std::fs::OpenOptions::new() let mut file = std::fs::OpenOptions::new()
.write(true) .write(true)
.create_new(true) .create_new(true)
.open(&path).unwrap(); .open(&path)
.unwrap();
let buffer = vec![0u8; 64 * 1024]; let buffer = vec![0u8; 64 * 1024];
@ -40,7 +40,6 @@ fn create_large_file(path: PathBuf) {
} }
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
let base = PathBuf::from("dyntest1"); let base = PathBuf::from("dyntest1");
let _ = std::fs::create_dir(&base); let _ = std::fs::create_dir(&base);

View File

@ -2,7 +2,7 @@ extern crate proxmox_backup;
// also see https://www.johndcook.com/blog/standard_deviation/ // also see https://www.johndcook.com/blog/standard_deviation/
use anyhow::{Error}; use anyhow::Error;
use std::io::{Read, Write}; use std::io::{Read, Write};
use pbs_datastore::Chunker; use pbs_datastore::Chunker;
@ -21,7 +21,6 @@ struct ChunkWriter {
} }
impl ChunkWriter { impl ChunkWriter {
fn new(chunk_size: usize) -> Self { fn new(chunk_size: usize) -> Self {
ChunkWriter { ChunkWriter {
chunker: Chunker::new(chunk_size), chunker: Chunker::new(chunk_size),
@ -37,7 +36,6 @@ impl ChunkWriter {
} }
fn record_stat(&mut self, chunk_size: f64) { fn record_stat(&mut self, chunk_size: f64) {
self.chunk_count += 1; self.chunk_count += 1;
if self.chunk_count == 1 { if self.chunk_count == 1 {
@ -46,8 +44,7 @@ impl ChunkWriter {
self.s_old = 0.0; self.s_old = 0.0;
} else { } else {
self.m_new = self.m_old + (chunk_size - self.m_old) / (self.chunk_count as f64); self.m_new = self.m_old + (chunk_size - self.m_old) / (self.chunk_count as f64);
self.s_new = self.s_old + self.s_new = self.s_old + (chunk_size - self.m_old) * (chunk_size - self.m_new);
(chunk_size - self.m_old)*(chunk_size - self.m_new);
// set up for next iteration // set up for next iteration
self.m_old = self.m_new; self.m_old = self.m_new;
self.s_old = self.s_new; self.s_old = self.s_new;
@ -55,18 +52,21 @@ impl ChunkWriter {
let variance = if self.chunk_count > 1 { let variance = if self.chunk_count > 1 {
self.s_new / ((self.chunk_count - 1) as f64) self.s_new / ((self.chunk_count - 1) as f64)
} else { 0.0 }; } else {
0.0
};
let std_deviation = variance.sqrt(); let std_deviation = variance.sqrt();
let deviation_per = (std_deviation * 100.0) / self.m_new; 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); 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 { impl Write for ChunkWriter {
fn write(&mut self, data: &[u8]) -> std::result::Result<usize, std::io::Error> { fn write(&mut self, data: &[u8]) -> std::result::Result<usize, std::io::Error> {
let chunker = &mut self.chunker; let chunker = &mut self.chunker;
let pos = chunker.scan(data); let pos = chunker.scan(data);
@ -80,7 +80,6 @@ impl Write for ChunkWriter {
self.last_chunk = self.chunk_offset; self.last_chunk = self.chunk_offset;
Ok(pos) Ok(pos)
} else { } else {
self.chunk_offset += data.len(); self.chunk_offset += data.len();
Ok(data.len()) Ok(data.len())
@ -93,7 +92,6 @@ impl Write for ChunkWriter {
} }
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
let mut file = std::fs::File::open("/dev/urandom")?; let mut file = std::fs::File::open("/dev/urandom")?;
let mut bytes = 0; let mut bytes = 0;
@ -103,13 +101,14 @@ fn main() -> Result<(), Error> {
let mut writer = ChunkWriter::new(4096 * 1024); let mut writer = ChunkWriter::new(4096 * 1024);
loop { loop {
file.read_exact(&mut buffer)?; file.read_exact(&mut buffer)?;
bytes += buffer.len(); bytes += buffer.len();
writer.write_all(&buffer)?; writer.write_all(&buffer)?;
if bytes > 1024*1024*1024 { break; } if bytes > 1024 * 1024 * 1024 {
break;
}
} }
Ok(()) Ok(())

View File

@ -3,7 +3,6 @@ extern crate proxmox_backup;
use pbs_datastore::Chunker; use pbs_datastore::Chunker;
fn main() { fn main() {
let mut buffer = Vec::new(); let mut buffer = Vec::new();
for i in 0..20 * 1024 * 1024 { for i in 0..20 * 1024 * 1024 {
@ -39,11 +38,14 @@ fn main() {
} }
let elapsed = start.elapsed().unwrap(); let elapsed = start.elapsed().unwrap();
let elapsed = (elapsed.as_secs() as f64) + let elapsed = (elapsed.as_secs() as f64) + (elapsed.subsec_millis() as f64) / 1000.0;
(elapsed.subsec_millis() as f64)/1000.0;
let mbytecount = ((count * buffer.len()) as f64) / (1024.0 * 1024.0); let mbytecount = ((count * buffer.len()) as f64) / (1024.0 * 1024.0);
let avg_chunk_size = mbytecount / (chunk_count as f64); let avg_chunk_size = mbytecount / (chunk_count as f64);
let mbytes_per_sec = mbytecount / elapsed; let mbytes_per_sec = mbytecount / elapsed;
println!("SPEED = {} MB/s, avg chunk size = {} KB", mbytes_per_sec, avg_chunk_size*1024.0); println!(
"SPEED = {} MB/s, avg chunk size = {} KB",
mbytes_per_sec,
avg_chunk_size * 1024.0
);
} }

View File

@ -1,4 +1,4 @@
use anyhow::{Error}; use anyhow::Error;
use futures::*; use futures::*;
extern crate proxmox_backup; extern crate proxmox_backup;
@ -19,7 +19,6 @@ fn main() {
} }
async fn run() -> Result<(), Error> { async fn run() -> Result<(), Error> {
let file = tokio::fs::File::open("random-test.dat").await?; let file = tokio::fs::File::open("random-test.dat").await?;
let stream = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new()) let stream = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
@ -44,10 +43,19 @@ async fn run() -> Result<(), Error> {
println!("Got chunk {}", chunk.len()); println!("Got chunk {}", chunk.len());
} }
let speed = ((stream_len*1_000_000)/(1024*1024))/(start_time.elapsed().as_micros() as usize); let speed =
println!("Uploaded {} chunks in {} seconds ({} MB/s).", repeat, start_time.elapsed().as_secs(), 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!("Average chunk size was {} bytes.", stream_len / repeat);
println!("time per request: {} microseconds.", (start_time.elapsed().as_micros())/(repeat as u128)); println!(
"time per request: {} microseconds.",
(start_time.elapsed().as_micros()) / (repeat as u128)
);
Ok(()) Ok(())
} }

View File

@ -1,10 +1,9 @@
use anyhow::{Error}; use anyhow::Error;
use pbs_client::{HttpClient, HttpClientOptions, BackupWriter};
use pbs_api_types::Authid; use pbs_api_types::Authid;
use pbs_client::{BackupWriter, HttpClient, HttpClientOptions};
async fn upload_speed() -> Result<f64, Error> { async fn upload_speed() -> Result<f64, Error> {
let host = "localhost"; let host = "localhost";
let datastore = "store2"; let datastore = "store2";
@ -18,7 +17,17 @@ async fn upload_speed() -> Result<f64, Error> {
let backup_time = proxmox_time::epoch_i64(); 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"); println!("start upload speed test");
let res = client.upload_speedtest(true).await?; let res = client.upload_speedtest(true).await?;