Rocket/core/lib/benches/simple-routing.rs
Sergio Benitez 61f107f550 Reimplement route attribute as a proc-macro.
This commits also implement the query reform from #608. It also consists
of many, many breaking changes. Among them are:

  * Query parts in route paths use new query reform syntax.
  * Routing for queries is now lenient.
    - Default ranking has changed to reflect query reform.
  * Format routing matching has been fixed.
    - Routes with formats matching "accept" will always collide.
    - Routes with formats matching "content-type" require requests to
      have an equivalent content-type header to match.
    - Requests with imprecise content-types are treated as not having a
      content-type.
  * Generated routes and catchers respect visibility modifiers.
  * Raw getter methods from request were renamed and retooled.
    - In particular, the index parameter is based on segments in the
      route path, not dynamic parameters.
  * The method-based attributes no longer accept a keyed 'path'.
  * The 'rocket_codegen' crate is gone and will no longer be public.
  * The 'FormItems' iterator emits values of type 'FormItem'.
    - The internal form items' string can no longer be retrieved.
  * In general, routes are more strictly validated.
  * Logging from codegen now funnels through logging infrastructure.
  * Routing has been optimized by caching routing metadata.

Resolves #93.
Resolves #608.
Resolves #693.
Resolves #476.
2018-10-09 04:18:04 -07:00

130 lines
3.4 KiB
Rust

#![feature(proc_macro_non_items, proc_macro_gen, decl_macro)]
// #![feature(alloc_system)]
// extern crate alloc_system;
#[macro_use] extern crate rocket;
use rocket::config::{Environment, Config, LoggingLevel};
use rocket::http::RawStr;
#[get("/")]
fn hello_world() -> &'static str { "Hello, world!" }
#[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" }
#[get("/<a>")]
fn index_dyn_a(a: &RawStr) -> &'static str { "index" }
fn hello_world_rocket() -> rocket::Rocket {
let config = Config::build(Environment::Production).log_level(LoggingLevel::Off);
rocket::custom(config.unwrap()).mount("/", routes![hello_world])
}
fn rocket() -> rocket::Rocket {
let config = Config::build(Environment::Production).log_level(LoggingLevel::Off);
rocket::custom(config.unwrap())
.mount("/", routes![get_index, put_index, post_index, index_a,
index_b, index_c, index_dyn_a])
}
mod benches {
extern crate test;
use super::{hello_world_rocket, rocket};
use self::test::Bencher;
use rocket::local::Client;
#[bench]
fn bench_hello_world(b: &mut Bencher) {
let client = Client::new(hello_world_rocket()).unwrap();
let mut request = client.get("/");
b.iter(|| {
request.mut_dispatch();
});
}
#[bench]
fn bench_single_get_index(b: &mut Bencher) {
let client = Client::new(rocket()).unwrap();
let mut request = client.get("/");
b.iter(|| {
request.mut_dispatch();
});
}
#[bench]
fn bench_get_put_post_index(b: &mut Bencher) {
let client = Client::new(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.iter_mut() {
request.mut_dispatch();
}
});
}
#[bench]
fn bench_dynamic(b: &mut Bencher) {
let client = Client::new(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.iter_mut() {
request.mut_dispatch();
}
});
}
#[bench]
fn bench_simple_routing(b: &mut Bencher) {
let client = Client::new(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.iter_mut() {
request.mut_dispatch();
}
});
}
}