Skip to content

Commit

Permalink
Merge pull request #81 from silent-rs/features/benchmark
Browse files Browse the repository at this point in the history
feat: route benchmark
  • Loading branch information
hubertshelley authored Jan 23, 2025
2 parents 7e57a6d + 1d34870 commit 2782697
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["silent", "examples/*", ]
members = ["silent", "examples/*", "benchmark"]
default-members = ["silent", ]
resolver = "2"

Expand Down
15 changes: 15 additions & 0 deletions benchmark/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "benchmark"
version = "0.1.0"
edition = "2021"

[dependencies]
silent = { path = "../silent" }

[dev-dependencies]
criterion = "0.5"

[[bench]]
name = "route_benchmark"
path = "benches/route_benchmark.rs"
harness = false
126 changes: 126 additions & 0 deletions benchmark/benches/route_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
use criterion::{criterion_group, criterion_main, Criterion};
use silent::prelude::*;

fn simple_route_benchmark(c: &mut Criterion) {
let route = Route::new("").get(|_req| async { Ok("hello world") });

c.bench_function("simple route match", |b| {
b.iter(|| {
let req = Request::default();
let _ = route.call(req);
});
});
}

fn nested_route_benchmark(c: &mut Criterion) {
let route = Route::new("api/v1")
.append(Route::new("users").get(|_req| async { Ok("users") }))
.append(Route::new("posts").get(|_req| async { Ok("posts") }));

c.bench_function("nested route match", |b| {
b.iter(|| {
let mut req = Request::default();
*req.uri_mut() = "/api/v1/posts".parse().unwrap();
let _ = route.call(req);
});
});
}

fn middleware_route_benchmark(c: &mut Criterion) {
let route = Route::new("")
.hook(silent::middlewares::RequestTimeLogger)
.get(|_req| async { Ok("hello world") });

c.bench_function("route with middleware", |b| {
b.iter(|| {
let req = Request::default();
let _ = route.call(req);
});
});
}

fn complex_route_benchmark(c: &mut Criterion) {
let route = Route::new("api/v1")
.append(
Route::new("users")
.get(|_req| async { Ok("users") })
.post(|_req| async { Ok("create user") }),
)
.append(
Route::new("posts")
.get(|_req| async { Ok("posts") })
.post(|_req| async { Ok("create post") })
.append(Route::new("comments").get(|_req| async { Ok("comments") })),
);

let mut group = c.benchmark_group("Complex Routes");
group.bench_function("GET /api/v1/users", |b| {
b.iter(|| {
let mut req = Request::default();
*req.uri_mut() = "/api/v1/users".parse().unwrap();
let _ = route.call(req);
});
});
group.bench_function("POST /api/v1/posts", |b| {
b.iter(|| {
let mut req = Request::default();
*req.uri_mut() = "/api/v1/posts".parse().unwrap();
*req.method_mut() = Method::POST;
let _ = route.call(req);
});
});
group.bench_function("GET /api/v1/posts/comments", |b| {
b.iter(|| {
let mut req = Request::default();
*req.uri_mut() = "/api/v1/posts/comments".parse().unwrap();
let _ = route.call(req);
});
});
group.finish();
}

fn multiple_middleware_benchmark(c: &mut Criterion) {
let route = Route::new("")
.hook(silent::middlewares::RequestTimeLogger)
.hook(silent::middlewares::RequestTimeLogger) // Using same middleware twice for testing
.get(|_req| async { Ok("hello world") });

c.bench_function("route with multiple middleware", |b| {
b.iter(|| {
let req = Request::default();
let _ = route.call(req);
});
});
}

fn high_load_benchmark(c: &mut Criterion) {
let route = Route::new("api/v1").append(
Route::new("users")
.get(|_req| async { Ok("users") })
.post(|_req| async { Ok("create user") }),
);

let mut group = c.benchmark_group("High Load");
group.sample_size(1000);
group.bench_function("1000 sequential requests", |b| {
b.iter(|| {
for i in 0..1000 {
let mut req = Request::default();
*req.uri_mut() = format!("/api/v1/users?page={}", i).parse().unwrap();
let _ = route.call(req);
}
});
});
group.finish();
}

criterion_group!(
benches,
simple_route_benchmark,
nested_route_benchmark,
middleware_route_benchmark,
complex_route_benchmark,
multiple_middleware_benchmark,
high_load_benchmark
);
criterion_main!(benches);

0 comments on commit 2782697

Please sign in to comment.