2018-09-20 04:14:30 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
2020-07-21 19:06:06 +00:00
|
|
|
#[macro_use] extern crate bencher;
|
2017-04-05 08:19:33 +00:00
|
|
|
|
|
|
|
use rocket::http::RawStr;
|
|
|
|
|
2017-05-20 02:39:10 +00:00
|
|
|
#[get("/")]
|
|
|
|
fn hello_world() -> &'static str { "Hello, world!" }
|
|
|
|
|
2017-04-05 08:19:33 +00:00
|
|
|
#[put("/")]
|
|
|
|
fn put_index() -> &'static str { "index" }
|
|
|
|
|
|
|
|
#[post("/")]
|
|
|
|
fn post_index() -> &'static str { "index" }
|
|
|
|
|
|
|
|
#[get("/a")]
|
|
|
|
fn index_a() -> &'static str { "index" }
|
|
|
|
|
|
|
|
#[get("/b")]
|
|
|
|
fn index_b() -> &'static str { "index" }
|
|
|
|
|
|
|
|
#[get("/c")]
|
|
|
|
fn index_c() -> &'static str { "index" }
|
|
|
|
|
2019-07-18 03:38:24 +00:00
|
|
|
#[get("/<_a>")]
|
|
|
|
fn index_dyn_a(_a: &RawStr) -> &'static str { "index" }
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2017-05-20 02:39:10 +00:00
|
|
|
fn hello_world_rocket() -> rocket::Rocket {
|
2020-09-03 05:41:31 +00:00
|
|
|
let config = rocket::Config::figment().merge(("log_level", "off"));
|
|
|
|
rocket::custom(config).mount("/", routes![hello_world])
|
2017-05-20 02:39:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 08:19:33 +00:00
|
|
|
fn rocket() -> rocket::Rocket {
|
2020-09-03 05:41:31 +00:00
|
|
|
hello_world_rocket()
|
|
|
|
.mount("/", routes![
|
|
|
|
put_index, post_index, index_a, index_b, index_c, index_dyn_a
|
|
|
|
])
|
2017-04-05 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
use bencher::Bencher;
|
|
|
|
use rocket::local::blocking::Client;
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
fn bench_hello_world(b: &mut Bencher) {
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(hello_world_rocket()).unwrap();
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
b.iter(|| {
|
|
|
|
client.get("/").dispatch();
|
|
|
|
});
|
|
|
|
}
|
2020-06-19 13:01:10 +00:00
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
fn bench_single_get_index(b: &mut Bencher) {
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(rocket()).unwrap();
|
2017-05-20 02:39:10 +00:00
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
b.iter(|| {
|
|
|
|
client.get("/").dispatch();
|
|
|
|
});
|
|
|
|
}
|
2017-05-20 02:39:10 +00:00
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
fn bench_get_put_post_index(b: &mut Bencher) {
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(rocket()).unwrap();
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
// 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("/"));
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
b.iter(|| {
|
|
|
|
for request in &requests {
|
|
|
|
request.clone().dispatch();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
fn bench_dynamic(b: &mut Bencher) {
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(rocket()).unwrap();
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
// 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"));
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
b.iter(|| {
|
|
|
|
for request in &requests {
|
|
|
|
request.clone().dispatch();
|
2017-04-05 08:19:33 +00:00
|
|
|
}
|
2020-07-21 19:06:06 +00:00
|
|
|
});
|
|
|
|
}
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
fn bench_simple_routing(b: &mut Bencher) {
|
2020-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(rocket()).unwrap();
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2020-07-21 19:06:06 +00:00
|
|
|
// Hold all of the requests we're going to make during the benchmark.
|
|
|
|
let mut requests = vec![];
|
2020-10-22 10:27:04 +00:00
|
|
|
for route in client.rocket().routes() {
|
2020-07-21 19:06:06 +00:00
|
|
|
let request = client.req(route.method, route.uri.path());
|
|
|
|
requests.push(request);
|
2017-04-05 08:19:33 +00:00
|
|
|
}
|
2020-07-21 19:06:06 +00:00
|
|
|
|
|
|
|
// 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,
|
2017-04-05 08:19:33 +00:00
|
|
|
}
|