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::sync::Arc;
//use serde_json::Value;
use failure::*;
use chrono::{DateTime, Utc};
//use proxmox_backup::tools;
//use proxmox_backup::backup::*;
use proxmox_backup::client::*;
//use proxmox_backup::pxar;
//use futures::stream::Stream;
use proxmox_backup::client::HttpClient;
pub struct DummyWriter {
bytes: usize,
@ -29,7 +23,7 @@ impl Write for DummyWriter {
}
fn run() -> Result<(), Error> {
async fn run() -> Result<(), Error> {
let host = "localhost";
@ -39,57 +33,33 @@ fn run() -> Result<(), Error> {
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();
futures::stream::repeat(())
.take(100)
.and_then(|_| {
let writer = DummyWriter { bytes: 0 };
client.speedtest(writer)
.and_then(|writer| {
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;
let mut bytes = 0;
for _ in 0..100 {
let writer = DummyWriter { bytes: 0 };
let writer = client.speedtest(writer).await?;
println!("Received {} bytes", writer.bytes);
bytes += writer.bytes;
}
println!("Downloaded {} bytes, {} MB/s", bytes, (bytes as f64)/(elapsed*1024.0*1024.0));
}
}
Ok::<_, Error>(())
})
.wait()?;
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));
Ok(())
}
fn main() {
//let mut rt = tokio::runtime::Runtime::new().unwrap();
// should be rt.block_on_all, but this block forever in release builds
tokio::run(lazy(move || {
// let _ = rt.block_on(lazy(move || -> Result<(), ()> {
if let Err(err) = run() {
eprintln!("ERROR: {}", err);
}
println!("DONE1");
Ok(())
}));
println!("DONE2");
#[tokio::main]
async fn main() {
if let Err(err) = run().await {
eprintln!("ERROR: {}", err);
}
println!("DONE");
}