From cd7e99a535f11f5fad732a3856578f5e4d6f7983 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Tue, 21 Jul 2020 12:06:06 -0700 Subject: [PATCH] Use 'bencher' for benchmarks on stable. --- core/lib/Cargo.toml | 13 +++ core/lib/benches/format-routing.rs | 75 ++++++------- core/lib/benches/ranked-routing.rs | 81 +++++++------- core/lib/benches/simple-routing.rs | 165 ++++++++++++++--------------- 4 files changed, 166 insertions(+), 168 deletions(-) diff --git a/core/lib/Cargo.toml b/core/lib/Cargo.toml index 65d1fa57..8579e515 100644 --- a/core/lib/Cargo.toml +++ b/core/lib/Cargo.toml @@ -53,5 +53,18 @@ yansi = "0.5" version_check = "0.9.1" [dev-dependencies] +bencher = "0.1" # TODO: Find a way to not depend on this. lazy_static = "1.0" + +[[bench]] +name = "format-routing" +harness = false + +[[bench]] +name = "ranked-routing" +harness = false + +[[bench]] +name = "simple-routing" +harness = false diff --git a/core/lib/benches/format-routing.rs b/core/lib/benches/format-routing.rs index 0770f415..75d3e80b 100644 --- a/core/lib/benches/format-routing.rs +++ b/core/lib/benches/format-routing.rs @@ -1,6 +1,7 @@ -#![cfg_attr(test, feature(test))] #[macro_use] extern crate rocket; +#[macro_use] extern crate bencher; +use rocket::local::blocking::Client; use rocket::config::{Environment, Config, LoggingLevel}; #[get("/", format = "application/json")] @@ -14,44 +15,38 @@ fn rocket() -> rocket::Rocket { rocket::custom(config.unwrap()).mount("/", routes![get, post]) } -#[allow(unused_must_use)] -mod benches { - extern crate test; +use bencher::Bencher; +use rocket::http::{Accept, ContentType}; - use super::rocket; - use self::test::Bencher; - use rocket::local::blocking::Client; - use rocket::http::{Accept, ContentType}; - - fn client(_rocket: rocket::Rocket) -> Option { - unimplemented!("waiting for sync-client"); - } - - #[bench] - fn accept_format(b: &mut Bencher) { - let client = client(rocket()).unwrap(); - let request = client.get("/").header(Accept::JSON); - b.iter(|| { request.clone().dispatch(); }); - } - - #[bench] - fn wrong_accept_format(b: &mut Bencher) { - let client = client(rocket()).unwrap(); - let request = client.get("/").header(Accept::HTML); - b.iter(|| { request.clone().dispatch(); }); - } - - #[bench] - fn content_type_format(b: &mut Bencher) { - let client = client(rocket()).unwrap(); - let request = client.post("/").header(ContentType::JSON); - b.iter(|| { request.clone().dispatch(); }); - } - - #[bench] - fn wrong_content_type_format(b: &mut Bencher) { - let client = client(rocket()).unwrap(); - let request = client.post("/").header(ContentType::Plain); - b.iter(|| { request.clone().dispatch(); }); - } +fn accept_format(b: &mut Bencher) { + let client = Client::new(rocket()).unwrap(); + let request = client.get("/").header(Accept::JSON); + b.iter(|| { request.clone().dispatch(); }); +} + +fn wrong_accept_format(b: &mut Bencher) { + let client = Client::new(rocket()).unwrap(); + let request = client.get("/").header(Accept::HTML); + b.iter(|| { request.clone().dispatch(); }); +} + +fn content_type_format(b: &mut Bencher) { + let client = Client::new(rocket()).unwrap(); + let request = client.post("/").header(ContentType::JSON); + b.iter(|| { request.clone().dispatch(); }); +} + +fn wrong_content_type_format(b: &mut Bencher) { + let client = Client::new(rocket()).unwrap(); + let request = client.post("/").header(ContentType::Plain); + b.iter(|| { request.clone().dispatch(); }); +} + +benchmark_main!(benches); +benchmark_group! { + benches, + accept_format, + wrong_accept_format, + content_type_format, + wrong_content_type_format, } diff --git a/core/lib/benches/ranked-routing.rs b/core/lib/benches/ranked-routing.rs index 62d23bb0..f17fba7b 100644 --- a/core/lib/benches/ranked-routing.rs +++ b/core/lib/benches/ranked-routing.rs @@ -1,5 +1,5 @@ -#![cfg_attr(test, feature(test))] #[macro_use] extern crate rocket; +#[macro_use] extern crate bencher; use rocket::config::{Environment, Config, LoggingLevel}; @@ -28,48 +28,43 @@ fn rocket() -> rocket::Rocket { .mount("/", routes![post, post2, post3]) } -#[allow(unused_must_use)] -mod benches { - extern crate test; +use bencher::Bencher; +use rocket::local::blocking::Client; +use rocket::http::{Accept, ContentType}; - use super::rocket; - use self::test::Bencher; - use rocket::local::blocking::Client; - use rocket::http::{Accept, ContentType}; +fn accept_format(b: &mut Bencher) { + let client = Client::new(rocket()).unwrap(); + let requests = vec![ + client.get("/").header(Accept::JSON), + client.get("/").header(Accept::HTML), + client.get("/").header(Accept::Plain), + ]; - fn client(_rocket: rocket::Rocket) -> Option { - unimplemented!("waiting for sync-client"); - } - - #[bench] - fn accept_format(b: &mut Bencher) { - let client = client(rocket()).unwrap(); - let requests = vec![ - client.get("/").header(Accept::JSON), - client.get("/").header(Accept::HTML), - client.get("/").header(Accept::Plain), - ]; - - b.iter(|| { - for request in &requests { - request.clone().dispatch(); - } - }); - } - - #[bench] - fn content_type_format(b: &mut Bencher) { - let client = client(rocket()).unwrap(); - let requests = vec![ - client.post("/").header(ContentType::JSON), - client.post("/").header(ContentType::HTML), - client.post("/").header(ContentType::Plain), - ]; - - b.iter(|| { - for request in &requests { - request.clone().dispatch(); - } - }); - } + b.iter(|| { + for request in &requests { + request.clone().dispatch(); + } + }); +} + +fn content_type_format(b: &mut Bencher) { + let client = Client::new(rocket()).unwrap(); + let requests = vec![ + client.post("/").header(ContentType::JSON), + client.post("/").header(ContentType::HTML), + client.post("/").header(ContentType::Plain), + ]; + + b.iter(|| { + for request in &requests { + request.clone().dispatch(); + } + }); +} + +benchmark_main!(benches); +benchmark_group! { + benches, + accept_format, + content_type_format, } diff --git a/core/lib/benches/simple-routing.rs b/core/lib/benches/simple-routing.rs index ac009162..717a8e0b 100644 --- a/core/lib/benches/simple-routing.rs +++ b/core/lib/benches/simple-routing.rs @@ -1,5 +1,5 @@ -#![cfg_attr(test, feature(test))] #[macro_use] extern crate rocket; +#[macro_use] extern crate bencher; use rocket::config::{Environment, Config, LoggingLevel}; use rocket::http::RawStr; @@ -40,90 +40,85 @@ fn rocket() -> rocket::Rocket { index_b, index_c, index_dyn_a]) } -#[allow(unused_must_use)] -mod benches { - extern crate test; +use bencher::Bencher; +use rocket::local::blocking::Client; - use super::{hello_world_rocket, rocket}; - use self::test::Bencher; - use rocket::local::blocking::Client; +fn bench_hello_world(b: &mut Bencher) { + let client = Client::new(hello_world_rocket()).unwrap(); - fn client(_rocket: rocket::Rocket) -> Option { - unimplemented!("waiting for sync-client"); - } - - #[bench] - fn bench_hello_world(b: &mut Bencher) { - let client = client(hello_world_rocket()).unwrap(); - - b.iter(|| { - client.get("/").dispatch(); - }); - } - - #[bench] - fn bench_single_get_index(b: &mut Bencher) { - let client = client(rocket()).unwrap(); - - b.iter(|| { - client.get("/").dispatch(); - }); - } - - #[bench] - fn bench_get_put_post_index(b: &mut Bencher) { - let client = client(rocket()).unwrap(); - - // Hold all of the requests we're going to make during the benchmark. - let mut requests = vec![]; - requests.push(client.get("/")); - requests.push(client.put("/")); - requests.push(client.post("/")); - - b.iter(|| { - for request in &requests { - request.clone().dispatch(); - } - }); - } - - #[bench] - fn bench_dynamic(b: &mut Bencher) { - let client = client(rocket()).unwrap(); - - // Hold all of the requests we're going to make during the benchmark. - let mut requests = vec![]; - requests.push(client.get("/abc")); - requests.push(client.get("/abcdefg")); - requests.push(client.get("/123")); - - b.iter(|| { - for request in &requests { - request.clone().dispatch(); - } - }); - } - - #[bench] - fn bench_simple_routing(b: &mut Bencher) { - let client = client(rocket()).unwrap(); - - // Hold all of the requests we're going to make during the benchmark. - let mut requests = vec![]; - for route in client.cargo().routes() { - let request = client.req(route.method, route.uri.path()); - requests.push(request); - } - - // A few more for the dynamic route. - requests.push(client.get("/abc")); - requests.push(client.get("/abcdefg")); - requests.push(client.get("/123")); - - b.iter(|| { - for request in &requests { - request.clone().dispatch(); - } - }); - } + b.iter(|| { + client.get("/").dispatch(); + }); +} + +fn bench_single_get_index(b: &mut Bencher) { + let client = Client::new(rocket()).unwrap(); + + b.iter(|| { + client.get("/").dispatch(); + }); +} + +fn bench_get_put_post_index(b: &mut Bencher) { + let client = Client::new(rocket()).unwrap(); + + // Hold all of the requests we're going to make during the benchmark. + let mut requests = vec![]; + requests.push(client.get("/")); + requests.push(client.put("/")); + requests.push(client.post("/")); + + b.iter(|| { + for request in &requests { + request.clone().dispatch(); + } + }); +} + +fn bench_dynamic(b: &mut Bencher) { + let client = Client::new(rocket()).unwrap(); + + // Hold all of the requests we're going to make during the benchmark. + let mut requests = vec![]; + requests.push(client.get("/abc")); + requests.push(client.get("/abcdefg")); + requests.push(client.get("/123")); + + b.iter(|| { + for request in &requests { + request.clone().dispatch(); + } + }); +} + +fn bench_simple_routing(b: &mut Bencher) { + let client = Client::new(rocket()).unwrap(); + + // Hold all of the requests we're going to make during the benchmark. + let mut requests = vec![]; + for route in client.cargo().routes() { + let request = client.req(route.method, route.uri.path()); + requests.push(request); + } + + // A few more for the dynamic route. + requests.push(client.get("/abc")); + requests.push(client.get("/abcdefg")); + requests.push(client.get("/123")); + + b.iter(|| { + for request in &requests { + request.clone().dispatch(); + } + }); +} + +benchmark_main!(benches); +benchmark_group! { + benches, + bench_hello_world, + bench_single_get_index, + bench_get_put_post_index, + bench_dynamic, + bench_simple_routing, }