Rocket/core/lib/benches/simple-routing.rs

121 lines
2.9 KiB
Rust
Raw Normal View History

#[macro_use] extern crate rocket;
#[macro_use] extern crate bencher;
use rocket::http::RawStr;
2017-05-20 02:39:10 +00:00
#[get("/")]
fn hello_world() -> &'static str { "Hello, world!" }
#[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-05-20 02:39:10 +00:00
fn hello_world_rocket() -> rocket::Rocket {
Revamp configuration. This commit completely overhauls Rocket's configuration systems, basing it on the new Figment library. It includes many breaking changes pertaining to configuration. They are: * "Environments" are replaced by "profiles". * 'ROCKET_PROFILE' takes the place of 'ROCKET_ENV'. * Profile names are now arbitrary, but 'debug' and 'release' are given special treatment as default profiles for the debug and release compilation profiles. * A 'default' profile now sits along-side the meta 'global' profile. * The concept of "extras" is no longer present; users can extract any values they want from the configured 'Figment'. * The 'Poolable' trait takes an '&Config'. * The 'secrets' feature is disabled by default. * It is a hard error if 'secrets' is enabled under the 'release' profile and no 'secret_key' is configured. * 'ConfigBuilder' no longer exists: all fields of 'Config' are public with public constructors for each type. * 'keep_alive' is disabled with '0', not 'false' or 'off'. * Inlined error variants into the 'Error' structure. * 'LoggingLevel' is now 'LogLevel'. * Limits can now be specified in SI units: "1 MiB". The summary of other changes are: * The default config file can be configured with 'ROCKET_CONFIG'. * HTTP/1 and HTTP/2 keep-alive configuration is restored. * 'ctrlc' is now a recognized config option. * 'serde' is now a core dependency. * TLS misconfiguration errors are improved. * Several example use '_' as the return type of '#[launch]' fns. * 'AdHoc::config()' was added for simple config extraction. * Added more documentation for using 'Limits'. * Launch information is no longer treated specially. * The configuration guide was rewritten. Resolves #852. Resolves #209. Closes #1404. Closes #652.
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
}
fn rocket() -> rocket::Rocket {
Revamp configuration. This commit completely overhauls Rocket's configuration systems, basing it on the new Figment library. It includes many breaking changes pertaining to configuration. They are: * "Environments" are replaced by "profiles". * 'ROCKET_PROFILE' takes the place of 'ROCKET_ENV'. * Profile names are now arbitrary, but 'debug' and 'release' are given special treatment as default profiles for the debug and release compilation profiles. * A 'default' profile now sits along-side the meta 'global' profile. * The concept of "extras" is no longer present; users can extract any values they want from the configured 'Figment'. * The 'Poolable' trait takes an '&Config'. * The 'secrets' feature is disabled by default. * It is a hard error if 'secrets' is enabled under the 'release' profile and no 'secret_key' is configured. * 'ConfigBuilder' no longer exists: all fields of 'Config' are public with public constructors for each type. * 'keep_alive' is disabled with '0', not 'false' or 'off'. * Inlined error variants into the 'Error' structure. * 'LoggingLevel' is now 'LogLevel'. * Limits can now be specified in SI units: "1 MiB". The summary of other changes are: * The default config file can be configured with 'ROCKET_CONFIG'. * HTTP/1 and HTTP/2 keep-alive configuration is restored. * 'ctrlc' is now a recognized config option. * 'serde' is now a core dependency. * TLS misconfiguration errors are improved. * Several example use '_' as the return type of '#[launch]' fns. * 'AdHoc::config()' was added for simple config extraction. * Added more documentation for using 'Limits'. * Launch information is no longer treated specially. * The configuration guide was rewritten. Resolves #852. Resolves #209. Closes #1404. Closes #652.
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
])
}
use bencher::Bencher;
use rocket::local::blocking::Client;
fn bench_hello_world(b: &mut Bencher) {
let client = Client::tracked(hello_world_rocket()).unwrap();
b.iter(|| {
client.get("/").dispatch();
});
}
fn bench_single_get_index(b: &mut Bencher) {
let client = Client::tracked(rocket()).unwrap();
2017-05-20 02:39:10 +00:00
b.iter(|| {
client.get("/").dispatch();
});
}
2017-05-20 02:39:10 +00:00
fn bench_get_put_post_index(b: &mut Bencher) {
let client = Client::tracked(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::tracked(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::tracked(rocket()).unwrap();
// Hold all of the requests we're going to make during the benchmark.
let mut requests = vec![];
for route in client.rocket().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,
}