]>
Commit | Line | Data |
---|---|---|
15d0e4a3 WB |
1 | use std::future::Future; |
2 | use std::pin::Pin; | |
3 | use std::task::{Context, Poll}; | |
4 | ||
19f5aa25 | 5 | use anyhow::Error; |
15d0e4a3 WB |
6 | use futures::future::TryFutureExt; |
7 | use futures::stream::Stream; | |
8 | use tokio::net::TcpStream; | |
fded1f31 DM |
9 | |
10 | // Simple H2 client to test H2 download speed using h2server.rs | |
11 | ||
fded1f31 DM |
12 | struct Process { |
13 | body: h2::RecvStream, | |
14 | trailers: bool, | |
15 | bytes: usize, | |
16 | } | |
17 | ||
18 | impl Future for Process { | |
15d0e4a3 WB |
19 | type Output = Result<usize, Error>; |
20 | ||
21 | fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { | |
22 | let this = self.get_mut(); | |
fded1f31 | 23 | |
fded1f31 | 24 | loop { |
15d0e4a3 WB |
25 | if this.trailers { |
26 | match futures::ready!(this.body.poll_trailers(cx)) { | |
27 | Ok(Some(trailers)) => println!("trailers: {:?}", trailers), | |
28 | Ok(None) => (), | |
29 | Err(err) => return Poll::Ready(Err(Error::from(err))), | |
fded1f31 | 30 | } |
fded1f31 | 31 | |
15d0e4a3 WB |
32 | println!("Received {} bytes", this.bytes); |
33 | ||
34 | return Poll::Ready(Ok(this.bytes)); | |
fded1f31 | 35 | } else { |
15d0e4a3 WB |
36 | match futures::ready!(Pin::new(&mut this.body).poll_next(cx)) { |
37 | Some(Ok(chunk)) => { | |
db0cb9ce | 38 | this.body.flow_control().release_capacity(chunk.len())?; |
15d0e4a3 | 39 | this.bytes += chunk.len(); |
fded1f31 | 40 | // println!("GOT FRAME {}", chunk.len()); |
19f5aa25 | 41 | } |
15d0e4a3 | 42 | Some(Err(err)) => return Poll::Ready(Err(Error::from(err))), |
fded1f31 | 43 | None => { |
15d0e4a3 | 44 | this.trailers = true; |
19f5aa25 | 45 | } |
fded1f31 DM |
46 | } |
47 | } | |
48 | } | |
49 | } | |
50 | } | |
51 | ||
15d0e4a3 WB |
52 | fn send_request( |
53 | mut client: h2::client::SendRequest<bytes::Bytes>, | |
54 | ) -> impl Future<Output = Result<usize, Error>> { | |
fded1f31 DM |
55 | println!("sending request"); |
56 | ||
57 | let request = http::Request::builder() | |
58 | .uri("http://localhost/") | |
59 | .body(()) | |
60 | .unwrap(); | |
61 | ||
62 | let (response, _stream) = client.send_request(request, true).unwrap(); | |
63 | ||
19f5aa25 FG |
64 | response.map_err(Error::from).and_then(|response| Process { |
65 | body: response.into_body(), | |
66 | trailers: false, | |
67 | bytes: 0, | |
68 | }) | |
fded1f31 DM |
69 | } |
70 | ||
d973aa82 WB |
71 | fn main() -> Result<(), Error> { |
72 | proxmox_backup::tools::runtime::main(run()) | |
73 | } | |
74 | ||
75 | async fn run() -> Result<(), Error> { | |
fded1f31 DM |
76 | let start = std::time::SystemTime::now(); |
77 | ||
19f5aa25 FG |
78 | let conn = TcpStream::connect(std::net::SocketAddr::from(([127, 0, 0, 1], 8008))).await?; |
79 | conn.set_nodelay(true).unwrap(); | |
fded1f31 | 80 | |
15d0e4a3 | 81 | let (client, h2) = h2::client::Builder::new() |
19f5aa25 FG |
82 | .initial_connection_window_size(1024 * 1024 * 1024) |
83 | .initial_window_size(1024 * 1024 * 1024) | |
84 | .max_frame_size(4 * 1024 * 1024) | |
15d0e4a3 WB |
85 | .handshake(conn) |
86 | .await?; | |
87 | ||
88 | tokio::spawn(async move { | |
89 | if let Err(err) = h2.await { | |
90 | println!("GOT ERR={:?}", err); | |
91 | } | |
92 | }); | |
93 | ||
94 | let mut bytes = 0; | |
95 | for _ in 0..2000 { | |
96 | bytes += send_request(client.clone()).await?; | |
97 | } | |
98 | ||
99 | let elapsed = start.elapsed().unwrap(); | |
19f5aa25 | 100 | let elapsed = (elapsed.as_secs() as f64) + (elapsed.subsec_millis() as f64) / 1000.0; |
fded1f31 | 101 | |
19f5aa25 FG |
102 | println!( |
103 | "Downloaded {} bytes, {} MB/s", | |
104 | bytes, | |
105 | (bytes as f64) / (elapsed * 1024.0 * 1024.0) | |
106 | ); | |
fded1f31 DM |
107 | |
108 | Ok(()) | |
109 | } |