#![allow(missing_docs)]
use cobs_codec_rs::{cobs, cobsr};
use criterion::{Criterion, Throughput, black_box, criterion_group, criterion_main};
const PAYLOAD_LEN: usize = 1024;
fn representative_payload() -> Vec<u8> {
let mut state: u32 = 0x1234_5678;
let mut next = || {
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
state
};
(0..PAYLOAD_LEN)
.map(|_| {
let bytes = next().to_le_bytes();
if bytes[0] % 8 == 0 {
0x00
} else {
match bytes[1] {
0 => 0xA5,
b => b,
}
}
})
.collect()
}
fn throughput(c: &mut Criterion) {
let payload = representative_payload();
let encoded = cobs::encode_to_vec(&payload);
let mut group = c.benchmark_group("cobs_1kib");
group.throughput(Throughput::Bytes(1024));
group.bench_function("cobs_encode", |b| {
b.iter(|| cobs::encode_to_vec(black_box(&payload)));
});
group.bench_function("cobs_decode", |b| {
b.iter(|| cobs::decode_to_vec(black_box(&encoded)).unwrap());
});
group.bench_function("cobsr_encode", |b| {
b.iter(|| cobsr::encode_to_vec(black_box(&payload)));
});
group.finish();
}
criterion_group!(benches, throughput);
criterion_main!(benches);