use bytes::BytesMut;
use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use rustis::resp::{Command, bench_encode_command, cmd};
use std::hint::black_box;
fn build_set(payload_len: usize) -> Command {
let value = vec![b'x'; payload_len];
cmd("SET").key("bench:key").arg(value).into()
}
fn bench_command_encode(c: &mut Criterion) {
let mut group = c.benchmark_group("command_encode/set");
for &payload_len in &[64usize, 4 * 1024, 256 * 1024, 4 * 1024 * 1024] {
let command = build_set(payload_len);
let mut buf = BytesMut::with_capacity(payload_len + 64);
group.throughput(Throughput::Bytes(payload_len as u64));
group.bench_with_input(
BenchmarkId::from_parameter(payload_len),
&command,
|b, command| {
b.iter(|| bench_encode_command(black_box(command), &mut buf));
},
);
}
group.finish();
}
criterion_group!(benches, bench_command_encode);
criterion_main!(benches);