From eb2073113fa8d61081436af24814900d02ffc401 Mon Sep 17 00:00:00 2001 From: Vic Demuzere Date: Thu, 21 Oct 2021 21:07:16 +0200 Subject: [PATCH 1/2] Add benchmark for nested routers --- Cargo.toml | 4 ++++ benches/nest.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 benches/nest.rs diff --git a/Cargo.toml b/Cargo.toml index f523b414d..8f5d6f19c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -79,6 +79,10 @@ required-features = ["sessions"] name = "router" harness = false +[[bench]] +name = "nest" +harness = false + [[example]] name = "cookies" required-features = ["cookies"] diff --git a/benches/nest.rs b/benches/nest.rs new file mode 100644 index 000000000..b98de3710 --- /dev/null +++ b/benches/nest.rs @@ -0,0 +1,33 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use http_types::{Method, Request, Response, Url}; + +fn criterion_benchmark(c: &mut Criterion) { + let mut app = tide::new(); + app.at("/x").get(|_| async { Ok("X") }); + app.at("/x/y").get(|_| async { Ok("Y") }); + app.at("/x/y/z").get(|_| async { Ok("Z") }); + + let route = Url::parse("https://example.com/x/y/z").unwrap(); + let req = Request::new(Method::Get, route); + c.bench_function("plain", |b| { + b.iter(|| black_box(app.respond::<_, Response>(req.clone()))); + }); + + let mut appz = tide::new(); + appz.at("/z").get(|_| async { Ok("Z") }); + + let mut appy = tide::new(); + appy.at("/y").nest(appz); + + let mut appx = tide::new(); + appx.at("/x").nest(appy); + + let route = Url::parse("https://example.com/x/y/z").unwrap(); + let req = Request::new(Method::Get, route); + c.bench_function("nested", |b| { + b.iter(|| black_box(appx.respond::<_, Response>(req.clone()))); + }); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); From b5a7dc03895b92a28376e209d0f9ab4d7e543990 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Mon, 25 Oct 2021 17:33:59 +0200 Subject: [PATCH 2/2] fix formatting on nest --- benches/nest.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benches/nest.rs b/benches/nest.rs index b98de3710..9cbe16dd8 100644 --- a/benches/nest.rs +++ b/benches/nest.rs @@ -15,7 +15,7 @@ fn criterion_benchmark(c: &mut Criterion) { let mut appz = tide::new(); appz.at("/z").get(|_| async { Ok("Z") }); - + let mut appy = tide::new(); appy.at("/y").nest(appz);