From 3a9017e62bd2f8efdcabf60861160d6eb236bb43 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sat, 5 Aug 2023 12:25:33 -0400 Subject: [PATCH] Add some experimental benchmarks --- libs/interproto/Cargo.toml | 7 ++ libs/interproto/benches/benchmarks.rs | 106 ++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 libs/interproto/benches/benchmarks.rs diff --git a/libs/interproto/Cargo.toml b/libs/interproto/Cargo.toml index 3ae9d31..075a418 100644 --- a/libs/interproto/Cargo.toml +++ b/libs/interproto/Cargo.toml @@ -23,3 +23,10 @@ log = "^0.4" pnet = "0.34.0" thiserror = "^1.0.44" profiling = "1.0.9" + +[dev-dependencies] +criterion = { version = "0.5.1", features = ["html_reports"] } + +[[bench]] +name = "benchmarks" +harness = false \ No newline at end of file diff --git a/libs/interproto/benches/benchmarks.rs b/libs/interproto/benches/benchmarks.rs new file mode 100644 index 0000000..773a366 --- /dev/null +++ b/libs/interproto/benches/benchmarks.rs @@ -0,0 +1,106 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use interproto::protocols::*; +use pnet::packet::{ + tcp::{MutableTcpPacket, TcpPacket}, + udp::{MutableUdpPacket, UdpPacket}, +}; + +/// Translate TCP packets from IPv4 to IPv6 +fn bench_tcp_4_to_6(c: &mut Criterion) { + // Create a test input packet + let mut input_buffer = vec![0u8; TcpPacket::minimum_packet_size() + 13]; + let mut input_packet = MutableTcpPacket::new(&mut input_buffer).unwrap(); + input_packet.set_source(1234); + input_packet.set_destination(5678); + input_packet.set_payload(&"Hello, world!".as_bytes().to_vec()); + + // Pre-calculate the source and dest addrs + let source = "2001:db8::1".parse().unwrap(); + let dest = "2001:db8::2".parse().unwrap(); + + // Build a benchmark group for measuring throughput + let mut group = c.benchmark_group("tcp_4_to_6"); + group.throughput(criterion::Throughput::Bytes(input_buffer.len() as u64)); + group.bench_function("translate", |b| { + b.iter(|| tcp::recalculate_tcp_checksum_ipv6(&input_buffer, source, dest)) + }); + group.finish(); +} + +/// Translate TCP packets from IPv6 to IPv4 +fn bench_tcp_6_to_4(c: &mut Criterion) { + // Create a test input packet + let mut input_buffer = vec![0u8; TcpPacket::minimum_packet_size() + 13]; + let mut input_packet = MutableTcpPacket::new(&mut input_buffer).unwrap(); + input_packet.set_source(1234); + input_packet.set_destination(5678); + input_packet.set_payload(&"Hello, world!".as_bytes().to_vec()); + + // Pre-calculate the source and dest addrs + let source = "192.0.2.1".parse().unwrap(); + let dest = "192.0.2.2".parse().unwrap(); + + // Build a benchmark group for measuring throughput + let mut group = c.benchmark_group("tcp_6_to_4"); + group.throughput(criterion::Throughput::Bytes(input_buffer.len() as u64)); + group.bench_function("translate", |b| { + b.iter(|| tcp::recalculate_tcp_checksum_ipv4(&input_buffer, source, dest)) + }); + group.finish(); +} + +/// Translate UDP packets from IPv4 to IPv6 +fn bench_udp_4_to_6(c: &mut Criterion) { + // Create a test input packet + let mut input_buffer = vec![0u8; UdpPacket::minimum_packet_size() + 13]; + let mut udp_packet = MutableUdpPacket::new(&mut input_buffer).unwrap(); + udp_packet.set_source(1234); + udp_packet.set_destination(5678); + udp_packet.set_length(13); + udp_packet.set_payload(&"Hello, world!".as_bytes().to_vec()); + + // Pre-calculate the source and dest addrs + let source = "2001:db8::1".parse().unwrap(); + let dest = "2001:db8::2".parse().unwrap(); + + // Build a benchmark group for measuring throughput + let mut group = c.benchmark_group("udp_4_to_6"); + group.throughput(criterion::Throughput::Bytes(input_buffer.len() as u64)); + group.bench_function("translate", |b| { + b.iter(|| udp::recalculate_udp_checksum_ipv6(&input_buffer, source, dest)) + }); + group.finish(); +} + +/// Translate UDP packets from IPv6 to IPv4 +fn bench_udp_6_to_4(c: &mut Criterion) { + // Create a test input packet + let mut input_buffer = vec![0u8; UdpPacket::minimum_packet_size() + 13]; + let mut udp_packet = MutableUdpPacket::new(&mut input_buffer).unwrap(); + udp_packet.set_source(1234); + udp_packet.set_destination(5678); + udp_packet.set_length(13); + udp_packet.set_payload(&"Hello, world!".as_bytes().to_vec()); + + // Pre-calculate the source and dest addrs + let source = "192.0.2.1".parse().unwrap(); + let dest = "192.0.2.2".parse().unwrap(); + + // Build a benchmark group for measuring throughput + let mut group = c.benchmark_group("udp_6_to_4"); + group.throughput(criterion::Throughput::Bytes(input_buffer.len() as u64)); + group.bench_function("translate", |b| { + b.iter(|| udp::recalculate_udp_checksum_ipv4(&input_buffer, source, dest)) + }); + group.finish(); +} + +// Generate a main function +criterion_group!( + benches, + bench_tcp_4_to_6, + bench_tcp_6_to_4, + bench_udp_4_to_6, + bench_udp_6_to_4 +); +criterion_main!(benches);