|
| 1 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 2 | +use maxminddb::geoip2; |
| 3 | +use maxminddb::{LookupResult, PathElement, Reader}; |
| 4 | +use std::hint::black_box; |
| 5 | + |
| 6 | +use std::net::IpAddr; |
| 7 | + |
| 8 | +mod common; |
| 9 | +use common::generate_ipv4; |
| 10 | + |
| 11 | +const DB_FILE: &str = "GeoLite2-City.mmdb"; |
| 12 | + |
| 13 | +fn cache_lookups<'a, T>(ips: &[IpAddr], reader: &'a Reader<T>) -> Vec<LookupResult<'a, T>> |
| 14 | +where |
| 15 | + T: AsRef<[u8]>, |
| 16 | +{ |
| 17 | + ips.iter() |
| 18 | + .map(|ip| reader.lookup(*ip).unwrap()) |
| 19 | + .filter(|r| r.has_data()) |
| 20 | + .collect() |
| 21 | +} |
| 22 | + |
| 23 | +fn bench_lookup_only<T>(ips: &[IpAddr], reader: &Reader<T>) |
| 24 | +where |
| 25 | + T: AsRef<[u8]>, |
| 26 | +{ |
| 27 | + for ip in ips { |
| 28 | + let result = reader.lookup(*ip).unwrap(); |
| 29 | + black_box(result.has_data()); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +fn bench_decode_city_only<T>(results: &[LookupResult<'_, T>]) |
| 34 | +where |
| 35 | + T: AsRef<[u8]>, |
| 36 | +{ |
| 37 | + for result in results { |
| 38 | + let city: Option<geoip2::City<'_>> = result.decode().unwrap(); |
| 39 | + black_box(city); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +fn bench_decode_country_only<T>(results: &[LookupResult<'_, T>]) |
| 44 | +where |
| 45 | + T: AsRef<[u8]>, |
| 46 | +{ |
| 47 | + for result in results { |
| 48 | + let country: Option<geoip2::Country<'_>> = result.decode().unwrap(); |
| 49 | + black_box(country); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fn bench_decode_path_country_iso<T>(results: &[LookupResult<'_, T>]) |
| 54 | +where |
| 55 | + T: AsRef<[u8]>, |
| 56 | +{ |
| 57 | + let path = [PathElement::Key("country"), PathElement::Key("iso_code")]; |
| 58 | + for result in results { |
| 59 | + let value: Option<&str> = result.decode_path(&path).unwrap(); |
| 60 | + black_box(value); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +fn bench_decode_path_city_name<T>(results: &[LookupResult<'_, T>]) |
| 65 | +where |
| 66 | + T: AsRef<[u8]>, |
| 67 | +{ |
| 68 | + let path = [ |
| 69 | + PathElement::Key("city"), |
| 70 | + PathElement::Key("names"), |
| 71 | + PathElement::Key("en"), |
| 72 | + ]; |
| 73 | + for result in results { |
| 74 | + let value: Option<&str> = result.decode_path(&path).unwrap(); |
| 75 | + black_box(value); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +pub fn serde_usage_benchmark(c: &mut Criterion) { |
| 80 | + let ips = generate_ipv4(100); |
| 81 | + |
| 82 | + #[cfg(not(feature = "mmap"))] |
| 83 | + let reader = Reader::open_readfile(DB_FILE).unwrap(); |
| 84 | + #[cfg(feature = "mmap")] |
| 85 | + // SAFETY: The benchmark database file will not be modified during the benchmark. |
| 86 | + let reader = unsafe { Reader::open_mmap(DB_FILE) }.unwrap(); |
| 87 | + |
| 88 | + let cached_results = cache_lookups(&ips, &reader); |
| 89 | + |
| 90 | + c.bench_function("serde_usage/lookup_only", |b| { |
| 91 | + b.iter(|| bench_lookup_only(&ips, &reader)) |
| 92 | + }); |
| 93 | + c.bench_function("serde_usage/decode_city_only", |b| { |
| 94 | + b.iter(|| bench_decode_city_only(&cached_results)) |
| 95 | + }); |
| 96 | + c.bench_function("serde_usage/decode_country_only", |b| { |
| 97 | + b.iter(|| bench_decode_country_only(&cached_results)) |
| 98 | + }); |
| 99 | + c.bench_function("serde_usage/decode_path_country_iso", |b| { |
| 100 | + b.iter(|| bench_decode_path_country_iso(&cached_results)) |
| 101 | + }); |
| 102 | + c.bench_function("serde_usage/decode_path_city_name", |b| { |
| 103 | + b.iter(|| bench_decode_path_city_name(&cached_results)) |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +criterion_group! { |
| 108 | + name = benches; |
| 109 | + config = Criterion::default().sample_size(20); |
| 110 | + targets = serde_usage_benchmark |
| 111 | +} |
| 112 | +criterion_main!(benches); |
0 commit comments