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 super::rocket;
use self::test::Bencher;
use rocket::local::blocking::Client;
use bencher::Bencher;
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 client = Client::new(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 client = Client::new(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 client = Client::new(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 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,22 +28,12 @@ fn rocket() -> rocket::Rocket {
.mount("/", routes![post, post2, post3])
}
#[allow(unused_must_use)]
mod benches {
extern crate test;
use super::rocket;
use self::test::Bencher;
use bencher::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 client = Client::new(rocket()).unwrap();
let requests = vec![
client.get("/").header(Accept::JSON),
client.get("/").header(Accept::HTML),
@ -57,9 +47,8 @@ mod benches {
});
}
#[bench]
fn content_type_format(b: &mut Bencher) {
let client = client(rocket()).unwrap();
let client = Client::new(rocket()).unwrap();
let requests = vec![
client.post("/").header(ContentType::JSON),
client.post("/").header(ContentType::HTML),
@ -72,4 +61,10 @@ mod benches {
}
});
}
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,39 +40,27 @@ fn rocket() -> rocket::Rocket {
index_b, index_c, index_dyn_a])
}
#[allow(unused_must_use)]
mod benches {
extern crate test;
use super::{hello_world_rocket, rocket};
use self::test::Bencher;
use bencher::Bencher;
use rocket::local::blocking::Client;
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();
let client = Client::new(hello_world_rocket()).unwrap();
b.iter(|| {
client.get("/").dispatch();
});
}
#[bench]
fn bench_single_get_index(b: &mut Bencher) {
let client = client(rocket()).unwrap();
let client = Client::new(rocket()).unwrap();
b.iter(|| {
client.get("/").dispatch();
});
}
#[bench]
fn bench_get_put_post_index(b: &mut Bencher) {
let client = client(rocket()).unwrap();
let client = Client::new(rocket()).unwrap();
// Hold all of the requests we're going to make during the benchmark.
let mut requests = vec![];
@ -87,9 +75,8 @@ mod benches {
});
}
#[bench]
fn bench_dynamic(b: &mut Bencher) {
let client = client(rocket()).unwrap();
let client = Client::new(rocket()).unwrap();
// Hold all of the requests we're going to make during the benchmark.
let mut requests = vec![];
@ -104,9 +91,8 @@ mod benches {
});
}
#[bench]
fn bench_simple_routing(b: &mut Bencher) {
let client = client(rocket()).unwrap();
let client = Client::new(rocket()).unwrap();
// Hold all of the requests we're going to make during the benchmark.
let mut requests = vec![];
@ -126,4 +112,13 @@ mod benches {
}
});
}
benchmark_main!(benches);
benchmark_group! {
benches,
bench_hello_world,
bench_single_get_index,
bench_get_put_post_index,
bench_dynamic,
bench_simple_routing,
}