Skip to content

Commit

Permalink
chore: new bencher crate
Browse files Browse the repository at this point in the history
  • Loading branch information
zavakid committed Feb 9, 2025
1 parent 08e6ef6 commit cf5d6a4
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"crates/http",
"crates/web",
"crates/bencher",
]
resolver = "2"

Expand All @@ -27,6 +28,7 @@ tracing = "0.1.40"
tracing-subscriber = "0.3.18"

tokio = {version = "1", features = ["full", "tracing"] }
tokio-util = { version = "0.7.13", features = ["codec", "io", "tracing"] }
async-trait = "0.1.83"
futures = "0.3.31"
bytes = "1.10.0"
Expand Down Expand Up @@ -58,6 +60,12 @@ matchit = "0.8.5"
mockall = "0.13.1"
criterion ="0.5"

codspeed-criterion-compat = { version = "2.6.0", default-features = false }


micro-web = { path = "crates/web" }
micro-http = { path = "crates/http" }

[patch.crates-io]
micro-http = { path = "crates/http" }

Expand Down
32 changes: 32 additions & 0 deletions crates/bencher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "bencher"
version = "0.0.0"
edition = "2021"
description = "micro-http benchmarks"
authors.workspace = true
homepage.workspace = true
license.workspace = true
readme.workspace = true
repository.workspace = true

[lib]
bench = false
test = false
doctest = false

[[bench]]
name = "decoder"
harness = false

[dependencies]
codspeed-criterion-compat = { workspace = true, default-features = false, optional = true }
criterion = { workspace = true, default-features = false }


[dev-dependencies]
micro-http.workspace = true
micro-web.workspace = true
tokio-util.workspace = true

[features]
codspeed = ["codspeed-criterion-compat"]
37 changes: 37 additions & 0 deletions crates/bencher/benches/decoder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use bencher::{TestCase, TestFile};
use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput};
use micro_http::codec::RequestDecoder;
use tokio_util::bytes::BytesMut;
use tokio_util::codec::Decoder;

static SMALL_HEADER: TestFile = TestFile::new("get_header.txt", include_str!("../resources/request/get_header.txt"));

fn create_test_cases() -> Vec<TestCase> {
vec![TestCase::normal("get_header.txt", SMALL_HEADER.clone())]
}

fn benchmark_request_decoder(criterion: &mut Criterion) {
let test_cases = create_test_cases();
let mut group = criterion.benchmark_group("request_decoder");

for case in test_cases {
group.throughput(Throughput::Bytes(case.file().content().len() as u64));
group.bench_with_input(BenchmarkId::from_parameter(case.name()), &case, |b, case| {
b.iter_batched_ref(
|| (RequestDecoder::new(), BytesMut::from(case.file().content())),
|(decoder, bytes_mut)| {
let header = decoder.decode(bytes_mut).expect("input should be valide http request header").unwrap();
black_box(header);
let body = decoder.decode(bytes_mut).expect("input should be valide http request body").unwrap();
black_box(body);
},
BatchSize::SmallInput,
);
});
}

group.finish();
}

criterion_group!(decoder, benchmark_request_decoder);
criterion_main!(decoder);
17 changes: 17 additions & 0 deletions crates/bencher/resources/request/get_header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
GET /user/123 HTTP/1.1
Host: 127.0.0.1:3000
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
sec-ch-ua: "Not A(Brand";v="8", "Chromium";v="132", "Microsoft Edge";v="132"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "macOS"
Cache-Control: max-age=0
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0
Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7
Accept-Encoding: gzip, deflate, br, zstd
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7

67 changes: 67 additions & 0 deletions crates/bencher/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#[derive(Debug, Copy, Clone)]
pub struct TestCase {
name: &'static str,
group: TestGroup,
file: TestFile,
}

impl TestCase {
pub fn new(name: &'static str, group: TestGroup, file: TestFile) -> Self {
Self { name, group, file }
}

pub fn small(name: &'static str, file: TestFile) -> Self {
Self::new(name, TestGroup::Small, file)
}

pub fn normal(name: &'static str, file: TestFile) -> Self {
Self::new(name, TestGroup::Normal, file)
}

pub fn large(name: &'static str, file: TestFile) -> Self {
Self::new(name, TestGroup::Large, file)
}

pub fn name(&self) -> &'static str {
self.name
}

pub fn group(&self) -> TestGroup {
self.group
}

pub fn file(&self) -> &TestFile {
&self.file
}

pub fn file_name(&self) -> &'static str {
self.file().file_name
}
}

#[derive(Debug, Copy, Clone)]
pub struct TestFile {
file_name: &'static str,
content: &'static str,
}

impl TestFile {
pub const fn new(file_name: &'static str, content: &'static str) -> Self {
Self { file_name, content }
}

pub fn content(&self) -> &'static str {
self.content
}

pub fn file_name(&self) -> &'static str {
self.file_name
}
}

#[derive(Clone, Copy, Debug)]
pub enum TestGroup {
Small,
Normal,
Large,
}
2 changes: 1 addition & 1 deletion crates/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tracing-subscriber.workspace = true

bytes.workspace = true
tokio.workspace = true
tokio-util = { version = "0.7.13", features = ["codec", "io", "tracing"] }
tokio-util.workspace = true
futures.workspace = true
trait-variant.workspace = true

Expand Down

0 comments on commit cf5d6a4

Please sign in to comment.