From 17243003b3c5b04606b4a538a111bc1c9a6bbca5 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 29 Jun 2019 13:43:10 +0200 Subject: [PATCH] src/bin/download-speed.rs: h2 download speedtest for our server I get about 30MB/s (much too slow) --- src/api2/reader.rs | 2 +- src/bin/download-speed.rs | 95 +++++++++++++++++++++++++++++++++++++++ src/client/http_client.rs | 7 +++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/bin/download-speed.rs diff --git a/src/api2/reader.rs b/src/api2/reader.rs index 11ae96c9..46d5152e 100644 --- a/src/api2/reader.rs +++ b/src/api2/reader.rs @@ -260,7 +260,7 @@ fn speedtest( _rpcenv: Box, ) -> Result { - let buffer = vec![2u8; 8*1024*1024]; // nonsense [2,2,2,2,2...] + let buffer = vec![65u8; 1024*1024]; // nonsense [A,A,A...] let body = Body::from(buffer); diff --git a/src/bin/download-speed.rs b/src/bin/download-speed.rs new file mode 100644 index 00000000..5b38077c --- /dev/null +++ b/src/bin/download-speed.rs @@ -0,0 +1,95 @@ +use failure::*; +use futures::*; +use std::io::Write; + +//use std::sync::Arc; +//use serde_json::Value; +use chrono::{DateTime, Local}; + +//use proxmox_backup::tools; +//use proxmox_backup::backup::*; +use proxmox_backup::client::*; +//use proxmox_backup::pxar; +//use futures::stream::Stream; + +pub struct DummyWriter { + bytes: usize, +} + +impl Write for DummyWriter { + + fn write(&mut self, data: &[u8]) -> Result { + self.bytes += data.len(); + Ok(data.len()) + } + + fn flush(&mut self) -> Result<(), std::io::Error> { + Ok(()) + } +} + + +fn run() -> Result<(), Error> { + + let host = "localhost"; + + let username = "root@pam"; + + let client = HttpClient::new(host, username)?; + + let backup_time = "2019-06-28T10:49:48+02:00".parse::>()?; + + let client = client.start_backup_reader("store2", "host", "elsa", backup_time, true).wait()?; + + 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; + + println!("Downloaded {} bytes, {} MB/s", bytes, (bytes as f64)/(elapsed*1024.0*1024.0)); + } + } + Ok::<_, Error>(()) + }) + .wait()?; + + 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"); +} diff --git a/src/client/http_client.rs b/src/client/http_client.rs index a5cc406f..3e5750e2 100644 --- a/src/client/http_client.rs +++ b/src/client/http_client.rs @@ -500,6 +500,13 @@ impl BackupReader { self.h2.download(path, Some(param), output) } + pub fn speedtest( + &self, + output: W, + ) -> impl Future { + self.h2.download("speedtest", None, output) + } + pub fn download_chunk( &self, digest: &[u8; 32],