use atomic_http::TestData;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use std::hint::black_box;
fn bench_standard_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("standard_parsing");
for size in [1, 10, 100].iter() {
let data = TestData::generate(*size);
let json_bytes = serde_json::to_vec(&data).unwrap();
group.bench_with_input(BenchmarkId::new("json_parsing", size), size, |b, _| {
b.iter(|| {
let json_string = String::from_utf8(json_bytes.clone()).unwrap();
let _parsed: TestData = serde_json::from_str(&json_string).unwrap();
})
});
group.bench_with_input(BenchmarkId::new("string_conversion", size), size, |b, _| {
b.iter(|| black_box(String::from_utf8(json_bytes.clone()).unwrap()))
});
}
group.finish();
}
#[cfg(feature = "arena")]
fn bench_arena_parsing(c: &mut Criterion) {
use bumpalo::Bump;
let mut group = c.benchmark_group("arena_parsing");
for size in [1, 10, 100].iter() {
let data = TestData::generate(*size);
let json_bytes = serde_json::to_vec(&data).unwrap();
group.bench_with_input(
BenchmarkId::new("arena_json_parsing", size),
size,
|b, _| {
b.iter(|| {
let bump = Bump::new();
let allocated_data = bump.alloc_slice_copy(&json_bytes);
let _parsed: TestData = serde_json::from_slice(allocated_data).unwrap();
})
},
);
group.bench_with_input(BenchmarkId::new("arena_allocation", size), size, |b, _| {
b.iter(|| {
let bump = Bump::new();
black_box(bump.alloc_slice_copy(&json_bytes));
})
});
}
group.finish();
}
fn bench_memory_allocation(c: &mut Criterion) {
let mut group = c.benchmark_group("memory_allocation");
for size in [1, 10, 100].iter() {
let size_bytes = size * 1024;
let test_data = vec![0u8; size_bytes];
group.bench_with_input(BenchmarkId::new("vec_clone", size), size, |b, _| {
b.iter(|| black_box(test_data.clone()))
});
group.bench_with_input(BenchmarkId::new("string_conversion", size), size, |b, _| {
b.iter(|| {
let cloned = test_data.clone();
black_box(String::from_utf8(cloned).unwrap_or_default())
})
});
#[cfg(feature = "arena")]
group.bench_with_input(BenchmarkId::new("arena_allocation", size), size, |b, _| {
b.iter(|| {
use bumpalo::Bump;
let bump = Bump::new();
black_box(bump.alloc_slice_copy(&test_data));
})
});
}
group.finish();
}
#[cfg(feature = "arena")]
criterion_group!(
benches,
bench_standard_parsing,
bench_arena_parsing,
bench_memory_allocation
);
#[cfg(not(feature = "arena"))]
criterion_group!(benches, bench_standard_parsing, bench_memory_allocation);
criterion_main!(benches);