1 #![feature(test)]
2 #![feature(proc_macro_hygiene)]
3
4 #[macro_use] extern crate rocket;
5
6 use rocket::config::{Environment, Config, LoggingLevel};
7 use rocket::http::RawStr;
8
9 #[get("/")]
hello_world() -> &'static str10 fn hello_world() -> &'static str { "Hello, world!" }
11
12 #[get("/")]
get_index() -> &'static str13 fn get_index() -> &'static str { "index" }
14
15 #[put("/")]
put_index() -> &'static str16 fn put_index() -> &'static str { "index" }
17
18 #[post("/")]
post_index() -> &'static str19 fn post_index() -> &'static str { "index" }
20
21 #[get("/a")]
index_a() -> &'static str22 fn index_a() -> &'static str { "index" }
23
24 #[get("/b")]
index_b() -> &'static str25 fn index_b() -> &'static str { "index" }
26
27 #[get("/c")]
index_c() -> &'static str28 fn index_c() -> &'static str { "index" }
29
30 #[get("/<_a>")]
index_dyn_a(_a: &RawStr) -> &'static str31 fn index_dyn_a(_a: &RawStr) -> &'static str { "index" }
32
hello_world_rocket() -> rocket::Rocket33 fn hello_world_rocket() -> rocket::Rocket {
34 let config = Config::build(Environment::Production).log_level(LoggingLevel::Off);
35 rocket::custom(config.unwrap()).mount("/", routes![hello_world])
36 }
37
rocket() -> rocket::Rocket38 fn rocket() -> rocket::Rocket {
39 let config = Config::build(Environment::Production).log_level(LoggingLevel::Off);
40 rocket::custom(config.unwrap())
41 .mount("/", routes![get_index, put_index, post_index, index_a,
42 index_b, index_c, index_dyn_a])
43 }
44
45 mod benches {
46 extern crate test;
47
48 use super::{hello_world_rocket, rocket};
49 use self::test::Bencher;
50 use rocket::local::Client;
51
52 #[bench]
bench_hello_world(b: &mut Bencher)53 fn bench_hello_world(b: &mut Bencher) {
54 let client = Client::new(hello_world_rocket()).unwrap();
55 let mut request = client.get("/");
56
57 b.iter(|| {
58 request.mut_dispatch();
59 });
60 }
61
62 #[bench]
bench_single_get_index(b: &mut Bencher)63 fn bench_single_get_index(b: &mut Bencher) {
64 let client = Client::new(rocket()).unwrap();
65 let mut request = client.get("/");
66
67 b.iter(|| {
68 request.mut_dispatch();
69 });
70 }
71
72 #[bench]
bench_get_put_post_index(b: &mut Bencher)73 fn bench_get_put_post_index(b: &mut Bencher) {
74 let client = Client::new(rocket()).unwrap();
75
76 // Hold all of the requests we're going to make during the benchmark.
77 let mut requests = vec![];
78 requests.push(client.get("/"));
79 requests.push(client.put("/"));
80 requests.push(client.post("/"));
81
82 b.iter(|| {
83 for request in requests.iter_mut() {
84 request.mut_dispatch();
85 }
86 });
87 }
88
89 #[bench]
bench_dynamic(b: &mut Bencher)90 fn bench_dynamic(b: &mut Bencher) {
91 let client = Client::new(rocket()).unwrap();
92
93 // Hold all of the requests we're going to make during the benchmark.
94 let mut requests = vec![];
95 requests.push(client.get("/abc"));
96 requests.push(client.get("/abcdefg"));
97 requests.push(client.get("/123"));
98
99 b.iter(|| {
100 for request in requests.iter_mut() {
101 request.mut_dispatch();
102 }
103 });
104 }
105
106 #[bench]
bench_simple_routing(b: &mut Bencher)107 fn bench_simple_routing(b: &mut Bencher) {
108 let client = Client::new(rocket()).unwrap();
109
110 // Hold all of the requests we're going to make during the benchmark.
111 let mut requests = vec![];
112 for route in client.rocket().routes() {
113 let request = client.req(route.method, route.uri.path());
114 requests.push(request);
115 }
116
117 // A few more for the dynamic route.
118 requests.push(client.get("/abc"));
119 requests.push(client.get("/abcdefg"));
120 requests.push(client.get("/123"));
121
122 b.iter(|| {
123 for request in requests.iter_mut() {
124 request.mut_dispatch();
125 }
126 });
127 }
128 }
129