use criterion::{black_box, criterion_group, criterion_main, Criterion}; use btest_rs::protocol::{Command, StatusMessage, CMD_PROTO_TCP, CMD_DIR_BOTH}; fn bench_command_serialize(c: &mut Criterion) { let cmd = Command::new(CMD_PROTO_TCP, CMD_DIR_BOTH); c.bench_function("command_serialize", |b| { b.iter(|| black_box(cmd.serialize())) }); } fn bench_command_deserialize(c: &mut Criterion) { let bytes = [0x01, 0x03, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; c.bench_function("command_deserialize", |b| { b.iter(|| black_box(Command::deserialize(black_box(&bytes)))) }); } fn bench_status_message_serialize(c: &mut Criterion) { let msg = StatusMessage { seq: 42, bytes_received: 1_000_000, cpu_load: 50, }; c.bench_function("status_message_serialize", |b| { b.iter(|| black_box(msg.serialize())) }); } fn bench_status_message_deserialize(c: &mut Criterion) { let bytes = [0x07, 0xB2, 0x00, 0x00, 0x2A, 0x00, 0x00, 0x00, 0x40, 0x42, 0x0F, 0x00]; c.bench_function("status_message_deserialize", |b| { b.iter(|| black_box(StatusMessage::deserialize(black_box(&bytes)))) }); } fn bench_roundtrip(c: &mut Criterion) { let cmd = Command::new(CMD_PROTO_TCP, CMD_DIR_BOTH); let msg = StatusMessage { seq: 99, bytes_received: 50_000, cpu_load: 75, }; c.bench_function("command_roundtrip", |b| { b.iter(|| { let s = black_box(cmd.serialize()); black_box(Command::deserialize(&s)) }) }); c.bench_function("status_message_roundtrip", |b| { b.iter(|| { let s = black_box(msg.serialize()); black_box(StatusMessage::deserialize(&s)) }) }); } criterion_group!( protocol_benches, bench_command_serialize, bench_command_deserialize, bench_status_message_serialize, bench_status_message_deserialize, bench_roundtrip ); criterion_main!(protocol_benches);