Skip to content

Commit

Permalink
style(lib): run rustfmt and enforce in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Dec 5, 2019
1 parent b0060f2 commit 0dc8968
Show file tree
Hide file tree
Showing 69 changed files with 2,991 additions and 2,508 deletions.
24 changes: 23 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,31 @@ env:
RUST_BACKTRACE: 1

jobs:
style:
name: Check Style
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt

- name: cargo fmt --check
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check


test:
name: Test ${{ matrix.rust }} on ${{ matrix.os }}

needs: [style]
strategy:
matrix:
rust:
Expand Down
1 change: 0 additions & 1 deletion .rustfmt.toml

This file was deleted.

11 changes: 6 additions & 5 deletions benches/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

extern crate test;

use http::Uri;
use hyper::client::connect::HttpConnector;
use hyper::service::Service;
use std::net::SocketAddr;
use tokio::net::TcpListener;
use hyper::client::connect::{HttpConnector};
use hyper::service::Service;
use http::Uri;

#[bench]
fn http_connector(b: &mut test::Bencher) {
Expand All @@ -17,7 +17,9 @@ fn http_connector(b: &mut test::Bencher) {
.basic_scheduler()
.build()
.expect("rt build");
let mut listener = rt.block_on(TcpListener::bind(&SocketAddr::from(([127, 0, 0, 1], 0)))).expect("bind");
let mut 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();
Expand All @@ -28,7 +30,6 @@ fn http_connector(b: &mut test::Bencher) {
}
});


b.iter(|| {
rt.block_on(async {
connector.call(dst.clone()).await.expect("connect");
Expand Down
64 changes: 26 additions & 38 deletions benches/end_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ use std::net::SocketAddr;

use futures_util::future::join_all;

use hyper::{body::HttpBody as _, Body, Method, Request, Response, Server};
use hyper::client::HttpConnector;
use hyper::{body::HttpBody as _, Body, Method, Request, Response, Server};

// HTTP1

#[bench]
fn http1_get(b: &mut test::Bencher) {
opts()
.bench(b)
opts().bench(b)
}

#[bench]
Expand Down Expand Up @@ -48,9 +47,7 @@ fn http1_body_both_10mb(b: &mut test::Bencher) {

#[bench]
fn http1_parallel_x10_empty(b: &mut test::Bencher) {
opts()
.parallel(10)
.bench(b)
opts().parallel(10).bench(b)
}

#[bench]
Expand All @@ -76,19 +73,13 @@ fn http1_parallel_x10_req_10kb_100_chunks(b: &mut test::Bencher) {
#[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)
opts().parallel(10).response_body(body).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)
opts().parallel(10).response_body(body).bench(b)
}

// HTTP2
Expand All @@ -97,9 +88,7 @@ const HTTP2_MAX_WINDOW: u32 = std::u32::MAX >> 1;

#[bench]
fn http2_get(b: &mut test::Bencher) {
opts()
.http2()
.bench(b)
opts().http2().bench(b)
}

#[bench]
Expand All @@ -123,10 +112,7 @@ fn http2_req_100kb(b: &mut test::Bencher) {

#[bench]
fn http2_parallel_x10_empty(b: &mut test::Bencher) {
opts()
.http2()
.parallel(10)
.bench(b)
opts().http2().parallel(10).bench(b)
}

#[bench]
Expand Down Expand Up @@ -293,18 +279,18 @@ impl Opts {
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");
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
self.request_body
.map(Body::from)
.unwrap_or_else(|| Body::empty())
};
Expand All @@ -328,14 +314,12 @@ impl Opts {
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)
});
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));
});
Expand All @@ -353,12 +337,16 @@ fn spawn_server(rt: &mut tokio::runtime::Runtime, opts: &Opts) -> SocketAddr {
.http2_only(opts.http2)
.http2_initial_stream_window_size(opts.http2_stream_window)
.http2_initial_connection_window_size(opts.http2_conn_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)))
}))
.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();
Expand All @@ -367,5 +355,5 @@ fn spawn_server(rt: &mut tokio::runtime::Runtime, opts: &Opts) -> SocketAddr {
panic!("server error: {}", err);
}
});
return addr
return addr;
}
25 changes: 13 additions & 12 deletions benches/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
extern crate test;

use std::io::{Read, Write};
use std::net::{TcpStream};
use std::net::TcpStream;
use std::sync::mpsc;
use std::time::Duration;

use tokio::sync::oneshot;

use hyper::{Body, Response, Server};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Response, Server};

const PIPELINED_REQUESTS: usize = 16;

Expand All @@ -25,10 +25,12 @@ fn hello_world(b: &mut test::Bencher) {
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::new(Body::from("Hello, World!")))
}))
let make_svc = make_service_fn(|_| {
async {
Ok::<_, hyper::Error>(service_fn(|_| {
async { Ok::<_, hyper::Error>(Response::new(Body::from("Hello, World!"))) }
}))
}
});

let mut rt = tokio::runtime::Builder::new()
Expand All @@ -44,10 +46,9 @@ fn hello_world(b: &mut test::Bencher) {

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 {
if let Err(e) = graceful.await {
Expand All @@ -66,7 +67,8 @@ fn hello_world(b: &mut test::Bencher) {

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()
} * PIPELINED_REQUESTS;
Expand All @@ -85,4 +87,3 @@ fn hello_world(b: &mut test::Bencher) {
assert_eq!(sum, total_bytes);
});
}

Loading

0 comments on commit 0dc8968

Please sign in to comment.