1 use std
::future
::Future
;
3 use std
::task
::{Context, Poll}
;
5 use anyhow
::{format_err, Error}
;
6 use futures
::future
::TryFutureExt
;
7 use futures
::stream
::Stream
;
9 // Simple H2 client to test H2 download speed using h2s-server.rs
17 impl Future
for Process
{
18 type Output
= Result
<usize, Error
>;
20 fn poll(self: Pin
<&mut Self>, cx
: &mut Context
) -> Poll
<Self::Output
> {
21 let this
= self.get_mut();
25 match futures
::ready
!(this
.body
.poll_trailers(cx
)) {
26 Ok(Some(trailers
)) => println
!("trailers: {:?}", trailers
),
28 Err(err
) => return Poll
::Ready(Err(Error
::from(err
))),
31 println
!("Received {} bytes", this
.bytes
);
33 return Poll
::Ready(Ok(this
.bytes
));
35 match futures
::ready
!(Pin
::new(&mut this
.body
).poll_next(cx
)) {
37 this
.body
.flow_control().release_capacity(chunk
.len())?
;
38 this
.bytes
+= chunk
.len();
39 // println!("GOT FRAME {}", chunk.len());
41 Some(Err(err
)) => return Poll
::Ready(Err(Error
::from(err
))),
52 mut client
: h2
::client
::SendRequest
<bytes
::Bytes
>,
53 ) -> impl Future
<Output
= Result
<usize, Error
>> {
54 println
!("sending request");
56 let request
= http
::Request
::builder()
57 .uri("http://localhost/")
61 let (response
, _stream
) = client
.send_request(request
, true).unwrap();
65 .and_then(|response
| {
66 Process { body: response.into_body(), trailers: false, bytes: 0 }
70 fn main() -> Result
<(), Error
> {
71 proxmox_backup
::tools
::runtime
::main(run())
74 async
fn run() -> Result
<(), Error
> {
75 let start
= std
::time
::SystemTime
::now();
78 tokio
::net
::TcpStream
::connect(std
::net
::SocketAddr
::from(([127,0,0,1], 8008))).await?
;
80 conn
.set_nodelay(true).unwrap();
81 conn
.set_recv_buffer_size(1024*1024).unwrap();
83 use openssl
::ssl
::{SslConnector, SslMethod}
;
85 let mut ssl_connector_builder
= SslConnector
::builder(SslMethod
::tls()).unwrap();
86 ssl_connector_builder
.set_verify(openssl
::ssl
::SslVerifyMode
::NONE
);
88 tokio_openssl
::connect(
89 ssl_connector_builder
.build().configure()?
,
94 .map_err(|err
| format_err
!("connect failed - {}", err
))?
;
96 let (client
, h2
) = h2
::client
::Builder
::new()
97 .initial_connection_window_size(1024*1024*1024)
98 .initial_window_size(1024*1024*1024)
99 .max_frame_size(4*1024*1024)
103 // Spawn a task to run the conn...
104 tokio
::spawn(async
move {
105 if let Err(e
) = h2
.await
{
106 println
!("GOT ERR={:?}", e
);
112 match send_request(client
.clone()).await
{
117 println
!("ERROR {}", e
);
123 let elapsed
= start
.elapsed().unwrap();
124 let elapsed
= (elapsed
.as_secs() as f64) +
125 (elapsed
.subsec_millis() as f64)/1000.0;
127 println
!("Downloaded {} bytes, {} MB/s", bytes
, (bytes
as f64)/(elapsed
*1024.0*1024.0));