use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use repe::{BodyFormat, HEADER_SIZE, Message, QueryFormat};
use std::hint::black_box;
const QUERY: &str = "/collect/file_chunk";
const BODY_SIZES: &[usize] = &[64, 4 * 1024, 64 * 1024, 4 * 1024 * 1024];
fn build_message_no_reserve(body_len: usize) -> Message {
Message::builder()
.id(1)
.query_str(QUERY)
.query_format(QueryFormat::JsonPointer)
.body_bytes(vec![0xABu8; body_len])
.body_format(BodyFormat::RawBinary)
.build()
}
fn build_message_with_reserve(body_len: usize) -> Message {
let mut body = Vec::with_capacity(HEADER_SIZE + QUERY.len() + body_len);
body.resize(body_len, 0xAB);
Message::builder()
.id(1)
.query_str(QUERY)
.query_format(QueryFormat::JsonPointer)
.body_bytes(body)
.body_format(BodyFormat::RawBinary)
.build()
}
fn bench_wire_serialization(c: &mut Criterion) {
let mut group = c.benchmark_group("wire_serialization");
for &size in BODY_SIZES {
group.throughput(Throughput::Bytes((HEADER_SIZE + QUERY.len() + size) as u64));
group.bench_with_input(BenchmarkId::new("to_vec", size), &size, |b, &size| {
b.iter_batched(
|| build_message_no_reserve(size),
|msg| black_box(msg.to_vec()),
criterion::BatchSize::SmallInput,
);
});
group.bench_with_input(
BenchmarkId::new("into_wire_bytes_slow", size),
&size,
|b, &size| {
b.iter_batched(
|| build_message_no_reserve(size),
|msg| black_box(msg.into_wire_bytes()),
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("into_wire_bytes_fast", size),
&size,
|b, &size| {
b.iter_batched(
|| build_message_with_reserve(size),
|msg| black_box(msg.into_wire_bytes()),
criterion::BatchSize::SmallInput,
);
},
);
}
group.finish();
}
criterion_group!(benches, bench_wire_serialization);
criterion_main!(benches);