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"
[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

View File

@ -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<Client> {
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,
}

View File

@ -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<Client> {
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,
}

View File

@ -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<Client> {
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,
}