From 52786465f6b8f8c2822f2615edc2f4605eaa7e91 Mon Sep 17 00:00:00 2001 From: Brannon Dorsey Date: Sun, 23 Feb 2025 11:01:30 -0500 Subject: [PATCH] Support benchmarking mmap feature Adds support to run `cargo bench --features=mmap` --- benches/lookup.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/benches/lookup.rs b/benches/lookup.rs index bf9074a4..9a87d619 100644 --- a/benches/lookup.rs +++ b/benches/lookup.rs @@ -27,29 +27,43 @@ pub fn generate_ipv4(count: u64) -> Vec { } // Single-threaded -pub fn bench_maxminddb(ips: &[IpAddr], reader: &maxminddb::Reader>) { +pub fn bench_maxminddb(ips: &[IpAddr], reader: &maxminddb::Reader) +where + T: AsRef<[u8]>, +{ for ip in ips.iter() { let _ = reader.lookup::(*ip); } } // Using rayon for parallel execution -pub fn bench_par_maxminddb(ips: &[IpAddr], reader: &maxminddb::Reader>) { +pub fn bench_par_maxminddb(ips: &[IpAddr], reader: &maxminddb::Reader) +where + T: AsRef<[u8]> + std::marker::Sync, +{ ips.par_iter().for_each(|ip| { let _ = reader.lookup::(*ip); }); } +const DB_FILE: &str = "GeoLite2-City.mmdb"; + pub fn criterion_benchmark(c: &mut Criterion) { let ips = generate_ipv4(100); - let reader = maxminddb::Reader::open_readfile("GeoLite2-City.mmdb").unwrap(); + #[cfg(not(feature = "mmap"))] + let reader = maxminddb::Reader::open_readfile(DB_FILE).unwrap(); + #[cfg(feature = "mmap")] + let reader = maxminddb::Reader::open_mmap(DB_FILE).unwrap(); c.bench_function("maxminddb", |b| b.iter(|| bench_maxminddb(&ips, &reader))); } pub fn criterion_par_benchmark(c: &mut Criterion) { let ips = generate_ipv4(100); - let reader = maxminddb::Reader::open_readfile("GeoLite2-City.mmdb").unwrap(); + #[cfg(not(feature = "mmap"))] + let reader = maxminddb::Reader::open_readfile(DB_FILE).unwrap(); + #[cfg(feature = "mmap")] + let reader = maxminddb::Reader::open_mmap(DB_FILE).unwrap(); c.bench_function("maxminddb_par", |b| { b.iter(|| bench_par_maxminddb(&ips, &reader))