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
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();
@ -53,28 +51,16 @@ fn main() -> Result<(), Error> {
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();
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()
});

View File

@ -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<bool>,
) -> Result<(), Error> {
fn hello_command(verbose: Option<bool>) -> 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::<CliHelper>::new();

View File

@ -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<usize, std::io::Error> {
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(())
}

View File

@ -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);

View File

@ -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<usize, std::io::Error> {
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(())

View File

@ -3,17 +3,16 @@ 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;
@ -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 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 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
);
}

View File

@ -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(())
}

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_client::{BackupWriter, HttpClient, HttpClientOptions};
async fn upload_speed() -> Result<f64, Error> {
let host = "localhost";
let datastore = "store2";
@ -18,7 +17,17 @@ async fn upload_speed() -> Result<f64, Error> {
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?;