Debian Codegraph @

benches/lookup.rs

raw symbols SCIP
1 #[macro_use]
2 extern crate criterion;
3 extern crate maxminddb;
4 extern crate rayon;
5
6 use criterion::Criterion;
7 use maxminddb::geoip2;
8 use rayon::prelude::*;
9
10 use std::net::IpAddr;
11
12 mod common;
13 use common::{generate_ipv4, open_reader};
14
15 // Single-threaded
16 pub fn bench_maxminddb<T>(ips: &[IpAddr], reader: &maxminddb::Reader<T>)
17 where
18 T: AsRef<[u8]>,
19 {
20 for ip in ips.iter() {
21 let result = reader.lookup(*ip).unwrap();
22 if result.has_data() {
23 let _: geoip2::City = result.decode().unwrap().unwrap();
24 }
25 }
26 }
27
28 // Using rayon for parallel execution
29 pub fn bench_par_maxminddb<T>(ips: &[IpAddr], reader: &maxminddb::Reader<T>)
30 where
31 T: AsRef<[u8]> + std::marker::Sync,
32 {
33 ips.par_iter().for_each(|ip| {
34 let result = reader.lookup(*ip).unwrap();
35 if result.has_data() {
36 let _: geoip2::City = result.decode().unwrap().unwrap();
37 }
38 });
39 }
40
41 const DB_FILE: &str = "GeoLite2-City.mmdb";
42
43 pub fn criterion_benchmark(c: &mut Criterion) {
44 let ips = generate_ipv4(100);
45 let reader = open_reader(DB_FILE);
46
47 c.bench_function("maxminddb", |b| b.iter(|| bench_maxminddb(&ips, &reader)));
48 }
49
50 pub fn criterion_par_benchmark(c: &mut Criterion) {
51 let ips = generate_ipv4(100);
52 let reader = open_reader(DB_FILE);
53
54 c.bench_function("maxminddb_par", |b| {
55 b.iter(|| bench_par_maxminddb(&ips, &reader))
56 });
57 }
58
59 criterion_group! {
60 name = benches;
61 config = Criterion::default()
62 .sample_size(20);
63
64 targets = criterion_benchmark, criterion_par_benchmark
65 }
66 criterion_main!(benches);