Skip to content

Commit 05681a5

Browse files
authored
Merge pull request #93 from brannondorsey/bench-with-mmap-feature
Support benchmarking `mmap` feature
2 parents ee3b974 + 5278646 commit 05681a5

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

benches/lookup.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,43 @@ pub fn generate_ipv4(count: u64) -> Vec<IpAddr> {
2727
}
2828

2929
// Single-threaded
30-
pub fn bench_maxminddb(ips: &[IpAddr], reader: &maxminddb::Reader<Vec<u8>>) {
30+
pub fn bench_maxminddb<T>(ips: &[IpAddr], reader: &maxminddb::Reader<T>)
31+
where
32+
T: AsRef<[u8]>,
33+
{
3134
for ip in ips.iter() {
3235
let _ = reader.lookup::<geoip2::City>(*ip);
3336
}
3437
}
3538

3639
// Using rayon for parallel execution
37-
pub fn bench_par_maxminddb(ips: &[IpAddr], reader: &maxminddb::Reader<Vec<u8>>) {
40+
pub fn bench_par_maxminddb<T>(ips: &[IpAddr], reader: &maxminddb::Reader<T>)
41+
where
42+
T: AsRef<[u8]> + std::marker::Sync,
43+
{
3844
ips.par_iter().for_each(|ip| {
3945
let _ = reader.lookup::<geoip2::City>(*ip);
4046
});
4147
}
4248

49+
const DB_FILE: &str = "GeoLite2-City.mmdb";
50+
4351
pub fn criterion_benchmark(c: &mut Criterion) {
4452
let ips = generate_ipv4(100);
45-
let reader = maxminddb::Reader::open_readfile("GeoLite2-City.mmdb").unwrap();
53+
#[cfg(not(feature = "mmap"))]
54+
let reader = maxminddb::Reader::open_readfile(DB_FILE).unwrap();
55+
#[cfg(feature = "mmap")]
56+
let reader = maxminddb::Reader::open_mmap(DB_FILE).unwrap();
4657

4758
c.bench_function("maxminddb", |b| b.iter(|| bench_maxminddb(&ips, &reader)));
4859
}
4960

5061
pub fn criterion_par_benchmark(c: &mut Criterion) {
5162
let ips = generate_ipv4(100);
52-
let reader = maxminddb::Reader::open_readfile("GeoLite2-City.mmdb").unwrap();
63+
#[cfg(not(feature = "mmap"))]
64+
let reader = maxminddb::Reader::open_readfile(DB_FILE).unwrap();
65+
#[cfg(feature = "mmap")]
66+
let reader = maxminddb::Reader::open_mmap(DB_FILE).unwrap();
5367

5468
c.bench_function("maxminddb_par", |b| {
5569
b.iter(|| bench_par_maxminddb(&ips, &reader))

0 commit comments

Comments
 (0)