diff --git a/benches/end_to_end.rs b/benches/end_to_end.rs index 78a3232d..6e1c0348 100644 --- a/benches/end_to_end.rs +++ b/benches/end_to_end.rs @@ -33,6 +33,13 @@ fn http1_post(b: &mut test::Bencher) { }); } +#[bench] +fn http1_get_parallel(b: &mut test::Bencher) { + bench_parallel_with(b, Version::HTTP_11, || { + Request::new(Body::empty()) + }); +} + #[bench] fn http2_get(b: &mut test::Bencher) { bench_with(b, Version::HTTP_2, || { @@ -49,10 +56,18 @@ fn http2_post(b: &mut test::Bencher) { }); } +#[bench] +fn http2_get_parallel(b: &mut test::Bencher) { + bench_parallel_with(b, Version::HTTP_2, || { + Request::new(Body::empty()) + }); +} + fn bench_with(b: &mut test::Bencher, version: Version, make_request: F) where F: Fn() -> Request, { + let _ = pretty_env_logger::try_init(); let mut rt = Runtime::new().unwrap(); let body = b"Hello"; let addr = spawn_hello(&mut rt, body); @@ -76,6 +91,39 @@ where }); } +fn bench_parallel_with(b: &mut test::Bencher, version: Version, make_request: F) +where + F: Fn() -> Request, +{ + let _ = pretty_env_logger::try_init(); + let mut rt = Runtime::new().unwrap(); + let body = b"Hello"; + let addr = spawn_hello(&mut rt, body); + + let connector = HttpConnector::new(1); + let client = hyper::Client::builder() + .http2_only(version == Version::HTTP_2) + .build::<_, Body>(connector); + + let url: hyper::Uri = format!("http://{}/hello", addr).parse().unwrap(); + + b.bytes = body.len() as u64; + b.iter(move || { + let futs = (0..10) + .into_iter() + .map(|_| { + let mut req = make_request(); + *req.uri_mut() = url.clone(); + client.request(req).and_then(|res| { + res.into_body().for_each(|_chunk| { + Ok(()) + }) + }).map_err(|_e| ()) + }); + let _ = rt.block_on(::futures::future::join_all(futs)); + }); +} + fn spawn_hello(rt: &mut Runtime, body: &'static [u8]) -> SocketAddr { use hyper::service::{service_fn}; let addr = "127.0.0.1:0".parse().unwrap(); diff --git a/src/proto/h2/server.rs b/src/proto/h2/server.rs index 7714a1b8..8bc36bc3 100644 --- a/src/proto/h2/server.rs +++ b/src/proto/h2/server.rs @@ -134,16 +134,8 @@ where let req = req.map(|stream| { ::Body::h2(stream, content_length) }); - let mut fut = H2Stream::new(service.call(req), respond); - - // try to eagerly poll the future, so that we might - // not need to allocate a new task... - match fut.poll() { - Ok(Async::Ready(())) | Err(()) => (), - Ok(Async::NotReady) => { - exec.execute_h2stream(fut)?; - } - } + let fut = H2Stream::new(service.call(req), respond); + exec.execute_h2stream(fut)?; } // no more incoming streams...