feat(client,server): remove tcp feature and code (#2929)

This removes the `tcp` feature from hyper's `Cargo.toml`, and the code it enabled:

- `HttpConnector`
- `GaiResolver`
- `AddrStream`

And parts of `Client` and `Server` that used those types. Alternatives will be available in the `hyper-util` crate.

Closes #2856 
Co-authored-by: MrGunflame <mrgunflame@protonmail.com>
This commit is contained in:
Sean McArthur
2022-07-29 10:07:09 -07:00
committed by GitHub
parent d4b5bd4ee6
commit 0c8ee93d7f
40 changed files with 1565 additions and 2676 deletions

View File

@@ -3,35 +3,38 @@
extern crate test;
use http::Uri;
use hyper::client::connect::HttpConnector;
use hyper::service::Service;
use std::net::SocketAddr;
use tokio::net::TcpListener;
// TODO: Reimplement http_connector bench using hyper::client::conn
// (instead of removed HttpConnector).
#[bench]
fn http_connector(b: &mut test::Bencher) {
let _ = pretty_env_logger::try_init();
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("rt build");
let listener = rt
.block_on(TcpListener::bind(&SocketAddr::from(([127, 0, 0, 1], 0))))
.expect("bind");
let addr = listener.local_addr().expect("local_addr");
let dst: Uri = format!("http://{}/", addr).parse().expect("uri parse");
let mut connector = HttpConnector::new();
// use http::Uri;
// use hyper::client::connect::HttpConnector;
// use hyper::service::Service;
// use std::net::SocketAddr;
// use tokio::net::TcpListener;
rt.spawn(async move {
loop {
let _ = listener.accept().await;
}
});
// #[bench]
// fn http_connector(b: &mut test::Bencher) {
// let _ = pretty_env_logger::try_init();
// let rt = tokio::runtime::Builder::new_current_thread()
// .enable_all()
// .build()
// .expect("rt build");
// let listener = rt
// .block_on(TcpListener::bind(&SocketAddr::from(([127, 0, 0, 1], 0))))
// .expect("bind");
// let addr = listener.local_addr().expect("local_addr");
// let dst: Uri = format!("http://{}/", addr).parse().expect("uri parse");
// let mut connector = HttpConnector::new();
b.iter(|| {
rt.block_on(async {
connector.call(dst.clone()).await.expect("connect");
});
});
}
// rt.spawn(async move {
// loop {
// let _ = listener.accept().await;
// }
// });
// b.iter(|| {
// rt.block_on(async {
// connector.call(dst.clone()).await.expect("connect");
// });
// });
// }

View File

@@ -3,380 +3,383 @@
extern crate test;
use std::net::SocketAddr;
// TODO: Reimplement Opts::bench using hyper::server::conn and hyper::client::conn
// (instead of Server and HttpClient).
use futures_util::future::join_all;
// use std::net::SocketAddr;
use hyper::client::HttpConnector;
use hyper::{body::HttpBody as _, Body, Method, Request, Response, Server};
// use futures_util::future::join_all;
// HTTP1
// use hyper::client::HttpConnector;
// use hyper::{body::HttpBody as _, Body, Method, Request, Response, Server};
#[bench]
fn http1_consecutive_x1_empty(b: &mut test::Bencher) {
opts().bench(b)
}
// // HTTP1
#[bench]
fn http1_consecutive_x1_req_10b(b: &mut test::Bencher) {
opts()
.method(Method::POST)
.request_body(&[b's'; 10])
.bench(b)
}
// #[bench]
// fn http1_consecutive_x1_empty(b: &mut test::Bencher) {
// opts().bench(b)
// }
#[bench]
fn http1_consecutive_x1_both_100kb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 100];
opts()
.method(Method::POST)
.request_body(body)
.response_body(body)
.bench(b)
}
// #[bench]
// fn http1_consecutive_x1_req_10b(b: &mut test::Bencher) {
// opts()
// .method(Method::POST)
// .request_body(&[b's'; 10])
// .bench(b)
// }
#[bench]
fn http1_consecutive_x1_both_10mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 10];
opts()
.method(Method::POST)
.request_body(body)
.response_body(body)
.bench(b)
}
// #[bench]
// fn http1_consecutive_x1_both_100kb(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 100];
// opts()
// .method(Method::POST)
// .request_body(body)
// .response_body(body)
// .bench(b)
// }
#[bench]
fn http1_parallel_x10_empty(b: &mut test::Bencher) {
opts().parallel(10).bench(b)
}
// #[bench]
// fn http1_consecutive_x1_both_10mb(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 1024 * 10];
// opts()
// .method(Method::POST)
// .request_body(body)
// .response_body(body)
// .bench(b)
// }
#[bench]
fn http1_parallel_x10_req_10mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 10];
opts()
.parallel(10)
.method(Method::POST)
.request_body(body)
.bench(b)
}
// #[bench]
// fn http1_parallel_x10_empty(b: &mut test::Bencher) {
// opts().parallel(10).bench(b)
// }
#[bench]
fn http1_parallel_x10_req_10kb_100_chunks(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 10];
opts()
.parallel(10)
.method(Method::POST)
.request_chunks(body, 100)
.bench(b)
}
// #[bench]
// fn http1_parallel_x10_req_10mb(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 1024 * 10];
// opts()
// .parallel(10)
// .method(Method::POST)
// .request_body(body)
// .bench(b)
// }
#[bench]
fn http1_parallel_x10_res_1mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 1];
opts().parallel(10).response_body(body).bench(b)
}
// #[bench]
// fn http1_parallel_x10_req_10kb_100_chunks(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 10];
// opts()
// .parallel(10)
// .method(Method::POST)
// .request_chunks(body, 100)
// .bench(b)
// }
#[bench]
fn http1_parallel_x10_res_10mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 10];
opts().parallel(10).response_body(body).bench(b)
}
// #[bench]
// fn http1_parallel_x10_res_1mb(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 1024 * 1];
// opts().parallel(10).response_body(body).bench(b)
// }
// HTTP2
// #[bench]
// fn http1_parallel_x10_res_10mb(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 1024 * 10];
// opts().parallel(10).response_body(body).bench(b)
// }
const HTTP2_MAX_WINDOW: u32 = std::u32::MAX >> 1;
// // HTTP2
#[bench]
fn http2_consecutive_x1_empty(b: &mut test::Bencher) {
opts().http2().bench(b)
}
// const HTTP2_MAX_WINDOW: u32 = std::u32::MAX >> 1;
#[bench]
fn http2_consecutive_x1_req_10b(b: &mut test::Bencher) {
opts()
.http2()
.method(Method::POST)
.request_body(&[b's'; 10])
.bench(b)
}
// #[bench]
// fn http2_consecutive_x1_empty(b: &mut test::Bencher) {
// opts().http2().bench(b)
// }
#[bench]
fn http2_consecutive_x1_req_100kb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 100];
opts()
.http2()
.method(Method::POST)
.request_body(body)
.bench(b)
}
// #[bench]
// fn http2_consecutive_x1_req_10b(b: &mut test::Bencher) {
// opts()
// .http2()
// .method(Method::POST)
// .request_body(&[b's'; 10])
// .bench(b)
// }
#[bench]
fn http2_parallel_x10_empty(b: &mut test::Bencher) {
opts().http2().parallel(10).bench(b)
}
// #[bench]
// fn http2_consecutive_x1_req_100kb(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 100];
// opts()
// .http2()
// .method(Method::POST)
// .request_body(body)
// .bench(b)
// }
#[bench]
fn http2_parallel_x10_req_10mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 10];
opts()
.http2()
.parallel(10)
.method(Method::POST)
.request_body(body)
.http2_stream_window(HTTP2_MAX_WINDOW)
.http2_conn_window(HTTP2_MAX_WINDOW)
.bench(b)
}
// #[bench]
// fn http2_parallel_x10_empty(b: &mut test::Bencher) {
// opts().http2().parallel(10).bench(b)
// }
#[bench]
fn http2_parallel_x10_req_10kb_100_chunks(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 10];
opts()
.http2()
.parallel(10)
.method(Method::POST)
.request_chunks(body, 100)
.bench(b)
}
// #[bench]
// fn http2_parallel_x10_req_10mb(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 1024 * 10];
// opts()
// .http2()
// .parallel(10)
// .method(Method::POST)
// .request_body(body)
// .http2_stream_window(HTTP2_MAX_WINDOW)
// .http2_conn_window(HTTP2_MAX_WINDOW)
// .bench(b)
// }
#[bench]
fn http2_parallel_x10_req_10kb_100_chunks_adaptive_window(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 10];
opts()
.http2()
.parallel(10)
.method(Method::POST)
.request_chunks(body, 100)
.http2_adaptive_window()
.bench(b)
}
// #[bench]
// fn http2_parallel_x10_req_10kb_100_chunks(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 10];
// opts()
// .http2()
// .parallel(10)
// .method(Method::POST)
// .request_chunks(body, 100)
// .bench(b)
// }
#[bench]
fn http2_parallel_x10_req_10kb_100_chunks_max_window(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 10];
opts()
.http2()
.parallel(10)
.method(Method::POST)
.request_chunks(body, 100)
.http2_stream_window(HTTP2_MAX_WINDOW)
.http2_conn_window(HTTP2_MAX_WINDOW)
.bench(b)
}
// #[bench]
// fn http2_parallel_x10_req_10kb_100_chunks_adaptive_window(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 10];
// opts()
// .http2()
// .parallel(10)
// .method(Method::POST)
// .request_chunks(body, 100)
// .http2_adaptive_window()
// .bench(b)
// }
#[bench]
fn http2_parallel_x10_res_1mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 1];
opts()
.http2()
.parallel(10)
.response_body(body)
.http2_stream_window(HTTP2_MAX_WINDOW)
.http2_conn_window(HTTP2_MAX_WINDOW)
.bench(b)
}
// #[bench]
// fn http2_parallel_x10_req_10kb_100_chunks_max_window(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 10];
// opts()
// .http2()
// .parallel(10)
// .method(Method::POST)
// .request_chunks(body, 100)
// .http2_stream_window(HTTP2_MAX_WINDOW)
// .http2_conn_window(HTTP2_MAX_WINDOW)
// .bench(b)
// }
#[bench]
fn http2_parallel_x10_res_10mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 10];
opts()
.http2()
.parallel(10)
.response_body(body)
.http2_stream_window(HTTP2_MAX_WINDOW)
.http2_conn_window(HTTP2_MAX_WINDOW)
.bench(b)
}
// #[bench]
// fn http2_parallel_x10_res_1mb(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 1024 * 1];
// opts()
// .http2()
// .parallel(10)
// .response_body(body)
// .http2_stream_window(HTTP2_MAX_WINDOW)
// .http2_conn_window(HTTP2_MAX_WINDOW)
// .bench(b)
// }
// ==== Benchmark Options =====
// #[bench]
// fn http2_parallel_x10_res_10mb(b: &mut test::Bencher) {
// let body = &[b'x'; 1024 * 1024 * 10];
// opts()
// .http2()
// .parallel(10)
// .response_body(body)
// .http2_stream_window(HTTP2_MAX_WINDOW)
// .http2_conn_window(HTTP2_MAX_WINDOW)
// .bench(b)
// }
struct Opts {
http2: bool,
http2_stream_window: Option<u32>,
http2_conn_window: Option<u32>,
http2_adaptive_window: bool,
parallel_cnt: u32,
request_method: Method,
request_body: Option<&'static [u8]>,
request_chunks: usize,
response_body: &'static [u8],
}
// // ==== Benchmark Options =====
fn opts() -> Opts {
Opts {
http2: false,
http2_stream_window: None,
http2_conn_window: None,
http2_adaptive_window: false,
parallel_cnt: 1,
request_method: Method::GET,
request_body: None,
request_chunks: 0,
response_body: b"",
}
}
// struct Opts {
// http2: bool,
// http2_stream_window: Option<u32>,
// http2_conn_window: Option<u32>,
// http2_adaptive_window: bool,
// parallel_cnt: u32,
// request_method: Method,
// request_body: Option<&'static [u8]>,
// request_chunks: usize,
// response_body: &'static [u8],
// }
impl Opts {
fn http2(mut self) -> Self {
self.http2 = true;
self
}
// fn opts() -> Opts {
// Opts {
// http2: false,
// http2_stream_window: None,
// http2_conn_window: None,
// http2_adaptive_window: false,
// parallel_cnt: 1,
// request_method: Method::GET,
// request_body: None,
// request_chunks: 0,
// response_body: b"",
// }
// }
fn http2_stream_window(mut self, sz: impl Into<Option<u32>>) -> Self {
assert!(!self.http2_adaptive_window);
self.http2_stream_window = sz.into();
self
}
// impl Opts {
// fn http2(mut self) -> Self {
// self.http2 = true;
// self
// }
fn http2_conn_window(mut self, sz: impl Into<Option<u32>>) -> Self {
assert!(!self.http2_adaptive_window);
self.http2_conn_window = sz.into();
self
}
// fn http2_stream_window(mut self, sz: impl Into<Option<u32>>) -> Self {
// assert!(!self.http2_adaptive_window);
// self.http2_stream_window = sz.into();
// self
// }
fn http2_adaptive_window(mut self) -> Self {
assert!(self.http2_stream_window.is_none());
assert!(self.http2_conn_window.is_none());
self.http2_adaptive_window = true;
self
}
// fn http2_conn_window(mut self, sz: impl Into<Option<u32>>) -> Self {
// assert!(!self.http2_adaptive_window);
// self.http2_conn_window = sz.into();
// self
// }
fn method(mut self, m: Method) -> Self {
self.request_method = m;
self
}
// fn http2_adaptive_window(mut self) -> Self {
// assert!(self.http2_stream_window.is_none());
// assert!(self.http2_conn_window.is_none());
// self.http2_adaptive_window = true;
// self
// }
fn request_body(mut self, body: &'static [u8]) -> Self {
self.request_body = Some(body);
self
}
// fn method(mut self, m: Method) -> Self {
// self.request_method = m;
// self
// }
fn request_chunks(mut self, chunk: &'static [u8], cnt: usize) -> Self {
assert!(cnt > 0);
self.request_body = Some(chunk);
self.request_chunks = cnt;
self
}
// fn request_body(mut self, body: &'static [u8]) -> Self {
// self.request_body = Some(body);
// self
// }
fn response_body(mut self, body: &'static [u8]) -> Self {
self.response_body = body;
self
}
// fn request_chunks(mut self, chunk: &'static [u8], cnt: usize) -> Self {
// assert!(cnt > 0);
// self.request_body = Some(chunk);
// self.request_chunks = cnt;
// self
// }
fn parallel(mut self, cnt: u32) -> Self {
assert!(cnt > 0, "parallel count must be larger than 0");
self.parallel_cnt = cnt;
self
}
// fn response_body(mut self, body: &'static [u8]) -> Self {
// self.response_body = body;
// self
// }
fn bench(self, b: &mut test::Bencher) {
use std::sync::Arc;
let _ = pretty_env_logger::try_init();
// Create a runtime of current thread.
let rt = Arc::new(
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("rt build"),
);
let exec = rt.clone();
// fn parallel(mut self, cnt: u32) -> Self {
// assert!(cnt > 0, "parallel count must be larger than 0");
// self.parallel_cnt = cnt;
// self
// }
let req_len = self.request_body.map(|b| b.len()).unwrap_or(0) as u64;
let req_len = if self.request_chunks > 0 {
req_len * self.request_chunks as u64
} else {
req_len
};
let bytes_per_iter = (req_len + self.response_body.len() as u64) * self.parallel_cnt as u64;
b.bytes = bytes_per_iter;
// fn bench(self, b: &mut test::Bencher) {
// use std::sync::Arc;
// let _ = pretty_env_logger::try_init();
// // Create a runtime of current thread.
// let rt = Arc::new(
// tokio::runtime::Builder::new_current_thread()
// .enable_all()
// .build()
// .expect("rt build"),
// );
// let exec = rt.clone();
let addr = spawn_server(&rt, &self);
// let req_len = self.request_body.map(|b| b.len()).unwrap_or(0) as u64;
// let req_len = if self.request_chunks > 0 {
// req_len * self.request_chunks as u64
// } else {
// req_len
// };
// let bytes_per_iter = (req_len + self.response_body.len() as u64) * self.parallel_cnt as u64;
// b.bytes = bytes_per_iter;
let connector = HttpConnector::new();
let client = hyper::Client::builder()
.http2_only(self.http2)
.http2_initial_stream_window_size(self.http2_stream_window)
.http2_initial_connection_window_size(self.http2_conn_window)
.http2_adaptive_window(self.http2_adaptive_window)
.build::<_, Body>(connector);
// let addr = spawn_server(&rt, &self);
let url: hyper::Uri = format!("http://{}/hello", addr).parse().unwrap();
// let connector = HttpConnector::new();
// let client = hyper::Client::builder()
// .http2_only(self.http2)
// .http2_initial_stream_window_size(self.http2_stream_window)
// .http2_initial_connection_window_size(self.http2_conn_window)
// .http2_adaptive_window(self.http2_adaptive_window)
// .build::<_, Body>(connector);
let make_request = || {
let chunk_cnt = self.request_chunks;
let body = if chunk_cnt > 0 {
let (mut tx, body) = Body::channel();
let chunk = self
.request_body
.expect("request_chunks means request_body");
exec.spawn(async move {
for _ in 0..chunk_cnt {
tx.send_data(chunk.into()).await.expect("send_data");
}
});
body
} else {
self.request_body
.map(Body::from)
.unwrap_or_else(Body::empty)
};
let mut req = Request::new(body);
*req.method_mut() = self.request_method.clone();
*req.uri_mut() = url.clone();
req
};
// let url: hyper::Uri = format!("http://{}/hello", addr).parse().unwrap();
let send_request = |req: Request<Body>| {
let fut = client.request(req);
async {
let res = fut.await.expect("client wait");
let mut body = res.into_body();
while let Some(_chunk) = body.data().await {}
}
};
// let make_request = || {
// let chunk_cnt = self.request_chunks;
// let body = if chunk_cnt > 0 {
// let (mut tx, body) = Body::channel();
// let chunk = self
// .request_body
// .expect("request_chunks means request_body");
// exec.spawn(async move {
// for _ in 0..chunk_cnt {
// tx.send_data(chunk.into()).await.expect("send_data");
// }
// });
// body
// } else {
// self.request_body
// .map(Body::from)
// .unwrap_or_else(Body::empty)
// };
// let mut req = Request::new(body);
// *req.method_mut() = self.request_method.clone();
// *req.uri_mut() = url.clone();
// req
// };
if self.parallel_cnt == 1 {
b.iter(|| {
let req = make_request();
rt.block_on(send_request(req));
});
} else {
b.iter(|| {
let futs = (0..self.parallel_cnt).map(|_| {
let req = make_request();
send_request(req)
});
// Await all spawned futures becoming completed.
rt.block_on(join_all(futs));
});
}
}
}
// let send_request = |req: Request<Body>| {
// let fut = client.request(req);
// async {
// let res = fut.await.expect("client wait");
// let mut body = res.into_body();
// while let Some(_chunk) = body.data().await {}
// }
// };
fn spawn_server(rt: &tokio::runtime::Runtime, opts: &Opts) -> SocketAddr {
use hyper::service::{make_service_fn, service_fn};
let addr = "127.0.0.1:0".parse().unwrap();
// if self.parallel_cnt == 1 {
// b.iter(|| {
// let req = make_request();
// rt.block_on(send_request(req));
// });
// } else {
// b.iter(|| {
// let futs = (0..self.parallel_cnt).map(|_| {
// let req = make_request();
// send_request(req)
// });
// // Await all spawned futures becoming completed.
// rt.block_on(join_all(futs));
// });
// }
// }
// }
let body = opts.response_body;
let srv = rt.block_on(async move {
Server::bind(&addr)
.http2_only(opts.http2)
.http2_initial_stream_window_size(opts.http2_stream_window)
.http2_initial_connection_window_size(opts.http2_conn_window)
.http2_adaptive_window(opts.http2_adaptive_window)
.serve(make_service_fn(move |_| async move {
Ok::<_, hyper::Error>(service_fn(move |req: Request<Body>| async move {
let mut req_body = req.into_body();
while let Some(_chunk) = req_body.data().await {}
Ok::<_, hyper::Error>(Response::new(Body::from(body)))
}))
}))
});
let addr = srv.local_addr();
rt.spawn(async {
if let Err(err) = srv.await {
panic!("server error: {}", err);
}
});
addr
}
// fn spawn_server(rt: &tokio::runtime::Runtime, opts: &Opts) -> SocketAddr {
// use hyper::service::{make_service_fn, service_fn};
// let addr = "127.0.0.1:0".parse().unwrap();
// let body = opts.response_body;
// let srv = rt.block_on(async move {
// Server::bind(&addr)
// .http2_only(opts.http2)
// .http2_initial_stream_window_size(opts.http2_stream_window)
// .http2_initial_connection_window_size(opts.http2_conn_window)
// .http2_adaptive_window(opts.http2_adaptive_window)
// .serve(make_service_fn(move |_| async move {
// Ok::<_, hyper::Error>(service_fn(move |req: Request<Body>| async move {
// let mut req_body = req.into_body();
// while let Some(_chunk) = req_body.data().await {}
// Ok::<_, hyper::Error>(Response::new(Body::from(body)))
// }))
// }))
// });
// let addr = srv.local_addr();
// rt.spawn(async {
// if let Err(err) = srv.await {
// panic!("server error: {}", err);
// }
// });
// addr
// }

