src/bin/download-speed.rs: switch to async

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-08-28 15:30:25 +02:00
parent 745e652a7f
commit b9203d87f4
1 changed files with 25 additions and 55 deletions

View File

@ -1,16 +1,10 @@
use failure::*;
use futures::*;
use std::io::Write; use std::io::Write;
//use std::sync::Arc; use failure::*;
//use serde_json::Value;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
//use proxmox_backup::tools; use proxmox_backup::client::HttpClient;
//use proxmox_backup::backup::*;
use proxmox_backup::client::*;
//use proxmox_backup::pxar;
//use futures::stream::Stream;
pub struct DummyWriter { pub struct DummyWriter {
bytes: usize, bytes: usize,
@ -29,7 +23,7 @@ impl Write for DummyWriter {
} }
fn run() -> Result<(), Error> { async fn run() -> Result<(), Error> {
let host = "localhost"; let host = "localhost";
@ -39,57 +33,33 @@ fn run() -> Result<(), Error> {
let backup_time = "2019-06-28T10:49:48Z".parse::<DateTime<Utc>>()?; let backup_time = "2019-06-28T10:49:48Z".parse::<DateTime<Utc>>()?;
let client = client.start_backup_reader("store2", "host", "elsa", backup_time, true).wait()?; let client = client
.start_backup_reader("store2", "host", "elsa", backup_time, true)
.await?;
let start = std::time::SystemTime::now(); let start = std::time::SystemTime::now();
futures::stream::repeat(()) let mut bytes = 0;
.take(100) for _ in 0..100 {
.and_then(|_| { let writer = DummyWriter { bytes: 0 };
let writer = DummyWriter { bytes: 0 }; let writer = client.speedtest(writer).await?;
client.speedtest(writer) println!("Received {} bytes", writer.bytes);
.and_then(|writer| { bytes += writer.bytes;
println!("Received {} bytes", writer.bytes); }
Ok(writer.bytes)
})
})
.fold(0, move |mut acc, size| {
acc += size;
Ok::<_, Error>(acc)
})
.then(move |result| {
match result {
Err(err) => {
println!("ERROR {}", err);
}
Ok(bytes) => {
let elapsed = start.elapsed().unwrap();
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)); let elapsed = start.elapsed().unwrap();
} let elapsed = (elapsed.as_secs() as f64) +
} (elapsed.subsec_millis() as f64)/1000.0;
Ok::<_, Error>(())
}) println!("Downloaded {} bytes, {} MB/s", bytes, (bytes as f64)/(elapsed*1024.0*1024.0));
.wait()?;
Ok(()) Ok(())
} }
fn main() { #[tokio::main]
async fn main() {
//let mut rt = tokio::runtime::Runtime::new().unwrap(); if let Err(err) = run().await {
eprintln!("ERROR: {}", err);
// should be rt.block_on_all, but this block forever in release builds }
tokio::run(lazy(move || { println!("DONE");
// let _ = rt.block_on(lazy(move || -> Result<(), ()> {
if let Err(err) = run() {
eprintln!("ERROR: {}", err);
}
println!("DONE1");
Ok(())
}));
println!("DONE2");
} }