Use 'bencher' for benchmarks on stable.

This commit is contained in:
Sergio Benitez 2020-07-21 12:06:06 -07:00
parent 9a2149b43d
commit cd7e99a535
4 changed files with 166 additions and 168 deletions

View File

@ -53,5 +53,18 @@ yansi = "0.5"
version_check = "0.9.1" version_check = "0.9.1"
[dev-dependencies] [dev-dependencies]
bencher = "0.1"
# TODO: Find a way to not depend on this. # TODO: Find a way to not depend on this.
lazy_static = "1.0" lazy_static = "1.0"
[[bench]]
name = "format-routing"
harness = false
[[bench]]
name = "ranked-routing"
harness = false
[[bench]]
name = "simple-routing"
harness = false

View File

@ -1,6 +1,7 @@
#![cfg_attr(test, feature(test))]
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
#[macro_use] extern crate bencher;
use rocket::local::blocking::Client;
use rocket::config::{Environment, Config, LoggingLevel}; use rocket::config::{Environment, Config, LoggingLevel};
#[get("/", format = "application/json")] #[get("/", format = "application/json")]
@ -14,44 +15,38 @@ fn rocket() -> rocket::Rocket {
rocket::custom(config.unwrap()).mount("/", routes![get, post]) rocket::custom(config.unwrap()).mount("/", routes![get, post])
} }
#[allow(unused_must_use)] use bencher::Bencher;
mod benches { use rocket::http::{Accept, ContentType};
extern crate test;
use super::rocket; fn accept_format(b: &mut Bencher) {
use self::test::Bencher; let client = Client::new(rocket()).unwrap();
use rocket::local::blocking::Client; let request = client.get("/").header(Accept::JSON);
use rocket::http::{Accept, ContentType}; b.iter(|| { request.clone().dispatch(); });
}
fn client(_rocket: rocket::Rocket) -> Option<Client> {
unimplemented!("waiting for sync-client"); fn wrong_accept_format(b: &mut Bencher) {
} let client = Client::new(rocket()).unwrap();
let request = client.get("/").header(Accept::HTML);
#[bench] b.iter(|| { request.clone().dispatch(); });
fn accept_format(b: &mut Bencher) { }
let client = client(rocket()).unwrap();
let request = client.get("/").header(Accept::JSON); fn content_type_format(b: &mut Bencher) {
b.iter(|| { request.clone().dispatch(); }); let client = Client::new(rocket()).unwrap();
} let request = client.post("/").header(ContentType::JSON);
b.iter(|| { request.clone().dispatch(); });
#[bench] }
fn wrong_accept_format(b: &mut Bencher) {
let client = client(rocket()).unwrap(); fn wrong_content_type_format(b: &mut Bencher) {
let request = client.get("/").header(Accept::HTML); let client = Client::new(rocket()).unwrap();
b.iter(|| { request.clone().dispatch(); }); let request = client.post("/").header(ContentType::Plain);
} b.iter(|| { request.clone().dispatch(); });
}
#[bench]
fn content_type_format(b: &mut Bencher) { benchmark_main!(benches);
let client = client(rocket()).unwrap(); benchmark_group! {
let request = client.post("/").header(ContentType::JSON); benches,
b.iter(|| { request.clone().dispatch(); }); accept_format,
} wrong_accept_format,
content_type_format,
#[bench] wrong_content_type_format,
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(); });
}
} }

View File

