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

68 lines
1.7 KiB
Rust
Raw Normal View History

#[macro_use] extern crate rocket;
#[macro_use] extern crate bencher;
2019-07-18 03:38:24 +00:00
#[get("/", format = "application/json", rank = 1)]
fn get() -> &'static str { "json" }
2019-07-18 03:38:24 +00:00
#[get("/", format = "text/html", rank = 2)]
fn get2() -> &'static str { "html" }
2019-07-18 03:38:24 +00:00
#[get("/", format = "text/plain", rank = 3)]
fn get3() -> &'static str { "plain" }
#[post("/", format = "application/json")]
fn post() -> &'static str { "json" }
#[post("/", format = "text/html")]
fn post2() -> &'static str { "html" }
#[post("/", format = "text/plain")]
fn post3() -> &'static str { "plain" }
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
rocket::custom(rocket::Config::figment().merge(("log_level", "off")))
.mount("/", routes![get, get2, get3])
.mount("/", routes![post, post2, post3])
}
use bencher::Bencher;
use rocket::local::blocking::Client;
use rocket::http::{Accept, ContentType};
fn accept_format(b: &mut Bencher) {
let client = Client::tracked(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();
}
});
}
fn content_type_format(b: &mut Bencher) {
let client = Client::tracked(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,
}