#![feature(test)] extern crate test; use test::Bencher; use std::fs::File; use std::io::{BufRead, BufReader}; use std::iter::repeat_with; use std::net::{Ipv4Addr, Ipv6Addr}; use std::str::FromStr; use ip_network_table_deps_treebitmap::IpLookupTable; use iptrie::{IpPrefix, Ipv4Prefix, Ipv6Prefix, Ipv6Prefix120, LCTrieSet, RTrieSet}; fn read_ip() -> impl Iterator { let file = File::open("benches/bgpas.lpm").expect("can’t open LPM file"); BufReader::new(file) .lines() .map_while(Result::ok) .filter_map(|l| { let prefix = l.split(' ').next().expect("bad formatted file"); prefix.parse::

().ok() }) } fn random_ipv4addr() -> impl Iterator { use rand::*; use rand::distributions::*; let mut rng = thread_rng(); let addr = Uniform::::from(1..=u32::MAX); repeat_with(move || Ipv4Addr::from(addr.sample(&mut rng))) } fn random_ipv6addr() -> impl Iterator { use rand::*; use rand::distributions::*; let mut rng = thread_rng(); let addr = Uniform::::from(1..=u128::MAX); repeat_with(move || Ipv6Addr::from(addr.sample(&mut rng))) } #[bench] fn nop_ipv4prefix_trie(bencher: &mut Bencher) { let mut sample = random_ipv4addr(); let mut result = Vec::with_capacity(1_000); bencher.iter(|| result.push(sample.next().unwrap() )); println!("{}", result.len()); } #[bench] fn nop_ipv6prefix_trie(bencher: &mut Bencher) { let mut sample = random_ipv6addr(); let mut result = Vec::with_capacity(1_000); bencher.iter(|| result.push(sample.next().unwrap() )); println!("{}", result.len()); } #[bench] fn bgp_ipv4_lctrie(bencher: &mut Bencher) { let ip = read_ip::(); let trie = LCTrieSet::from_iter(ip); let mut sample= random_ipv4addr(); let mut result = Vec::with_capacity(1_000_000); bencher.iter(|| result.push(trie.lookup(&sample.next().unwrap())) ); println!("{}", result.len()); } #[bench] fn bgp_ipv4_trie(bencher: &mut Bencher) { let ip = read_ip::(); let trie = RTrieSet::from_iter(ip); let mut sample = random_ipv4addr(); let mut result = Vec::with_capacity(1_000_000); bencher.iter(|| result.push(trie.lookup(&sample.next().unwrap())) ); println!("{}", result.len()); } #[bench] fn bgp_ipv6_lctrie(bencher: &mut Bencher) { let ip = read_ip::(); let trie = LCTrieSet::from_iter(ip); let mut sample = random_ipv6addr(); let mut result = Vec::with_capacity(1_000_000); bencher.iter(|| result.push(trie.lookup(&sample.next().unwrap())) ); println!("{}", result.len()); } #[bench] fn bgp_ipv6_trie(bencher: &mut Bencher) { let ip = read_ip::(); let trie = RTrieSet::from_iter(ip); let mut sample = random_ipv6addr(); let mut result = Vec::with_capacity(1_000_000); bencher.iter(|| result.push(trie.lookup(&sample.next().unwrap())) ); println!("{}", result.len()); } #[bench] fn bgp_ipv6_120_lctrie(bencher: &mut Bencher) { let ip = read_ip::(); let trie = LCTrieSet::from_iter(ip); let mut sample = random_ipv6addr(); let mut result = Vec::with_capacity(1_000_000); bencher.iter(|| result.push(trie.lookup(&sample.next().unwrap())) ); println!("{}", result.len()); } #[bench] fn bgp_ipv4_treebitmap(bencher: &mut Bencher) { let trie = read_ip::() .fold(IpLookupTable::::with_capacity(1_000_000), |mut trie,p| { trie.insert(p.network(), p.len() as u32, ()); trie }); let mut sample = random_ipv4addr(); let mut result = Vec::with_capacity(1_000); bencher.iter(|| result.push(trie.longest_match(sample.next().unwrap())) ); println!("{}", result.len()); } #[bench] fn bgp_ipv6_treebitmap(bencher: &mut Bencher) { let trie = read_ip::() .fold(IpLookupTable::::with_capacity(1_000_000), |mut trie,p| { trie.insert(p.network(), p.len() as u32, ()); trie }); let mut sample = random_ipv6addr(); let mut result = Vec::with_capacity(1_000); bencher.iter(|| result.push(trie.longest_match(sample.next().unwrap())) ); println!("{}", result.len()); }