|
1 | 1 | #[macro_use] |
2 | 2 | extern crate criterion; |
3 | 3 | use criterion::{BenchmarkId, Criterion}; |
| 4 | +use redirectionio::action::Action; |
4 | 5 | use redirectionio::api::{Rule, RulesMessage}; |
5 | 6 | use redirectionio::http::Request; |
6 | 7 | use redirectionio::router::{Router, RouterConfig}; |
@@ -74,5 +75,91 @@ fn no_match_cache_bench(c: &mut Criterion) { |
74 | 75 | group.finish(); |
75 | 76 | } |
76 | 77 |
|
77 | | -criterion_group!(benches, no_match_bench, no_match_cache_bench); |
| 78 | +fn match_rule_in_200k(c: &mut Criterion) { |
| 79 | + let config = RouterConfig::default(); |
| 80 | + let mut router = create_router("../bench-files/large-rules-200k.json".to_string(), &config); |
| 81 | + let request = Request::from_config( |
| 82 | + &config, |
| 83 | + "/sites/default/files/image-gallery/lowtideonuseppaimage000000edited_0.jpg".to_string(), |
| 84 | + Some("usharbors.com".to_string()), |
| 85 | + None, |
| 86 | + None, |
| 87 | + None, |
| 88 | + None, |
| 89 | + ); |
| 90 | + |
| 91 | + router.cache(10000); |
| 92 | + |
| 93 | + let mut group = c.benchmark_group("match_rule_in_200k"); |
| 94 | + group.sample_size(10); |
| 95 | + |
| 96 | + group.bench_function("match_rule_in_200k", |b| { |
| 97 | + b.iter(|| { |
| 98 | + router.match_request(&request); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + group.finish(); |
| 103 | +} |
| 104 | + |
| 105 | +fn build_action_rule_in_200k(c: &mut Criterion) { |
| 106 | + let config = RouterConfig::default(); |
| 107 | + let mut router = create_router("../bench-files/large-rules-200k.json".to_string(), &config); |
| 108 | + let request = Request::from_config( |
| 109 | + &config, |
| 110 | + "/sites/default/files/image-gallery/lowtideonuseppaimage000000edited_0.jpg".to_string(), |
| 111 | + Some("usharbors.com".to_string()), |
| 112 | + None, |
| 113 | + None, |
| 114 | + None, |
| 115 | + None, |
| 116 | + ); |
| 117 | + |
| 118 | + router.cache(1000); |
| 119 | + |
| 120 | + let mut group = c.benchmark_group("build_action_rule_in_200k"); |
| 121 | + group.sample_size(10); |
| 122 | + |
| 123 | + group.bench_function("build_action_rule_in_200k", |b| { |
| 124 | + b.iter(|| { |
| 125 | + let rules = router.match_request(&request); |
| 126 | + let mut action = Action::from_routes_rule(rules.clone(), &request); |
| 127 | + |
| 128 | + let action_status_code = action.get_status_code(0); |
| 129 | + let (_, backend_status_code) = if action_status_code != 0 { |
| 130 | + (action_status_code, action_status_code) |
| 131 | + } else { |
| 132 | + // We call the backend and get a response code |
| 133 | + let final_status_code = action.get_status_code(200); |
| 134 | + (final_status_code, 200) |
| 135 | + }; |
| 136 | + |
| 137 | + action.filter_headers(Vec::new(), backend_status_code, false); |
| 138 | + |
| 139 | + let body = "<!DOCTYPE html> |
| 140 | +<html> |
| 141 | + <head> |
| 142 | + </head> |
| 143 | + <body> |
| 144 | + </body> |
| 145 | +</html>" |
| 146 | + .to_string(); |
| 147 | + |
| 148 | + if let Some(mut body_filter) = action.create_filter_body(backend_status_code) { |
| 149 | + body_filter.filter(body); |
| 150 | + body_filter.end(); |
| 151 | + } |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + group.finish(); |
| 156 | +} |
| 157 | + |
| 158 | +criterion_group!( |
| 159 | + benches, |
| 160 | + no_match_bench, |
| 161 | + no_match_cache_bench, |
| 162 | + match_rule_in_200k, |
| 163 | + build_action_rule_in_200k |
| 164 | +); |
78 | 165 | criterion_main!(benches); |
0 commit comments