2019-08-28 13:05:45 +00:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{format_err, Error};
|
2019-08-28 13:05:45 +00:00
|
|
|
use futures::future::TryFutureExt;
|
|
|
|
use futures::stream::Stream;
|
2020-12-04 11:59:34 +00:00
|
|
|
use tokio::net::TcpStream;
|
2019-06-29 12:57:54 +00:00
|
|
|
|
|
|
|
// Simple H2 client to test H2 download speed using h2s-server.rs
|
|
|
|
|
2019-06-29 13:58:18 +00:00
|
|
|
struct Process {
|
|
|
|
body: h2::RecvStream,
|
|
|
|
trailers: bool,
|
|
|
|
bytes: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for Process {
|
2019-08-28 13:05:45 +00:00
|
|
|
type Output = Result<usize, Error>;
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let this = self.get_mut();
|
2019-06-29 13:58:18 +00:00
|
|
|
|
|
|
|
loop {
|
2019-08-28 13:05:45 +00:00
|
|
|
if this.trailers {
|
|
|
|
match futures::ready!(this.body.poll_trailers(cx)) {
|
|
|
|
Ok(Some(trailers)) => println!("trailers: {:?}", trailers),
|
|
|
|
Ok(None) => (),
|
|
|
|
Err(err) => return Poll::Ready(Err(Error::from(err))),
|
2019-06-29 13:58:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 13:05:45 +00:00
|
|
|
println!("Received {} bytes", this.bytes);
|
|
|
|
|
|
|
|
return Poll::Ready(Ok(this.bytes));
|
2019-06-29 13:58:18 +00:00
|
|
|
} else {
|
2019-08-28 13:05:45 +00:00
|
|
|
match futures::ready!(Pin::new(&mut this.body).poll_next(cx)) {
|
|
|
|
Some(Ok(chunk)) => {
|
2019-12-12 14:27:07 +00:00
|
|
|
this.body.flow_control().release_capacity(chunk.len())?;
|
2019-08-28 13:05:45 +00:00
|
|
|
this.bytes += chunk.len();
|
2019-06-29 13:58:18 +00:00
|
|
|
// println!("GOT FRAME {}", chunk.len());
|
2020-12-04 11:59:34 +00:00
|
|
|
}
|
2019-08-28 13:05:45 +00:00
|
|
|
Some(Err(err)) => return Poll::Ready(Err(Error::from(err))),
|
2019-06-29 13:58:18 +00:00
|
|
|
None => {
|
2019-08-28 13:05:45 +00:00
|
|
|
this.trailers = true;
|
2020-12-04 11:59:34 +00:00
|
|
|
}
|
2019-06-29 13:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 13:05:45 +00:00
|
|
|
fn send_request(
|
|
|
|
mut client: h2::client::SendRequest<bytes::Bytes>,
|
|
|
|
) -> impl Future<Output = Result<usize, Error>> {
|
2019-06-29 13:58:18 +00:00
|
|
|
println!("sending request");
|
|
|
|
|
|
|
|
let request = http::Request::builder()
|
|
|
|
.uri("http://localhost/")
|
|
|
|
.body(())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let (response, _stream) = client.send_request(request, true).unwrap();
|
|
|
|
|
2020-12-04 11:59:34 +00:00
|
|
|
response.map_err(Error::from).and_then(|response| Process {
|
|
|
|
body: response.into_body(),
|
|
|
|
trailers: false,
|
|
|
|
bytes: 0,
|
|
|
|
})
|
2019-06-29 12:57:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 11:52:22 +00:00
|
|
|
fn main() -> Result<(), Error> {
|
2021-11-19 16:36:06 +00:00
|
|
|
proxmox_async::runtime::main(run())
|
2020-01-20 11:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn run() -> Result<(), Error> {
|
2019-06-29 12:57:54 +00:00
|
|
|
let start = std::time::SystemTime::now();
|
|
|
|
|
2020-12-04 11:59:34 +00:00
|
|
|
let conn = TcpStream::connect(std::net::SocketAddr::from(([127, 0, 0, 1], 8008))).await?;
|
2019-08-28 13:05:45 +00:00
|
|
|
conn.set_nodelay(true).unwrap();
|
|
|
|
|
|
|
|
use openssl::ssl::{SslConnector, SslMethod};
|
|
|
|
|
|
|
|
let mut ssl_connector_builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
|
|
|
ssl_connector_builder.set_verify(openssl::ssl::SslVerifyMode::NONE);
|
2020-12-04 11:59:34 +00:00
|
|
|
let ssl = ssl_connector_builder
|
|
|
|
.build()
|
|
|
|
.configure()?
|
|
|
|
.into_ssl("localhost")?;
|
|
|
|
|
|
|
|
let conn = tokio_openssl::SslStream::new(ssl, conn)?;
|
|
|
|
let mut conn = Box::pin(conn);
|
|
|
|
conn.as_mut()
|
|
|
|
.connect()
|
2019-08-28 13:05:45 +00:00
|
|
|
.await
|
|
|
|
.map_err(|err| format_err!("connect failed - {}", err))?;
|
|
|
|
|
|
|
|
let (client, h2) = h2::client::Builder::new()
|
2020-12-04 11:59:34 +00:00
|
|
|
.initial_connection_window_size(1024 * 1024 * 1024)
|
|
|
|
.initial_window_size(1024 * 1024 * 1024)
|
|
|
|
.max_frame_size(4 * 1024 * 1024)
|
2019-08-28 13:05:45 +00:00
|
|
|
.handshake(conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
2020-12-04 11:59:34 +00:00
|
|
|
if let Err(err) = h2.await {
|
|
|
|
println!("GOT ERR={:?}", err);
|
2019-08-28 13:05:45 +00:00
|
|
|
}
|
|
|
|
});
|
2019-06-29 12:57:54 +00:00
|
|
|
|
2019-08-28 13:05:45 +00:00
|
|
|
let mut bytes = 0;
|
2020-12-04 11:59:34 +00:00
|
|
|
for _ in 0..2000 {
|
|
|
|
bytes += send_request(client.clone()).await?;
|
2019-08-28 13:05:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let elapsed = start.elapsed().unwrap();
|
2020-12-04 11:59:34 +00:00
|
|
|
let elapsed = (elapsed.as_secs() as f64) + (elapsed.subsec_millis() as f64) / 1000.0;
|
2019-06-29 12:57:54 +00:00
|
|
|
|
2020-12-04 11:59:34 +00:00
|
|
|
println!(
|
|
|
|
"Downloaded {} bytes, {} MB/s",
|
|
|
|
bytes,
|
|
|
|
(bytes as f64) / (elapsed * 1024.0 * 1024.0)
|
|
|
|
);
|
2019-06-29 12:57:54 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|