Skip to content

Commit 5ed747f

Browse files
committed
fix(benches): re-enable pipeline and e2e bench
re-enable the recently disabled pipeline and e2e bench using `hyper::server::conn` instead of the removed higher-level `Server` api
1 parent 3c7bef3 commit 5ed747f

File tree

2 files changed

+213
-209
lines changed

2 files changed

+213
-209
lines changed

benches/pipeline.rs

Lines changed: 82 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,85 @@
33

44
extern crate test;
55

6-
// TODO: Reimplement hello_world_16 bench using hyper::server::conn
7-
// (instead of Server).
8-
9-
// use std::io::{Read, Write};
10-
// use std::net::TcpStream;
11-
// use std::sync::mpsc;
12-
// use std::time::Duration;
13-
14-
// use tokio::sync::oneshot;
15-
16-
// use hyper::service::{make_service_fn, service_fn};
17-
// use hyper::{Body, Response, Server};
18-
19-
// const PIPELINED_REQUESTS: usize = 16;
20-
21-
// #[bench]
22-
// fn hello_world_16(b: &mut test::Bencher) {
23-
// let _ = pretty_env_logger::try_init();
24-
// let (_until_tx, until_rx) = oneshot::channel::<()>();
25-
26-
// let addr = {
27-
// let (addr_tx, addr_rx) = mpsc::channel();
28-
// std::thread::spawn(move || {
29-
// let addr = "127.0.0.1:0".parse().unwrap();
30-
31-
// let make_svc = make_service_fn(|_| async {
32-
// Ok::<_, hyper::Error>(service_fn(|_| async {
33-
// Ok::<_, hyper::Error>(Response::new(Body::from("Hello, World!")))
34-
// }))
35-
// });
36-
37-
// let rt = tokio::runtime::Builder::new_current_thread()
38-
// .enable_all()
39-
// .build()
40-
// .expect("rt build");
41-
// let srv = rt.block_on(async move {
42-
// Server::bind(&addr)
43-
// .http1_pipeline_flush(true)
44-
// .serve(make_svc)
45-
// });
46-
47-
// addr_tx.send(srv.local_addr()).unwrap();
48-
49-
// let graceful = srv.with_graceful_shutdown(async {
50-
// until_rx.await.ok();
51-
// });
52-
53-
// rt.block_on(async {
54-
// if let Err(e) = graceful.await {
55-
// panic!("server error: {}", e);
56-
// }
57-
// });
58-
// });
59-
60-
// addr_rx.recv().unwrap()
61-
// };
62-
63-
// let mut pipelined_reqs = Vec::new();
64-
// for _ in 0..PIPELINED_REQUESTS {
65-
// pipelined_reqs.extend_from_slice(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
66-
// }
67-
68-
// let total_bytes = {
69-
// let mut tcp = TcpStream::connect(addr).unwrap();
70-
// tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
71-
// .unwrap();
72-
// let mut buf = Vec::new();
73-
// tcp.read_to_end(&mut buf).unwrap()
74-
// } * PIPELINED_REQUESTS;
75-
76-
// let mut tcp = TcpStream::connect(addr).unwrap();
77-
// tcp.set_read_timeout(Some(Duration::from_secs(3))).unwrap();
78-
// let mut buf = [0u8; 8192];
79-
80-
// b.bytes = (pipelined_reqs.len() + total_bytes) as u64;
81-
// b.iter(|| {
82-
// tcp.write_all(&pipelined_reqs).unwrap();
83-
// let mut sum = 0;
84-
// while sum < total_bytes {
85-
// sum += tcp.read(&mut buf).unwrap();
86-
// }
87-
// assert_eq!(sum, total_bytes);
88-
// });
89-
// }
6+
use std::io::{Read, Write};
7+
use std::net::{SocketAddr, TcpStream};
8+
use std::sync::mpsc;
9+
use std::time::Duration;
10+
11+
use tokio::net::TcpListener;
12+
use tokio::sync::oneshot;
13+
14+
use hyper::server::conn::Http;
15+
use hyper::service::service_fn;
16+
use hyper::{Body, Response};
17+
18+
const PIPELINED_REQUESTS: usize = 16;
19+
20+
#[bench]
21+
fn hello_world_16(b: &mut test::Bencher) {
22+
let _ = pretty_env_logger::try_init();
23+
let (_until_tx, until_rx) = oneshot::channel::<()>();
24+
25+
let addr = {
26+
let (addr_tx, addr_rx) = mpsc::channel();
27+
std::thread::spawn(move || {
28+
let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
29+
let rt = tokio::runtime::Builder::new_current_thread()
30+
.enable_all()
31+
.build()
32+
.expect("rt build");
33+
34+
let listener = rt.block_on(TcpListener::bind(addr)).unwrap();
35+
let addr = listener.local_addr().unwrap();
36+
37+
rt.spawn(async move {
38+
loop {
39+
let (stream, _addr) = listener.accept().await.expect("accept");
40+
41+
Http::new()
42+
.pipeline_flush(true)
43+
.serve_connection(
44+
stream,
45+
service_fn(|_| async {
46+
Ok::<_, hyper::Error>(Response::new(Body::from("Hello, World!")))
47+
}),
48+
)
49+
.await
50+
.unwrap();
51+
}
52+
});
53+
54+
addr_tx.send(addr).unwrap();
55+
rt.block_on(until_rx).ok();
56+
});
57+
58+
addr_rx.recv().unwrap()
59+
};
60+
61+
let mut pipelined_reqs = Vec::new();
62+
for _ in 0..PIPELINED_REQUESTS {
63+
pipelined_reqs.extend_from_slice(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
64+
}
65+
66+
let total_bytes = {
67+
let mut tcp = TcpStream::connect(addr).unwrap();
68+
tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
69+
.unwrap();
70+
let mut buf = Vec::new();
71+
tcp.read_to_end(&mut buf).unwrap()
72+
} * PIPELINED_REQUESTS;
73+
74+
let mut tcp = TcpStream::connect(addr).unwrap();
75+
tcp.set_read_timeout(Some(Duration::from_secs(3))).unwrap();
76+
let mut buf = [0u8; 8192];
77+
78+
b.bytes = (pipelined_reqs.len() + total_bytes) as u64;
79+
b.iter(|| {
80+
tcp.write_all(&pipelined_reqs).unwrap();
81+
let mut sum = 0;
82+
while sum < total_bytes {
83+
sum += tcp.read(&mut buf).unwrap();
84+
}
85+
assert_eq!(sum, total_bytes);
86+
});
87+
}

0 commit comments

Comments
 (0)