2019-07-18 03:38:24 +00:00
|
|
|
#![feature(test)]
|
2019-06-20 06:03:21 +00:00
|
|
|
#![feature(proc_macro_hygiene)]
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2018-09-20 04:14:30 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2018-08-12 13:10:11 +00:00
|
|
|
use rocket::config::{Environment, Config, LoggingLevel};
|
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
|
|
|
#[get("/")]
|
|
|
|
fn get_index() -> &'static str { "index" }
|
|
|
|
|
|
|
|
#[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 {
|
2018-08-12 13:10:11 +00:00
|
|
|
let config = Config::build(Environment::Production).log_level(LoggingLevel::Off);
|
|
|
|
rocket::custom(config.unwrap()).mount("/", routes![hello_world])
|
2017-05-20 02:39:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 08:19:33 +00:00
|
|
|
fn rocket() -> rocket::Rocket {
|
2018-08-12 13:10:11 +00:00
|
|
|
let config = Config::build(Environment::Production).log_level(LoggingLevel::Off);
|
|
|
|
rocket::custom(config.unwrap())
|
2017-04-05 08:19:33 +00:00
|
|
|
.mount("/", routes![get_index, put_index, post_index, index_a,
|
|
|
|
index_b, index_c, index_dyn_a])
|
|
|
|
}
|
|
|
|
|
2020-06-19 13:01:10 +00:00
|
|
|
#[allow(unused_must_use)]
|
2017-04-05 08:19:33 +00:00
|
|
|
mod benches {
|
|
|
|
extern crate test;
|
|
|
|
|
2017-05-20 02:39:10 +00:00
|
|
|
use super::{hello_world_rocket, rocket};
|
2017-04-05 08:19:33 +00:00
|
|
|
use self::test::Bencher;
|
2017-06-06 20:41:04 +00:00
|
|
|
use rocket::local::Client;
|
2017-04-05 08:19:33 +00:00
|
|
|
|
2020-06-19 13:01:10 +00:00
|
|
|
fn client(_rocket: rocket::Rocket) -> Option<Client> {
|
|
|
|
unimplemented!("waiting for sync-client");
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:39:10 +00:00
|
|
|
#[bench]
|
|
|
|
fn bench_hello_world(b: &mut Bencher) {
|
2020-06-19 13:01:10 +00:00
|
|
|
let client = client(hello_world_rocket()).unwrap();
|
2017-06-06 20:41:04 +00:00
|
|
|
let mut request = client.get("/");
|
2017-05-20 02:39:10 +00:00
|
|
|
|
|
|
|
b.iter(|| {
|
2017-06-06 20:41:04 +00:00
|
|
|
request.mut_dispatch();
|
2017-05-20 02:39:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-05 08:19:33 +00:00
|
|
|
#[bench]
|
|
|
|
fn bench_single_get_index(b: &mut Bencher) {
|
2020-06-19 13:01:10 +00:00
|
|
|
let client = client(rocket()).unwrap();
|
2017-06-06 20:41:04 +00:00
|
|
|
let mut request = client.get("/");
|
2017-04-05 08:19:33 +00:00
|
|
|
|
|
|
|
b.iter(|| {
|
2017-06-06 20:41:04 +00:00
|
|
|
request.mut_dispatch();
|
2017-04-05 08:19:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_get_put_post_index(b: &mut Bencher) {
|
2020-06-19 13:01:10 +00:00
|
|
|
let client = client(rocket()).unwrap();
|
2017-04-05 08:19:33 +00:00
|
|
|
|
|
|
|
// Hold all of the requests we're going to make during the benchmark.
|
|
|
|
let mut requests = vec![];
|
2017-06-06 20:41:04 +00:00
|
|
|
requests.push(client.get("/"));
|
|
|
|
requests.push(client.put("/"));
|
|
|
|
requests.push(client.post("/"));
|
2017-04-05 08:19:33 +00:00
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
for request in requests.iter_mut() {
|
2017-06-06 20:41:04 +00:00
|
|
|
request.mut_dispatch();
|
2017-04-05 08:19:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_dynamic(b: &mut Bencher) {
|
2020-06-19 13:01:10 +00:00
|
|
|
let client = client(rocket()).unwrap();
|
2017-04-05 08:19:33 +00:00
|
|
|
|
|
|
|
// Hold all of the requests we're going to make during the benchmark.
|
|
|
|
let mut requests = vec![];
|
2017-06-06 20:41:04 +00:00
|
|
|
requests.push(client.get("/abc"));
|
|
|
|
requests.push(client.get("/abcdefg"));
|
|
|
|
requests.push(client.get("/123"));
|
2017-04-05 08:19:33 +00:00
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
for request in requests.iter_mut() {
|
2017-06-06 20:41:04 +00:00
|
|
|
request.mut_dispatch();
|
2017-04-05 08:19:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_simple_routing(b: &mut Bencher) {
|
2020-06-19 13:01:10 +00:00
|
|
|
let client = client(rocket()).unwrap();
|
2017-04-05 08:19:33 +00:00
|
|
|
|
|
|
|
// Hold all of the requests we're going to make during the benchmark.
|
|
|
|
let mut requests = vec![];
|
2020-06-28 05:59:40 +00:00
|
|
|
for route in client.cargo().routes() {
|
2017-06-17 21:18:00 +00:00
|
|
|
let request = client.req(route.method, route.uri.path());
|
2017-04-05 08:19:33 +00:00
|
|
|
requests.push(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A few more for the dynamic route.
|
2017-06-06 20:41:04 +00:00
|
|
|
requests.push(client.get("/abc"));
|
|
|
|
requests.push(client.get("/abcdefg"));
|
|
|
|
requests.push(client.get("/123"));
|
2017-04-05 08:19:33 +00:00
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
for request in requests.iter_mut() {
|
2017-06-06 20:41:04 +00:00
|
|
|
request.mut_dispatch();
|
2017-04-05 08:19:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|