test(benches): update pipeline benchmark to async/await
This commit is contained in:
committed by
Sean McArthur
parent
ed10ffaef3
commit
63a8f26583
@@ -151,10 +151,10 @@ required-features = ["runtime"]
|
|||||||
#path = "benches/end_to_end.rs"
|
#path = "benches/end_to_end.rs"
|
||||||
#required-features = ["runtime"]
|
#required-features = ["runtime"]
|
||||||
|
|
||||||
#[[bench]]
|
[[bench]]
|
||||||
#name = "pipeline"
|
name = "pipeline"
|
||||||
#path = "benches/pipeline.rs"
|
path = "benches/pipeline.rs"
|
||||||
#required-features = ["runtime"]
|
required-features = ["runtime"]
|
||||||
|
|
||||||
#[[bench]]
|
#[[bench]]
|
||||||
#name = "server"
|
#name = "server"
|
||||||
|
|||||||
@@ -1,21 +1,19 @@
|
|||||||
|
#![feature(async_await)]
|
||||||
#![feature(test)]
|
#![feature(test)]
|
||||||
#![deny(warnings)]
|
#![deny(warnings)]
|
||||||
|
|
||||||
extern crate futures;
|
|
||||||
extern crate hyper;
|
|
||||||
extern crate pretty_env_logger;
|
|
||||||
extern crate test;
|
extern crate test;
|
||||||
extern crate tokio;
|
|
||||||
|
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::net::{TcpStream};
|
use std::net::{TcpStream};
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use futures::Future;
|
use tokio::runtime::current_thread;
|
||||||
use futures::sync::oneshot;
|
use tokio::sync::oneshot;
|
||||||
|
|
||||||
use hyper::{Body, Response, Server};
|
use hyper::{Body, Response, Server};
|
||||||
use hyper::service::service_fn_ok;
|
use hyper::service::{make_service_fn, service_fn};
|
||||||
|
|
||||||
const PIPELINED_REQUESTS: usize = 16;
|
const PIPELINED_REQUESTS: usize = 16;
|
||||||
|
|
||||||
@@ -23,24 +21,34 @@ const PIPELINED_REQUESTS: usize = 16;
|
|||||||
fn hello_world(b: &mut test::Bencher) {
|
fn hello_world(b: &mut test::Bencher) {
|
||||||
let _ = pretty_env_logger::try_init();
|
let _ = pretty_env_logger::try_init();
|
||||||
let (_until_tx, until_rx) = oneshot::channel::<()>();
|
let (_until_tx, until_rx) = oneshot::channel::<()>();
|
||||||
|
|
||||||
let addr = {
|
let addr = {
|
||||||
let (addr_tx, addr_rx) = mpsc::channel();
|
let (addr_tx, addr_rx) = mpsc::channel();
|
||||||
::std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
let addr = "127.0.0.1:0".parse().unwrap();
|
let addr = "127.0.0.1:0".parse().unwrap();
|
||||||
|
|
||||||
|
let make_svc = make_service_fn(|_| async {
|
||||||
|
Ok::<_, hyper::Error>(service_fn(|_| async {
|
||||||
|
Ok::<_, hyper::Error>(Response::new(Body::from("Hello, World!")))
|
||||||
|
}))
|
||||||
|
});
|
||||||
let srv = Server::bind(&addr)
|
let srv = Server::bind(&addr)
|
||||||
.http1_pipeline_flush(true)
|
.http1_pipeline_flush(true)
|
||||||
.serve(|| {
|
.serve(make_svc);
|
||||||
service_fn_ok(move |_| {
|
|
||||||
Response::new(Body::from("Hello, World!"))
|
|
||||||
})
|
|
||||||
});
|
|
||||||
addr_tx.send(srv.local_addr()).unwrap();
|
addr_tx.send(srv.local_addr()).unwrap();
|
||||||
let fut = srv
|
|
||||||
.map_err(|e| panic!("server error: {}", e))
|
let graceful = srv
|
||||||
.select(until_rx.then(|_| Ok(())))
|
.with_graceful_shutdown(async {
|
||||||
.then(|_| Ok(()));
|
until_rx.await.ok();
|
||||||
let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
|
});
|
||||||
rt.spawn(fut);
|
|
||||||
|
let mut rt = current_thread::Runtime::new().unwrap();
|
||||||
|
rt.spawn(async {
|
||||||
|
if let Err(e) = graceful.await {
|
||||||
|
panic!("server error: {}", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
rt.run().unwrap();
|
rt.run().unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -60,7 +68,7 @@ fn hello_world(b: &mut test::Bencher) {
|
|||||||
} * PIPELINED_REQUESTS;
|
} * PIPELINED_REQUESTS;
|
||||||
|
|
||||||
let mut tcp = TcpStream::connect(addr).unwrap();
|
let mut tcp = TcpStream::connect(addr).unwrap();
|
||||||
tcp.set_read_timeout(Some(::std::time::Duration::from_secs(3))).unwrap();
|
tcp.set_read_timeout(Some(Duration::from_secs(3))).unwrap();
|
||||||
let mut buf = [0u8; 8192];
|
let mut buf = [0u8; 8192];
|
||||||
|
|
||||||
b.bytes = (pipelined_reqs.len() + total_bytes) as u64;
|
b.bytes = (pipelined_reqs.len() + total_bytes) as u64;
|
||||||
Reference in New Issue
Block a user