Skip to content

Commit 2497023

Browse files
committed
fix(benches): re-enable server.rs benches
re-enable `server.rs` benches that relied on `Server`
1 parent cc347ee commit 2497023

File tree

1 file changed

+131
-125
lines changed

1 file changed

+131
-125
lines changed

benches/server.rs

Lines changed: 131 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -3,133 +3,139 @@
33

44
extern crate test;
55

6-
// TODO: Reimplement bench_server using hyper::server::conn (instead
7-
// of removed Server).
8-
96
use std::io::{Read, Write};
10-
use std::net::{TcpListener, TcpStream};
7+
use std::net::{SocketAddr, TcpListener, TcpStream};
118
use std::sync::mpsc;
12-
// use std::time::Duration;
13-
14-
// use futures_util::{stream, StreamExt};
15-
// use http_body_util::StreamBody;
16-
// use tokio::sync::oneshot;
17-
18-
// use hyper::service::{make_service_fn, service_fn};
19-
// use hyper::{Response, Server};
20-
21-
// macro_rules! bench_server {
22-
// ($b:ident, $header:expr, $body:expr) => {{
23-
// let _ = pretty_env_logger::try_init();
24-
// let (_until_tx, until_rx) = oneshot::channel::<()>();
25-
// let addr = {
26-
// let (addr_tx, addr_rx) = mpsc::channel();
27-
// std::thread::spawn(move || {
28-
// let addr = "127.0.0.1:0".parse().unwrap();
29-
// let make_svc = make_service_fn(|_| async {
30-
// Ok::<_, hyper::Error>(service_fn(|_| async {
31-
// Ok::<_, hyper::Error>(
32-
// Response::builder()
33-
// .header($header.0, $header.1)
34-
// .header("content-type", "text/plain")
35-
// .body($body())
36-
// .unwrap(),
37-
// )
38-
// }))
39-
// });
40-
41-
// let rt = tokio::runtime::Builder::new_current_thread()
42-
// .enable_all()
43-
// .build()
44-
// .expect("rt build");
45-
46-
// let srv = rt.block_on(async move { Server::bind(&addr).serve(make_svc) });
47-
48-
// addr_tx.send(srv.local_addr()).unwrap();
49-
50-
// let graceful = srv.with_graceful_shutdown(async {
51-
// until_rx.await.ok();
52-
// });
53-
// rt.block_on(async move {
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 total_bytes = {
64-
// let mut tcp = TcpStream::connect(addr).unwrap();
65-
// tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
66-
// .unwrap();
67-
// let mut buf = Vec::new();
68-
// tcp.read_to_end(&mut buf).unwrap()
69-
// };
70-
71-
// let mut tcp = TcpStream::connect(addr).unwrap();
72-
// tcp.set_read_timeout(Some(Duration::from_secs(3))).unwrap();
73-
// let mut buf = [0u8; 8192];
74-
75-
// $b.bytes = 35 + total_bytes as u64;
76-
// $b.iter(|| {
77-
// tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")
78-
// .unwrap();
79-
// let mut sum = 0;
80-
// while sum < total_bytes {
81-
// sum += tcp.read(&mut buf).unwrap();
82-
// }
83-
// assert_eq!(sum, total_bytes);
84-
// });
85-
// }};
86-
// }
87-
88-
// fn body(b: &'static [u8]) -> hyper::Body {
89-
// b.into()
90-
// }
91-
92-
// #[bench]
93-
// fn throughput_fixedsize_small_payload(b: &mut test::Bencher) {
94-
// bench_server!(b, ("content-length", "13"), || body(b"Hello, World!"))
95-
// }
96-
97-
// #[bench]
98-
// fn throughput_fixedsize_large_payload(b: &mut test::Bencher) {
99-
// bench_server!(b, ("content-length", "1000000"), || body(
100-
// &[b'x'; 1_000_000]
101-
// ))
102-
// }
103-
104-
// #[bench]
105-
// fn throughput_fixedsize_many_chunks(b: &mut test::Bencher) {
106-
// bench_server!(b, ("content-length", "1000000"), || {
107-
// static S: &[&[u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
108-
// StreamBody::new(stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)))
109-
// })
110-
// }
111-
112-
// #[bench]
113-
// fn throughput_chunked_small_payload(b: &mut test::Bencher) {
114-
// bench_server!(b, ("transfer-encoding", "chunked"), || body(
115-
// b"Hello, World!"
116-
// ))
117-
// }
118-
119-
// #[bench]
120-
// fn throughput_chunked_large_payload(b: &mut test::Bencher) {
121-
// bench_server!(b, ("transfer-encoding", "chunked"), || body(
122-
// &[b'x'; 1_000_000]
123-
// ))
124-
// }
125-
126-
// #[bench]
127-
// fn throughput_chunked_many_chunks(b: &mut test::Bencher) {
128-
// bench_server!(b, ("transfer-encoding", "chunked"), || {
129-
// static S: &[&[u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
130-
// StreamBody::new(stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)))
131-
// })
132-
// }
9+
use std::time::Duration;
10+
11+
use futures_util::{stream, StreamExt};
12+
use http_body_util::{BodyExt, StreamBody};
13+
use tokio::sync::oneshot;
14+
15+
use hyper::server::conn::Http;
16+
use hyper::service::service_fn;
17+
use hyper::Response;
18+
19+
macro_rules! bench_server {
20+
($b:ident, $header:expr, $body:expr) => {{
21+
let _ = pretty_env_logger::try_init();
22+
let (_until_tx, until_rx) = oneshot::channel::<()>();
23+
24+
let addr = {
25+
let (addr_tx, addr_rx) = mpsc::channel();
26+
std::thread::spawn(move || {
27+
let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
28+
let rt = tokio::runtime::Builder::new_current_thread()
29+
.enable_all()
30+
.build()
31+
.expect("rt build");
32+
33+
let listener = rt.block_on(tokio::net::TcpListener::bind(addr)).unwrap();
34+
let addr = listener.local_addr().unwrap();
35+
36+
rt.spawn(async move {
37+
loop {
38+
let (stream, _) = listener.accept().await.expect("accept");
39+
40+
Http::new()
41+
.serve_connection(
42+
stream,
43+
service_fn(|_| async {
44+
Ok::<_, hyper::Error>(
45+
Response::builder()
46+
.header($header.0, $header.1)
47+
.header("content-type", "text/plain")
48+
.body($body())
49+
.unwrap(),
50+
)
51+
}),
52+
)
53+
.await
54+
.unwrap();
55+
}
56+
});
57+
58+
addr_tx.send(addr).unwrap();
59+
rt.block_on(until_rx).ok();
60+
});
61+
62+
addr_rx.recv().unwrap()
63+
};
64+
65+
let total_bytes = {
66+
let mut tcp = TcpStream::connect(addr).unwrap();
67+
tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
68+
.unwrap();
69+
let mut buf = Vec::new();
70+
tcp.read_to_end(&mut buf).unwrap()
71+
};
72+
73+
let mut tcp = TcpStream::connect(addr).unwrap();
74+
tcp.set_read_timeout(Some(Duration::from_secs(3))).unwrap();
75+
let mut buf = [0u8; 8192];
76+
77+
$b.bytes = 35 + total_bytes as u64;
78+
$b.iter(|| {
79+
tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")
80+
.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+
}};
88+
}
89+
90+
fn body(b: &'static [u8]) -> hyper::Body {
91+
b.into()
92+
}
93+
94+
#[bench]
95+
fn throughput_fixedsize_small_payload(b: &mut test::Bencher) {
96+
bench_server!(b, ("content-length", "13"), || body(b"Hello, World!"))
97+
}
98+
99+
#[bench]
100+
fn throughput_fixedsize_large_payload(b: &mut test::Bencher) {
101+
bench_server!(b, ("content-length", "1000000"), || body(
102+
&[b'x'; 1_000_000]
103+
))
104+
}
105+
106+
#[bench]
107+
fn throughput_fixedsize_many_chunks(b: &mut test::Bencher) {
108+
bench_server!(b, ("content-length", "1000000"), move || {
109+
static S: &[&[u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
110+
BodyExt::boxed(StreamBody::new(
111+
stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)),
112+
))
113+
})
114+
}
115+
116+
#[bench]
117+
fn throughput_chunked_small_payload(b: &mut test::Bencher) {
118+
bench_server!(b, ("transfer-encoding", "chunked"), || body(
119+
b"Hello, World!"
120+
))
121+
}
122+
123+
#[bench]
124+
fn throughput_chunked_large_payload(b: &mut test::Bencher) {
125+
bench_server!(b, ("transfer-encoding", "chunked"), || body(
126+
&[b'x'; 1_000_000]
127+
))
128+
}
129+
130+
#[bench]
131+
fn throughput_chunked_many_chunks(b: &mut test::Bencher) {
132+
bench_server!(b, ("transfer-encoding", "chunked"), || {
133+
static S: &[&[u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
134+
BodyExt::boxed(StreamBody::new(
135+
stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)),
136+
))
137+
})
138+
}
133139

134140
#[bench]
135141
fn raw_tcp_throughput_small_payload(b: &mut test::Bencher) {

0 commit comments

Comments
 (0)