Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarks #8

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 64 additions & 35 deletions benches/routing_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,77 @@ use std::net::{IpAddr, Ipv4Addr};

use criterion::{criterion_group, criterion_main, Criterion};

use cidrs::{CidrRoutingTable, Ipv4Cidr};
use cidrs::{Cidr, CidrRoutingTable, Ipv4Cidr};

fn match_longest(m: &CidrRoutingTable<String>, addr: IpAddr) -> String {
m.match_longest(addr).unwrap().1.clone()
}
fn fixture() -> CidrRoutingTable<u64> {
let mut m = CidrRoutingTable::new();
let mut v = 0;
fn next(v: &mut u64) -> u64 {
*v += 1;
*v
}
m.insert(Ipv4Cidr::new([0, 0, 0, 0], 0).unwrap(), next(&mut v));

for i1 in 1..128 {
m.insert(Ipv4Cidr::new([i1, 0, 0, 0], 6).unwrap(), next(&mut v));
m.insert(Ipv4Cidr::new([i1, 0, 0, 0], 7).unwrap(), next(&mut v));
m.insert(Ipv4Cidr::new([i1, 0, 0, 0], 8).unwrap(), next(&mut v));

fn routing_table_match_longest_benchmark(c: &mut Criterion) {
let table = {
let mut m = CidrRoutingTable::new();

let cidr = Ipv4Cidr::new([0, 0, 0, 0], 0).unwrap();
m.insert(cidr, cidr.to_string());

for i1 in 1..128 {
let cidr = Ipv4Cidr::new([i1, 0, 0, 0], 6).unwrap();
m.insert(cidr, cidr.to_string());
let cidr = Ipv4Cidr::new([i1, 0, 0, 0], 7).unwrap();
m.insert(cidr, cidr.to_string());
let cidr = Ipv4Cidr::new([i1, 0, 0, 0], 8).unwrap();
m.insert(cidr, cidr.to_string());

for i2 in 0..128 {
let cidr = Ipv4Cidr::new([i1, i2, 0, 0], 9).unwrap();
m.insert(cidr, cidr.to_string());
let cidr = Ipv4Cidr::new([i1, i2, 0, 0], 11).unwrap();
m.insert(cidr, cidr.to_string());
let cidr = Ipv4Cidr::new([i1, i2, 0, 0], 13).unwrap();
m.insert(cidr, cidr.to_string());
for i3 in 0..128 {
let cidr = Ipv4Cidr::new([i1, i2, i3, 0], 24).unwrap();
m.insert(cidr, cidr.to_string());
}
for i2 in 0..128 {
m.insert(Ipv4Cidr::new([i1, i2, 0, 0], 9).unwrap(), next(&mut v));
m.insert(Ipv4Cidr::new([i1, i2, 0, 0], 11).unwrap(), next(&mut v));
m.insert(Ipv4Cidr::new([i1, i2, 0, 0], 13).unwrap(), next(&mut v));
for i3 in 0..128 {
m.insert(Ipv4Cidr::new([i1, i2, i3, 0], 24).unwrap(), next(&mut v));
}
}
}

m
}

m
};
fn match_longest(m: &CidrRoutingTable<u64>, addr: IpAddr) -> u64 {
m.match_longest(addr).unwrap().1.clone()
}

fn match_longest_benchmark(c: &mut Criterion) {
let table = fixture();
let ip = IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4));
c.bench_function("match_longest", |b| {
b.iter(|| match_longest(&table, black_box(ip)))
});
}

fn match_exact(m: &CidrRoutingTable<u64>, cidr: Cidr) -> u64 {
m.match_exact(cidr).unwrap().clone()
}

fn match_exact_benchmark(c: &mut Criterion) {
let table = fixture();
let cidr = Cidr::V4(Ipv4Cidr::new([127, 88, 55, 0], 24).unwrap());
c.bench_function("match_exact", |b| {
b.iter(|| match_exact(&table, black_box(cidr)))
});
}

fn insert(m: &mut CidrRoutingTable<u64>, cidr: Cidr, value: u64) {
m.insert(cidr, value);
}

c.bench_function("longest_match", |b| {
b.iter(|| match_longest(&table, black_box(IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4)))))
fn insert_benchmark(c: &mut Criterion) {
let cidr = Cidr::V4(Ipv4Cidr::new([127, 88, 55, 240], 32).unwrap());
c.bench_function("insert", |b| {
b.iter(|| {
let mut table = CidrRoutingTable::new();
insert(&mut table, black_box(cidr), black_box(42));
})
});
}

criterion_group!(benches, routing_table_match_longest_benchmark);
criterion_group!(
benches,
match_longest_benchmark,
match_exact_benchmark,
insert_benchmark,
);
criterion_main!(benches);