| 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, open_reader}; |
| 10 | |
| 11 | const DB_FILE: &str = "GeoLite2-City.mmdb"; |
| 12 | |
| 13 | #[derive(serde::Deserialize)] |
| 14 | struct EmptyRecord {} |
| 15 | |
| 16 | fn cache_lookups<'a, T>(ips: &[IpAddr], reader: &'a Reader<T>) -> Vec<LookupResult<'a, T>> |
| 17 | where |
| 18 | T: AsRef<[u8]>, |
| 19 | { |
| 20 | ips.iter() |
| 21 | .map(|ip| reader.lookup(*ip).unwrap()) |
| 22 | .filter(|r| r.has_data()) |
| 23 | .collect() |
| 24 | } |
| 25 | |
| 26 | fn bench_lookup_only<T>(ips: &[IpAddr], reader: &Reader<T>) |
| 27 | where |
| 28 | T: AsRef<[u8]>, |
| 29 | { |
| 30 | for ip in ips { |
| 31 | let result = reader.lookup(*ip).unwrap(); |
| 32 | black_box(result.has_data()); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | fn bench_decode_city_only<T>(results: &[LookupResult<'_, T>]) |
| 37 | where |
| 38 | T: AsRef<[u8]>, |
| 39 | { |
| 40 | for result in results { |
| 41 | let city: Option<geoip2::City<'_>> = result.decode().unwrap(); |
| 42 | black_box(city); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn bench_decode_country_only<T>(results: &[LookupResult<'_, T>]) |
| 47 | where |
| 48 | T: AsRef<[u8]>, |
| 49 | { |
| 50 | for result in results { |
| 51 | let country: Option<geoip2::Country<'_>> = result.decode().unwrap(); |
| 52 | black_box(country); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn bench_decode_empty_record<T>(results: &[LookupResult<'_, T>]) |
| 57 | where |
| 58 | T: AsRef<[u8]>, |
| 59 | { |
| 60 | for result in results { |
| 61 | let empty: Option<EmptyRecord> = result.decode().unwrap(); |
| 62 | black_box(empty); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn bench_decode_path_country_iso<T>(results: &[LookupResult<'_, T>]) |
| 67 | where |
| 68 | T: AsRef<[u8]>, |
| 69 | { |
| 70 | let path = [PathElement::Key("country"), PathElement::Key("iso_code")]; |
| 71 | for result in results { |
| 72 | let value: Option<&str> = result.decode_path(&path).unwrap(); |
| 73 | black_box(value); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fn bench_decode_path_city_name<T>(results: &[LookupResult<'_, T>]) |
| 78 | where |
| 79 | T: AsRef<[u8]>, |
| 80 | { |
| 81 | let path = [ |
| 82 | PathElement::Key("city"), |
| 83 | PathElement::Key("names"), |
| 84 | PathElement::Key("en"), |
| 85 | ]; |
| 86 | for result in results { |
| 87 | let value: Option<&str> = result.decode_path(&path).unwrap(); |
| 88 | black_box(value); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | pub fn serde_usage_benchmark(c: &mut Criterion) { |
| 93 | let ips = generate_ipv4(100); |
| 94 | let reader = open_reader(DB_FILE); |
| 95 | |
| 96 | let cached_results = cache_lookups(&ips, &reader); |
| 97 | |
| 98 | c.bench_function("serde_usage/lookup_only", |b| { |
| 99 | b.iter(|| bench_lookup_only(&ips, &reader)) |
| 100 | }); |
| 101 | c.bench_function("serde_usage/decode_city_only", |b| { |
| 102 | b.iter(|| bench_decode_city_only(&cached_results)) |
| 103 | }); |
| 104 | c.bench_function("serde_usage/decode_country_only", |b| { |
| 105 | b.iter(|| bench_decode_country_only(&cached_results)) |
| 106 | }); |
| 107 | c.bench_function("serde_usage/decode_empty_record", |b| { |
| 108 | b.iter(|| bench_decode_empty_record(&cached_results)) |
| 109 | }); |
| 110 | c.bench_function("serde_usage/decode_path_country_iso", |b| { |
| 111 | b.iter(|| bench_decode_path_country_iso(&cached_results)) |
| 112 | }); |
| 113 | c.bench_function("serde_usage/decode_path_city_name", |b| { |
| 114 | b.iter(|| bench_decode_path_city_name(&cached_results)) |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | criterion_group! { |
| 119 | name = benches; |
| 120 | config = Criterion::default().sample_size(20); |
| 121 | targets = serde_usage_benchmark |
| 122 | } |
| 123 | criterion_main!(benches); |