@@ -27,29 +27,43 @@ pub fn generate_ipv4(count: u64) -> Vec<IpAddr> {
27
27
}
28
28
29
29
// 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
+ {
31
34
for ip in ips. iter ( ) {
32
35
let _ = reader. lookup :: < geoip2:: City > ( * ip) ;
33
36
}
34
37
}
35
38
36
39
// 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
+ {
38
44
ips. par_iter ( ) . for_each ( |ip| {
39
45
let _ = reader. lookup :: < geoip2:: City > ( * ip) ;
40
46
} ) ;
41
47
}
42
48
49
+ const DB_FILE : & str = "GeoLite2-City.mmdb" ;
50
+
43
51
pub fn criterion_benchmark ( c : & mut Criterion ) {
44
52
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 ( ) ;
46
57
47
58
c. bench_function ( "maxminddb" , |b| b. iter ( || bench_maxminddb ( & ips, & reader) ) ) ;
48
59
}
49
60
50
61
pub fn criterion_par_benchmark ( c : & mut Criterion ) {
51
62
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 ( ) ;
53
67
54
68
c. bench_function ( "maxminddb_par" , |b| {
55
69
b. iter ( || bench_par_maxminddb ( & ips, & reader) )
0 commit comments