@ -1,5 +1,5 @@
#![cfg_attr(test, feature(test))]
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
#[macro_use] extern crate bencher;
use rocket::config::{Environment, Config, LoggingLevel}; use rocket::config::{Environment, Config, LoggingLevel};
@ -28,48 +28,43 @@ fn rocket() -> rocket::Rocket {
.mount("/", routes![post, post2, post3]) .mount("/", routes![post, post2, post3])
} }
#[allow(unused_must_use)] use bencher::Bencher;
mod benches { use rocket::local::blocking::Client;
extern crate test; use rocket::http::{Accept, ContentType};
use super::rocket; fn accept_format(b: &mut Bencher) {
use self::test::Bencher; let client = Client::new(rocket()).unwrap();
use rocket::local::blocking::Client; let requests = vec![
use rocket::http::{Accept, ContentType}; client.get("/").header(Accept::JSON),
client.get("/").header(Accept::HTML),
client.get("/").header(Accept::Plain),
];
fn client(_rocket: rocket::Rocket) -> Option<Client> { b.iter(|| {
unimplemented!("waiting for sync-client"); for request in &requests {
} request.clone().dispatch();
}
#[bench] });
fn accept_format(b: &mut Bencher) { }
let client = client(rocket()).unwrap();
let requests = vec![ fn content_type_format(b: &mut Bencher) {
client.get("/").header(Accept::JSON), let client = Client::new(rocket()).unwrap();
client.get("/").header(Accept::HTML), let requests = vec![
client.get("/").header(Accept::Plain), 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();
} }
});
#[bench] }
fn content_type_format(b: &mut Bencher) {
let client = client(rocket()).unwrap(); benchmark_main!(benches);
let requests = vec![ benchmark_group! {
client.post("/").header(ContentType::JSON), benches,
client.post("/").header(ContentType::HTML), accept_format,
client.post("/").header(ContentType::Plain), content_type_format,
];
b.iter(|| {
for request in &requests {
request.clone().dispatch();
}
});
}
} }

View File

@ -1,5 +1,5 @@
#![cfg_attr(test, feature(test))]
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
#[macro_use] extern crate bencher;
use rocket::config::{Environment, Config, LoggingLevel}; use rocket::config::{Environment, Config, LoggingLevel};
use rocket::http::RawStr; use rocket::http::RawStr;
@ -40,90 +40,85 @@ fn rocket() -> rocket::Rocket {
index_b, index_c, index_dyn_a]) index_b, index_c, index_dyn_a])
} }
#[allow(unused_must_use)] use bencher::Bencher;
mod benches { use rocket::local::blocking::Client;
extern crate test;
use super::{hello_world_rocket, rocket}; fn bench_hello_world(b: &mut Bencher) {
use self::test::Bencher; let client = Client::new(hello_world_rocket()).unwrap();
use rocket::local::blocking::Client;
fn client(_rocket: rocket::Rocket) -> Option<Client> { b.iter(|| {
unimplemented!("waiting for sync-client"); client.get("/").dispatch();
} });
}
#[bench]
fn bench_hello_world(b: &mut Bencher) { fn bench_single_get_index(b: &mut Bencher) {
let client = client(hello_world_rocket()).unwrap(); let client = Client::new(rocket()).unwrap();
b.iter(|| { b.iter(|| {
client.get("/").dispatch(); client.get("/").dispatch();
}); });
} }
#[bench] fn bench_get_put_post_index(b: &mut Bencher) {
fn bench_single_get_index(b: &mut Bencher) { let client = Client::new(rocket()).unwrap();
let client = client(rocket()).unwrap();
// Hold all of the requests we're going to make during the benchmark.
b.iter(|| { let mut requests = vec![];
client.get("/").dispatch(); requests.push(client.get("/"));
}); requests.push(client.put("/"));
} requests.push(client.post("/"));
#[bench] b.iter(|| {
fn bench_get_put_post_index(b: &mut Bencher) { for request in &requests {
let client = client(rocket()).unwrap(); request.clone().dispatch();
}
// 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("/")); fn bench_dynamic(b: &mut Bencher) {
requests.push(client.post("/")); let client = Client::new(rocket()).unwrap();
b.iter(|| { // Hold all of the requests we're going to make during the benchmark.
for request in &requests { let mut requests = vec![];
request.clone().dispatch(); requests.push(client.get("/abc"));
} requests.push(client.get("/abcdefg"));
}); requests.push(client.get("/123"));
}
b.iter(|| {
#[bench] for request in &requests {
fn bench_dynamic(b: &mut Bencher) { request.clone().dispatch();
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")); fn bench_simple_routing(b: &mut Bencher) {
requests.push(client.get("/abcdefg")); let client = Client::new(rocket()).unwrap();
requests.push(client.get("/123"));
// Hold all of the requests we're going to make during the benchmark.
b.iter(|| { let mut requests = vec![];
for request in &requests { for route in client.cargo().routes() {
request.clone().dispatch(); let request = client.req(route.method, route.uri.path());
} requests.push(request);
}); }
}
// A few more for the dynamic route.
#[bench] requests.push(client.get("/abc"));
fn bench_simple_routing(b: &mut Bencher) { requests.push(client.get("/abcdefg"));
let client = client(rocket()).unwrap(); requests.push(client.get("/123"));
// Hold all of the requests we're going to make during the benchmark. b.iter(|| {
let mut requests = vec![]; for request in &requests {
for route in client.cargo().routes() { request.clone().dispatch();
let request = client.req(route.method, route.uri.path()); }
requests.push(request); });
} }
// A few more for the dynamic route. benchmark_main!(benches);
requests.push(client.get("/abc")); benchmark_group! {
requests.push(client.get("/abcdefg")); benches,
requests.push(client.get("/123")); bench_hello_world,
bench_single_get_index,
b.iter(|| { bench_get_put_post_index,
for request in &requests { bench_dynamic,
request.clone().dispatch(); bench_simple_routing,
}
});
}
} }