Skip to content

Commit 63a8f26

Browse files
weihangloseanmonstar
authored andcommitted
test(benches): update pipeline benchmark to async/await
1 parent ed10ffa commit 63a8f26

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ required-features = ["runtime"]
151151
#path = "benches/end_to_end.rs"
152152
#required-features = ["runtime"]
153153

154-
#[[bench]]
155-
#name = "pipeline"
156-
#path = "benches/pipeline.rs"
157-
#required-features = ["runtime"]
154+
[[bench]]
155+
name = "pipeline"
156+
path = "benches/pipeline.rs"
157+
required-features = ["runtime"]
158158

159159
#[[bench]]
160160
#name = "server"

benches_disabled/pipeline.rs renamed to benches/pipeline.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,54 @@
1+
#![feature(async_await)]
12
#![feature(test)]
23
#![deny(warnings)]
34

4-
extern crate futures;
5-
extern crate hyper;
6-
extern crate pretty_env_logger;
75
extern crate test;
8-
extern crate tokio;
96

107
use std::io::{Read, Write};
118
use std::net::{TcpStream};
129
use std::sync::mpsc;
10+
use std::time::Duration;
1311

14-
use futures::Future;
15-
use futures::sync::oneshot;
12+
use tokio::runtime::current_thread;
13+
use tokio::sync::oneshot;
1614

1715
use hyper::{Body, Response, Server};
18-
use hyper::service::service_fn_ok;
16+
use hyper::service::{make_service_fn, service_fn};
1917

2018
const PIPELINED_REQUESTS: usize = 16;
2119

2220
#[bench]
2321
fn hello_world(b: &mut test::Bencher) {
2422
let _ = pretty_env_logger::try_init();
2523
let (_until_tx, until_rx) = oneshot::channel::<()>();
24+
2625
let addr = {
2726
let (addr_tx, addr_rx) = mpsc::channel();
28-
::std::thread::spawn(move || {
27+
std::thread::spawn(move || {
2928
let addr = "127.0.0.1:0".parse().unwrap();
29+
30+
let make_svc = make_service_fn(|_| async {
31+
Ok::<_, hyper::Error>(service_fn(|_| async {
32+
Ok::<_, hyper::Error>(Response::new(Body::from("Hello, World!")))
33+
}))
34+
});
3035
let srv = Server::bind(&addr)
3136
.http1_pipeline_flush(true)
32-
.serve(|| {
33-
service_fn_ok(move |_| {
34-
Response::new(Body::from("Hello, World!"))
35-
})
36-
});
37+
.serve(make_svc);
38+
3739
addr_tx.send(srv.local_addr()).unwrap();
38-
let fut = srv
39-
.map_err(|e| panic!("server error: {}", e))
40-
.select(until_rx.then(|_| Ok(())))
41-
.then(|_| Ok(()));
42-
let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
43-
rt.spawn(fut);
40+
41+
let graceful = srv
42+
.with_graceful_shutdown(async {
43+
until_rx.await.ok();
44+
});
45+
46+
let mut rt = current_thread::Runtime::new().unwrap();
47+
rt.spawn(async {
48+
if let Err(e) = graceful.await {
49+
panic!("server error: {}", e);
50+
}
51+
});
4452
rt.run().unwrap();
4553
});
4654

@@ -60,7 +68,7 @@ fn hello_world(b: &mut test::Bencher) {
6068
} * PIPELINED_REQUESTS;
6169

6270
let mut tcp = TcpStream::connect(addr).unwrap();
63-
tcp.set_read_timeout(Some(::std::time::Duration::from_secs(3))).unwrap();
71+
tcp.set_read_timeout(Some(Duration::from_secs(3))).unwrap();
6472
let mut buf = [0u8; 8192];
6573

6674
b.bytes = (pipelined_reqs.len() + total_bytes) as u64;

0 commit comments

Comments
 (0)