View File

@@ -3,84 +3,87 @@
extern crate test;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::sync::mpsc;
use std::time::Duration;
// TODO: Reimplement hello_world_16 bench using hyper::server::conn
// (instead of Server).
use tokio::sync::oneshot;
// use std::io::{Read, Write};
// use std::net::TcpStream;
// use std::sync::mpsc;
// use std::time::Duration;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Response, Server};
// use tokio::sync::oneshot;
const PIPELINED_REQUESTS: usize = 16;
// use hyper::service::{make_service_fn, service_fn};
// use hyper::{Body, Response, Server};
#[bench]
fn hello_world_16(b: &mut test::Bencher) {
let _ = pretty_env_logger::try_init();
let (_until_tx, until_rx) = oneshot::channel::<()>();
// const PIPELINED_REQUESTS: usize = 16;
let addr = {
let (addr_tx, addr_rx) = mpsc::channel();
std::thread::spawn(move || {
let addr = "127.0.0.1:0".parse().unwrap();
// #[bench]
// fn hello_world_16(b: &mut test::Bencher) {
// let _ = pretty_env_logger::try_init();
// let (_until_tx, until_rx) = oneshot::channel::<()>();
let make_svc = make_service_fn(|_| async {
Ok::<_, hyper::Error>(service_fn(|_| async {
Ok::<_, hyper::Error>(Response::new(Body::from("Hello, World!")))
}))
});
// let addr = {
// let (addr_tx, addr_rx) = mpsc::channel();
// std::thread::spawn(move || {
// let addr = "127.0.0.1:0".parse().unwrap();
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("rt build");
let srv = rt.block_on(async move {
Server::bind(&addr)
.http1_pipeline_flush(true)
.serve(make_svc)
});
// let make_svc = make_service_fn(|_| async {
// Ok::<_, hyper::Error>(service_fn(|_| async {
// Ok::<_, hyper::Error>(Response::new(Body::from("Hello, World!")))
// }))
// });
addr_tx.send(srv.local_addr()).unwrap();
// let rt = tokio::runtime::Builder::new_current_thread()
// .enable_all()
// .build()
// .expect("rt build");
// let srv = rt.block_on(async move {
// Server::bind(&addr)
// .http1_pipeline_flush(true)
// .serve(make_svc)
// });
let graceful = srv.with_graceful_shutdown(async {
until_rx.await.ok();
});
// addr_tx.send(srv.local_addr()).unwrap();
rt.block_on(async {
if let Err(e) = graceful.await {
panic!("server error: {}", e);
}
});
});
// let graceful = srv.with_graceful_shutdown(async {
// until_rx.await.ok();
// });
addr_rx.recv().unwrap()
};
// rt.block_on(async {
// if let Err(e) = graceful.await {
// panic!("server error: {}", e);
// }
// });
// });
let mut pipelined_reqs = Vec::new();
for _ in 0..PIPELINED_REQUESTS {
pipelined_reqs.extend_from_slice(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
}
// addr_rx.recv().unwrap()
// };
let total_bytes = {
let mut tcp = TcpStream::connect(addr).unwrap();
tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
.unwrap();
let mut buf = Vec::new();
tcp.read_to_end(&mut buf).unwrap()
} * PIPELINED_REQUESTS;
// let mut pipelined_reqs = Vec::new();
// for _ in 0..PIPELINED_REQUESTS {
// pipelined_reqs.extend_from_slice(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
// }
let mut tcp = TcpStream::connect(addr).unwrap();
tcp.set_read_timeout(Some(Duration::from_secs(3))).unwrap();
let mut buf = [0u8; 8192];
// let total_bytes = {
// let mut tcp = TcpStream::connect(addr).unwrap();
// tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
// .unwrap();
// let mut buf = Vec::new();
// tcp.read_to_end(&mut buf).unwrap()
// } * PIPELINED_REQUESTS;
b.bytes = (pipelined_reqs.len() + total_bytes) as u64;
b.iter(|| {
tcp.write_all(&pipelined_reqs).unwrap();
let mut sum = 0;
while sum < total_bytes {
sum += tcp.read(&mut buf).unwrap();
}
assert_eq!(sum, total_bytes);
});
}
// let mut tcp = TcpStream::connect(addr).unwrap();
// tcp.set_read_timeout(Some(Duration::from_secs(3))).unwrap();
// let mut buf = [0u8; 8192];
// b.bytes = (pipelined_reqs.len() + total_bytes) as u64;
// b.iter(|| {
// tcp.write_all(&pipelined_reqs).unwrap();
// let mut sum = 0;
// while sum < total_bytes {
// sum += tcp.read(&mut buf).unwrap();
// }
// assert_eq!(sum, total_bytes);
// });
// }

View File

@@ -3,130 +3,133 @@
extern crate test;
// TODO: Reimplement bench_server using hyper::server::conn (instead
// of removed Server).
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::mpsc;
use std::time::Duration;
// use std::time::Duration;
use futures_util::{stream, StreamExt};
use http_body_util::StreamBody;
use tokio::sync::oneshot;
// use futures_util::{stream, StreamExt};
// use http_body_util::StreamBody;
// use tokio::sync::oneshot;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Response, Server};
// use hyper::service::{make_service_fn, service_fn};
// use hyper::{Response, Server};
macro_rules! bench_server {
($b:ident, $header:expr, $body:expr) => {{
let _ = pretty_env_logger::try_init();
let (_until_tx, until_rx) = oneshot::channel::<()>();
let addr = {
let (addr_tx, addr_rx) = mpsc::channel();
std::thread::spawn(move || {
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::builder()
.header($header.0, $header.1)
.header("content-type", "text/plain")
.body($body())
.unwrap(),
)
}))
});
// macro_rules! bench_server {
// ($b:ident, $header:expr, $body:expr) => {{
// let _ = pretty_env_logger::try_init();
// let (_until_tx, until_rx) = oneshot::channel::<()>();
// let addr = {
// let (addr_tx, addr_rx) = mpsc::channel();
// std::thread::spawn(move || {
// 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::builder()
// .header($header.0, $header.1)
// .header("content-type", "text/plain")
// .body($body())
// .unwrap(),
// )
// }))
// });
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("rt build");
// let rt = tokio::runtime::Builder::new_current_thread()
// .enable_all()
// .build()
// .expect("rt build");
let srv = rt.block_on(async move { Server::bind(&addr).serve(make_svc) });
// let srv = rt.block_on(async move { Server::bind(&addr).serve(make_svc) });
addr_tx.send(srv.local_addr()).unwrap();
// addr_tx.send(srv.local_addr()).unwrap();
let graceful = srv.with_graceful_shutdown(async {
until_rx.await.ok();
});
rt.block_on(async move {
if let Err(e) = graceful.await {
panic!("server error: {}", e);
}
});
});
// let graceful = srv.with_graceful_shutdown(async {
// until_rx.await.ok();
// });
// rt.block_on(async move {
// if let Err(e) = graceful.await {
// panic!("server error: {}", e);
// }
// });
// });
addr_rx.recv().unwrap()
};
// addr_rx.recv().unwrap()
// };
let total_bytes = {
let mut tcp = TcpStream::connect(addr).unwrap();
tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
.unwrap();
let mut buf = Vec::new();
tcp.read_to_end(&mut buf).unwrap()
};
// let total_bytes = {
// let mut tcp = TcpStream::connect(addr).unwrap();
// tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
// .unwrap();
// let mut buf = Vec::new();
// tcp.read_to_end(&mut buf).unwrap()
// };
let mut tcp = TcpStream::connect(addr).unwrap();
tcp.set_read_timeout(Some(Duration::from_secs(3))).unwrap();
let mut buf = [0u8; 8192];
// let mut tcp = TcpStream::connect(addr).unwrap();
// tcp.set_read_timeout(Some(Duration::from_secs(3))).unwrap();
// let mut buf = [0u8; 8192];
$b.bytes = 35 + total_bytes as u64;
$b.iter(|| {
tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")
.unwrap();
let mut sum = 0;
while sum < total_bytes {
sum += tcp.read(&mut buf).unwrap();
}
assert_eq!(sum, total_bytes);
});
}};
}
// $b.bytes = 35 + total_bytes as u64;
// $b.iter(|| {
// tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")
// .unwrap();
// let mut sum = 0;
// while sum < total_bytes {
// sum += tcp.read(&mut buf).unwrap();
// }
// assert_eq!(sum, total_bytes);
// });
// }};
// }
fn body(b: &'static [u8]) -> hyper::Body {
b.into()
}
// fn body(b: &'static [u8]) -> hyper::Body {
// b.into()
// }
#[bench]
fn throughput_fixedsize_small_payload(b: &mut test::Bencher) {
bench_server!(b, ("content-length", "13"), || body(b"Hello, World!"))
}
// #[bench]
// fn throughput_fixedsize_small_payload(b: &mut test::Bencher) {
// bench_server!(b, ("content-length", "13"), || body(b"Hello, World!"))
// }
#[bench]
fn throughput_fixedsize_large_payload(b: &mut test::Bencher) {
bench_server!(b, ("content-length", "1000000"), || body(
&[b'x'; 1_000_000]
))
}
// #[bench]
// fn throughput_fixedsize_large_payload(b: &mut test::Bencher) {
// bench_server!(b, ("content-length", "1000000"), || body(
// &[b'x'; 1_000_000]
// ))
// }
#[bench]
fn throughput_fixedsize_many_chunks(b: &mut test::Bencher) {
bench_server!(b, ("content-length", "1000000"), || {
static S: &[&[u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
StreamBody::new(stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)))
})
}
// #[bench]
// fn throughput_fixedsize_many_chunks(b: &mut test::Bencher) {
// bench_server!(b, ("content-length", "1000000"), || {
// static S: &[&[u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
// StreamBody::new(stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)))
// })
// }
#[bench]
fn throughput_chunked_small_payload(b: &mut test::Bencher) {
bench_server!(b, ("transfer-encoding", "chunked"), || body(
b"Hello, World!"
))
}
// #[bench]
// fn throughput_chunked_small_payload(b: &mut test::Bencher) {
// bench_server!(b, ("transfer-encoding", "chunked"), || body(
// b"Hello, World!"
// ))
// }
#[bench]
fn throughput_chunked_large_payload(b: &mut test::Bencher) {
bench_server!(b, ("transfer-encoding", "chunked"), || body(
&[b'x'; 1_000_000]
))
}
// #[bench]
// fn throughput_chunked_large_payload(b: &mut test::Bencher) {
// bench_server!(b, ("transfer-encoding", "chunked"), || body(
// &[b'x'; 1_000_000]
// ))
// }
#[bench]
fn throughput_chunked_many_chunks(b: &mut test::Bencher) {
bench_server!(b, ("transfer-encoding", "chunked"), || {
static S: &[&[u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
StreamBody::new(stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)))
})
}
// #[bench]
// fn throughput_chunked_many_chunks(b: &mut test::Bencher) {
// bench_server!(b, ("transfer-encoding", "chunked"), || {
// static S: &[&[u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
// StreamBody::new(stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)))
// })
// }
#[bench]
fn raw_tcp_throughput_small_payload(b: &mut test::Bencher) {