| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use crate::{ |
| 6 |
benchmarks::{new_store, BenchmarkWithInput}, |
| 7 |
geoname::{Geoname, GeonameType}, |
| 8 |
SuggestStore, |
| 9 |
}; |
| 10 |
|
| 11 |
pub struct GeonameBenchmark { |
| 12 |
args: FetchGeonamesArgs, |
| 13 |
should_match: bool, |
| 14 |
} |
| 15 |
|
| 16 |
#[derive(Clone, Debug)] |
| 17 |
pub struct FetchGeonamesArgs { |
| 18 |
query: &'static str, |
| 19 |
match_name_prefix: bool, |
| 20 |
filter: Option<Vec<Geoname>>, |
| 21 |
} |
| 22 |
|
| 23 |
pub struct IterationInput { |
| 24 |
fetch_args: FetchGeonamesArgs, |
| 25 |
should_match_message: String, |
| 26 |
} |
| 27 |
|
| 28 |
impl BenchmarkWithInput for GeonameBenchmark { |
| 29 |
type GlobalInput = SuggestStore; |
| 30 |
type IterationInput = IterationInput; |
| 31 |
|
| 32 |
fn global_input(&self) -> Self::GlobalInput { |
| 33 |
new_store() |
| 34 |
} |
| 35 |
|
| 36 |
fn iteration_input(&self) -> Self::IterationInput { |
| 37 |
let fetch_args = self.args.clone(); |
| 38 |
// Format the message now so it doesn't take up time in the benchmark. |
| 39 |
let should_match_message = format!("should_match for fetch: {:?}", fetch_args); |
| 40 |
IterationInput { |
| 41 |
fetch_args, |
| 42 |
should_match_message, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
fn benchmarked_code(&self, store: &Self::GlobalInput, i_input: Self::IterationInput) { |
| 47 |
let matches = store |
| 48 |
.fetch_geonames( |
| 49 |
i_input.fetch_args.query, |
| 50 |
i_input.fetch_args.match_name_prefix, |
| 51 |
i_input.fetch_args.filter, |
| 52 |
) |
| 53 |
.unwrap_or_else(|e| panic!("Error fetching geonames: {e}")); |
| 54 |
|
| 55 |
// Make sure matches were returned or not as expected. Otherwise the |
| 56 |
// benchmark might not be testing what it's intended to test. |
| 57 |
assert_eq!( |
| 58 |
!matches.is_empty(), |
| 59 |
self.should_match, |
| 60 |
"{}", |
| 61 |
i_input.should_match_message, |
| 62 |
); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
pub fn all_benchmarks() -> Vec<(&'static str, GeonameBenchmark)> { |
| 67 |
let ny_state = Geoname { |
| 68 |
geoname_id: 5128638, |
| 69 |
geoname_type: GeonameType::AdminDivision { level: 1 }, |
| 70 |
name: "New York".to_string(), |
| 71 |
feature_class: "A".to_string(), |
| 72 |
feature_code: "ADM1".to_string(), |
| 73 |
country_code: "US".to_string(), |
| 74 |
admin_division_codes: [(1, "NY".to_string())].into(), |
| 75 |
population: 19274244, |
| 76 |
latitude: "43.00035".to_string(), |
| 77 |
longitude: "-75.4999".to_string(), |
| 78 |
}; |
| 79 |
|
| 80 |
vec![ |
| 81 |
// no matches |
| 82 |
( |
| 83 |
"geoname-fetch-no-match-1", |
| 84 |
GeonameBenchmark { |
| 85 |
args: FetchGeonamesArgs { |
| 86 |
query: "nomatch", |
| 87 |
match_name_prefix: false, |
| 88 |
filter: None, |
| 89 |
}, |
| 90 |
should_match: false, |
| 91 |
} |
| 92 |
), |
| 93 |
( |
| 94 |
"geoname-fetch-no-match-2", |
| 95 |
GeonameBenchmark { |
| 96 |
args: FetchGeonamesArgs { |
| 97 |
query: "no match", |
| 98 |
match_name_prefix: false, |
| 99 |
filter: None, |
| 100 |
}, |
| 101 |
should_match: false, |
| 102 |
} |
| 103 |
), |
| 104 |
( |
| 105 |
"geoname-fetch-no-match-3", |
| 106 |
GeonameBenchmark { |
| 107 |
args: FetchGeonamesArgs { |
| 108 |
query: "no match either", |
| 109 |
match_name_prefix: false, |
| 110 |
filter: None, |
| 111 |
}, |
| 112 |
should_match: false, |
| 113 |
} |
| 114 |
), |
| 115 |
( |
| 116 |
"geoname-fetch-no-match-long", |
| 117 |
GeonameBenchmark { |
| 118 |
args: FetchGeonamesArgs { |
| 119 |
query: "this is a very long string that does not match anything in the geonames database but it sure is very long", |
| 120 |
match_name_prefix: false, |
| 121 |
filter: None, |
| 122 |
}, |
| 123 |
should_match: false, |
| 124 |
} |
| 125 |
), |
| 126 |
|
| 127 |
// no matches w/ prefix matching |
| 128 |
( |
| 129 |
"geoname-fetch-no-match-1-prefix", |
| 130 |
GeonameBenchmark { |
| 131 |
args: FetchGeonamesArgs { |
| 132 |
query: "nomatch", |
| 133 |
match_name_prefix: true, |
| 134 |
filter: None, |
| 135 |
}, |
| 136 |
should_match: false, |
| 137 |
} |
| 138 |
), |
| 139 |
( |
| 140 |
"geoname-fetch-no-match-2-prefix", |
| 141 |
GeonameBenchmark { |
| 142 |
args: FetchGeonamesArgs { |
| 143 |
query: "no match", |
| 144 |
match_name_prefix: true, |
| 145 |
filter: None, |
| 146 |
}, |
| 147 |
should_match: false, |
| 148 |
} |
| 149 |
), |
| 150 |
( |
| 151 |
"geoname-fetch-no-match-3-prefix", |
| 152 |
GeonameBenchmark { |
| 153 |
args: FetchGeonamesArgs { |
| 154 |
query: "no match either", |
| 155 |
match_name_prefix: true, |
| 156 |
filter: None, |
| 157 |
}, |
| 158 |
should_match: false, |
| 159 |
} |
| 160 |
), |
| 161 |
( |
| 162 |
"geoname-fetch-no-match-long-prefix", |
| 163 |
GeonameBenchmark { |
| 164 |
args: FetchGeonamesArgs { |
| 165 |
query: "this is a very long string that does not match anything in the geonames database but it sure is very long", |
| 166 |
match_name_prefix: true, |
| 167 |
filter: None, |
| 168 |
}, |
| 169 |
should_match: false, |
| 170 |
} |
| 171 |
), |
| 172 |
|
| 173 |
// abbreviations and airport codes |
| 174 |
( |
| 175 |
"geoname-fetch-abbr-ny", |
| 176 |
GeonameBenchmark { |
| 177 |
args: FetchGeonamesArgs { |
| 178 |
query: "ny", |
| 179 |
match_name_prefix: false, |
| 180 |
filter: None, |
| 181 |
}, |
| 182 |
should_match: true, |
| 183 |
} |
| 184 |
), |
| 185 |
( |
| 186 |
"geoname-fetch-abbr-nyc", |
| 187 |
GeonameBenchmark { |
| 188 |
args: FetchGeonamesArgs { |
| 189 |
query: "nyc", |
| 190 |
match_name_prefix: false, |
| 191 |
filter: None, |
| 192 |
}, |
| 193 |
should_match: true, |
| 194 |
} |
| 195 |
), |
| 196 |
( |
| 197 |
"geoname-fetch-abbr-ca", |
| 198 |
GeonameBenchmark { |
| 199 |
args: FetchGeonamesArgs { |
| 200 |
query: "ca", |
| 201 |
match_name_prefix: false, |
| 202 |
filter: None, |
| 203 |
}, |
| 204 |
should_match: true, |
| 205 |
} |
| 206 |
), |
| 207 |
( |
| 208 |
"geoname-fetch-airport-pdx", |
| 209 |
GeonameBenchmark { |
| 210 |
args: FetchGeonamesArgs { |
| 211 |
query: "pdx", |
| 212 |
match_name_prefix: false, |
| 213 |
filter: None, |
| 214 |
}, |
| 215 |
should_match: true, |
| 216 |
} |
| 217 |
), |
| 218 |
( |
| 219 |
"geoname-fetch-airport-roc", |
| 220 |
GeonameBenchmark { |
| 221 |
args: FetchGeonamesArgs { |
| 222 |
query: "roc", |
| 223 |
match_name_prefix: false, |
| 224 |
filter: None, |
| 225 |
}, |
| 226 |
should_match: true, |
| 227 |
} |
| 228 |
), |
| 229 |
|
| 230 |
// abbreviations and airport codes w/ prefix matching |
| 231 |
( |
| 232 |
"geoname-fetch-abbr-prefix-ny", |
| 233 |
GeonameBenchmark { |
| 234 |
args: FetchGeonamesArgs { |
| 235 |
query: "ny", |
| 236 |
match_name_prefix: true, |
| 237 |
filter: None, |
| 238 |
}, |
| 239 |
should_match: true, |
| 240 |
} |
| 241 |
), |
| 242 |
( |
| 243 |
"geoname-fetch-abbr-prefix-nyc", |
| 244 |
GeonameBenchmark { |
| 245 |
args: FetchGeonamesArgs { |
| 246 |
query: "nyc", |
| 247 |
match_name_prefix: true, |
| 248 |
filter: None, |
| 249 |
}, |
| 250 |
should_match: true, |
| 251 |
} |
| 252 |
), |
| 253 |
( |
| 254 |
"geoname-fetch-abbr-prefix-ca", |
| 255 |
GeonameBenchmark { |
| 256 |
args: FetchGeonamesArgs { |
| 257 |
query: "ca", |
| 258 |
match_name_prefix: true, |
| 259 |
filter: None, |
| 260 |
}, |
| 261 |
should_match: true, |
| 262 |
} |
| 263 |
), |
| 264 |
( |
| 265 |
"geoname-fetch-airport-prefix-pdx", |
| 266 |
GeonameBenchmark { |
| 267 |
args: FetchGeonamesArgs { |
| 268 |
query: "pdx", |
| 269 |
match_name_prefix: true, |
| 270 |
filter: None, |
| 271 |
}, |
| 272 |
should_match: true, |
| 273 |
} |
| 274 |
), |
| 275 |
( |
| 276 |
"geoname-fetch-airport-prefix-roc", |
| 277 |
GeonameBenchmark { |
| 278 |
args: FetchGeonamesArgs { |
| 279 |
query: "roc", |
| 280 |
match_name_prefix: true, |
| 281 |
filter: None, |
| 282 |
}, |
| 283 |
should_match: true, |
| 284 |
} |
| 285 |
), |
| 286 |
|
| 287 |
// full names |
| 288 |
( |
| 289 |
"geoname-fetch-name-new-york", |
| 290 |
GeonameBenchmark { |
| 291 |
args: FetchGeonamesArgs { |
| 292 |
query: "new york", |
| 293 |
match_name_prefix: false, |
| 294 |
filter: None, |
| 295 |
}, |
| 296 |
should_match: true, |
| 297 |
} |
| 298 |
), |
| 299 |
( |
| 300 |
"geoname-fetch-name-rochester", |
| 301 |
GeonameBenchmark { |
| 302 |
args: FetchGeonamesArgs { |
| 303 |
query: "rochester", |
| 304 |
match_name_prefix: false, |
| 305 |
filter: None, |
| 306 |
}, |
| 307 |
should_match: true, |
| 308 |
} |
| 309 |
), |
| 310 |
|
| 311 |
// full names w/ prefix matching |
| 312 |
( |
| 313 |
"geoname-fetch-name-prefix-new-york", |
| 314 |
GeonameBenchmark { |
| 315 |
args: FetchGeonamesArgs { |
| 316 |
query: "new york", |
| 317 |
match_name_prefix: true, |
| 318 |
filter: None, |
| 319 |
}, |
| 320 |
should_match: true, |
| 321 |
} |
| 322 |
), |
| 323 |
( |
| 324 |
"geoname-fetch-name-prefix-rochester", |
| 325 |
GeonameBenchmark { |
| 326 |
args: FetchGeonamesArgs { |
| 327 |
query: "rochester", |
| 328 |
match_name_prefix: true, |
| 329 |
filter: None, |
| 330 |
}, |
| 331 |
should_match: true, |
| 332 |
} |
| 333 |
), |
| 334 |
|
| 335 |
// filtering |
| 336 |
( |
| 337 |
"geoname-fetch-filter-ny", |
| 338 |
GeonameBenchmark { |
| 339 |
args: FetchGeonamesArgs { |
| 340 |
query: "ny", |
| 341 |
match_name_prefix: false, |
| 342 |
filter: Some(vec![ny_state.clone()]), |
| 343 |
}, |
| 344 |
should_match: true, |
| 345 |
} |
| 346 |
), |
| 347 |
] |
| 348 |
} |