Skip to content

Commit e01f9d2

Browse files
committed
Add support for testing all examples + refactor impact & explain
1 parent edade8a commit e01f9d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3031
-881
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ wrangler.toml
1111
worker/generated
1212
Cargo.lock
1313
redirectionio.h
14+
/tests/test_examples/*.out.current.json

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ crate-type = ["staticlib", "cdylib", "rlib"]
1414

1515
[build-dependencies]
1616
cbindgen="0.24.3"
17+
glob = "^0.3"
1718
libtool = "0.1.1"
1819
linked_hash_set = { version = "0.1.4", features = ["serde"] }
1920
serde = { version = "1.0", features = ["derive"] }
@@ -30,6 +31,7 @@ heck = "0.4.0"
3031
http = "0.2.8"
3132
lazy_static = "1.4.0"
3233
libc = "0.2.137"
34+
linked_hash_set = "0.1.4"
3335
log = "0.4.17"
3436
percent-encoding = "2.2.0"
3537
rand = "0.8.5"

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ make
2020

2121
You can run `make install` (with root permissions) to install this library into your system, this is required if you need to
2222
compile some of our modules against this library.
23+
24+
## Tests
25+
26+
Some tests are generated. Templates are located in `tests/templates` folder. If
27+
you update them, you need to run `cargo build` to (re)generate the tests.

benches/match_rule_benchmark.rs

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[macro_use]
22
extern crate criterion;
33
use criterion::{BenchmarkId, Criterion};
4-
use redirectionio::action::Action;
4+
use redirectionio::action::{Action, UnitTrace};
55
use redirectionio::api::{Rule, RulesMessage};
66
use redirectionio::http::Request;
77
use redirectionio::router::{Router, RouterConfig};
@@ -125,41 +125,97 @@ fn build_action_rule_in_200k(c: &mut Criterion) {
125125
let rules = router.match_request(&request);
126126
let mut action = Action::from_routes_rule(rules.clone(), &request);
127127

128-
let action_status_code = action.get_status_code(0);
128+
let action_status_code = action.get_status_code(0, None);
129129
let (_, backend_status_code) = if action_status_code != 0 {
130130
(action_status_code, action_status_code)
131131
} else {
132132
// We call the backend and get a response code
133-
let final_status_code = action.get_status_code(200);
133+
let final_status_code = action.get_status_code(200, None);
134134
(final_status_code, 200)
135135
};
136136

137-
action.filter_headers(Vec::new(), backend_status_code, false);
137+
action.filter_headers(Vec::new(), backend_status_code, false, None);
138138

139139
let body = "<!DOCTYPE html>
140140
<html>
141141
<head>
142142
</head>
143143
<body>
144144
</body>
145-
</html>"
146-
.to_string();
145+
</html>";
147146

148-
if let Some(mut body_filter) = action.create_filter_body(backend_status_code) {
149-
body_filter.filter(body);
150-
body_filter.end();
147+
if let Some(mut body_filter) = action.create_filter_body(backend_status_code, &[]) {
148+
body_filter.filter(body.into(), None);
149+
body_filter.end(None);
151150
}
152151
});
153152
});
154153

155154
group.finish();
156155
}
157156

157+
fn impact(c: &mut Criterion) {
158+
let config = RouterConfig::default();
159+
let mut router = create_router("../bench-files/large-rules-200k.json".to_string(), &config);
160+
let request = Request::from_config(
161+
&config,
162+
"/sites/default/files/image-gallery/lowtideonuseppaimage000000edited_0.jpg".to_string(),
163+
Some("usharbors.com".to_string()),
164+
None,
165+
None,
166+
None,
167+
None,
168+
);
169+
170+
router.cache(1000);
171+
172+
let mut unit_trace = UnitTrace::default();
173+
174+
let mut group = c.benchmark_group("impact");
175+
group.sample_size(10);
176+
177+
group.bench_function("impact", |b| {
178+
b.iter(|| {
179+
let rules = router.match_request(&request);
180+
let mut action = Action::from_routes_rule(rules.clone(), &request);
181+
182+
let action_status_code = action.get_status_code(0, Some(&mut unit_trace));
183+
let (_, backend_status_code) = if action_status_code != 0 {
184+
(action_status_code, action_status_code)
185+
} else {
186+
// We call the backend and get a response code
187+
let final_status_code = action.get_status_code(200, Some(&mut unit_trace));
188+
(final_status_code, 200)
189+
};
190+
191+
action.filter_headers(Vec::new(), backend_status_code, false, Some(&mut unit_trace));
192+
193+
let body = "<!DOCTYPE html>
194+
<html>
195+
<head>
196+
</head>
197+
<body>
198+
</body>
199+
</html>";
200+
201+
if let Some(mut body_filter) = action.create_filter_body(backend_status_code, &[]) {
202+
body_filter.filter(body.into(), Some(&mut unit_trace));
203+
body_filter.end(Some(&mut unit_trace));
204+
}
205+
206+
unit_trace.squash_with_target_unit_traces();
207+
});
208+
});
209+
210+
group.finish();
211+
}
212+
158213
criterion_group!(
159214
benches,
160215
no_match_bench,
161216
no_match_cache_bench,
162217
match_rule_in_200k,
163-
build_action_rule_in_200k
218+
build_action_rule_in_200k,
219+
impact,
164220
);
165221
criterion_main!(benches);

src/action/ffi.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub unsafe extern "C" fn redirectionio_action_get_status_code(_action: *mut Acti
6767

6868
let action = &mut *_action;
6969

70-
action.get_status_code(response_status_code)
70+
action.get_status_code(response_status_code, None)
7171
}
7272

7373
#[no_mangle]
@@ -84,7 +84,7 @@ pub unsafe extern "C" fn redirectionio_action_header_filter_filter(
8484
let action = &mut *_action;
8585
let mut headers = header_map_to_http_headers(header_map);
8686

87-
headers = action.filter_headers(headers, response_status_code, add_rule_ids_header);
87+
headers = action.filter_headers(headers, response_status_code, add_rule_ids_header, None);
8888

8989
http_headers_to_header_map(headers)
9090
}
@@ -117,7 +117,7 @@ pub unsafe extern "C" fn redirectionio_action_body_filter_filter(_filter: *mut F
117117
let filter = &mut *_filter;
118118
let bytes = buffer.into_vec();
119119

120-
let new_body = filter.filter(bytes);
120+
let new_body = filter.filter(bytes, None);
121121

122122
Buffer::from_vec(new_body)
123123
}
@@ -129,7 +129,7 @@ pub unsafe extern "C" fn redirectionio_action_body_filter_close(_filter: *mut Fi
129129
}
130130

131131
let mut filter = Box::from_raw(_filter);
132-
let end_body = filter.end();
132+
let end_body = filter.end(None);
133133

134134
Buffer::from_vec(end_body)
135135
}

0 commit comments

Comments
 (0)