style(lib): run rustfmt and enforce in CI
This commit is contained in:
@@ -11,26 +11,31 @@ use std::time::Duration;
|
||||
use futures_util::{stream, StreamExt};
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
use hyper::{Body, Response, Server};
|
||||
use hyper::service::{make_service_fn, service_fn};
|
||||
use hyper::{Body, Response, Server};
|
||||
|
||||
macro_rules! bench_server {
|
||||
($b:ident, $header:expr, $body:expr) => ({
|
||||
($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 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 mut rt = tokio::runtime::Builder::new()
|
||||
@@ -39,17 +44,13 @@ macro_rules! bench_server {
|
||||
.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();
|
||||
|
||||
let graceful = srv
|
||||
.with_graceful_shutdown(async {
|
||||
until_rx.await.ok();
|
||||
});
|
||||
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);
|
||||
@@ -62,7 +63,8 @@ macro_rules! bench_server {
|
||||
|
||||
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();
|
||||
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()
|
||||
};
|
||||
@@ -73,14 +75,15 @@ macro_rules! bench_server {
|
||||
|
||||
$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();
|
||||
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 {
|
||||
@@ -94,7 +97,9 @@ fn throughput_fixedsize_small_payload(b: &mut test::Bencher) {
|
||||
|
||||
#[bench]
|
||||
fn throughput_fixedsize_large_payload(b: &mut test::Bencher) {
|
||||
bench_server!(b, ("content-length", "1000000"), || body(&[b'x'; 1_000_000]))
|
||||
bench_server!(b, ("content-length", "1000000"), || body(
|
||||
&[b'x'; 1_000_000]
|
||||
))
|
||||
}
|
||||
|
||||
#[bench]
|
||||
@@ -107,12 +112,16 @@ fn throughput_fixedsize_many_chunks(b: &mut test::Bencher) {
|
||||
|
||||
#[bench]
|
||||
fn throughput_chunked_small_payload(b: &mut test::Bencher) {
|
||||
bench_server!(b, ("transfer-encoding", "chunked"), || body(b"Hello, World!"))
|
||||
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_server!(b, ("transfer-encoding", "chunked"), || body(
|
||||
&[b'x'; 1_000_000]
|
||||
))
|
||||
}
|
||||
|
||||
#[bench]
|
||||
@@ -134,14 +143,17 @@ fn raw_tcp_throughput_small_payload(b: &mut test::Bencher) {
|
||||
let mut buf = [0u8; 8192];
|
||||
while rx.try_recv().is_err() {
|
||||
sock.read(&mut buf).unwrap();
|
||||
sock.write_all(b"\
|
||||
sock.write_all(
|
||||
b"\
|
||||
HTTP/1.1 200 OK\r\n\
|
||||
Content-Length: 13\r\n\
|
||||
Content-Type: text/plain; charset=utf-8\r\n\
|
||||
Date: Fri, 12 May 2017 18:21:45 GMT\r\n\
|
||||
\r\n\
|
||||
Hello, World!\
|
||||
").unwrap();
|
||||
",
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -150,7 +162,8 @@ fn raw_tcp_throughput_small_payload(b: &mut test::Bencher) {
|
||||
|
||||
b.bytes = 130 + 35;
|
||||
b.iter(|| {
|
||||
tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n").unwrap();
|
||||
tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")
|
||||
.unwrap();
|
||||
let n = tcp.read(&mut buf).unwrap();
|
||||
assert_eq!(n, 130);
|
||||
});
|
||||
@@ -191,7 +204,8 @@ fn raw_tcp_throughput_large_payload(b: &mut test::Bencher) {
|
||||
b.bytes = expect_read as u64 + 35;
|
||||
|
||||
b.iter(|| {
|
||||
tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n").unwrap();
|
||||
tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")
|
||||
.unwrap();
|
||||
let mut sum = 0;
|
||||
while sum < expect_read {
|
||||
sum += tcp.read(&mut buf).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user