-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters