| 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 |
SuggestStore, SuggestionProvider, SuggestionQuery, |
| 8 |
}; |
| 9 |
|
| 10 |
pub struct QueryBenchmark { |
| 11 |
provider: SuggestionProvider, |
| 12 |
query: &'static str, |
| 13 |
should_match: bool, |
| 14 |
} |
| 15 |
|
| 16 |
pub struct IterationInput { |
| 17 |
query: SuggestionQuery, |
| 18 |
should_match_message: String, |
| 19 |
} |
| 20 |
|
| 21 |
impl BenchmarkWithInput for QueryBenchmark { |
| 22 |
type GlobalInput = SuggestStore; |
| 23 |
type IterationInput = IterationInput; |
| 24 |
|
| 25 |
fn global_input(&self) -> Self::GlobalInput { |
| 26 |
new_store() |
| 27 |
} |
| 28 |
|
| 29 |
fn iteration_input(&self) -> Self::IterationInput { |
| 30 |
let query = SuggestionQuery { |
| 31 |
providers: vec![self.provider], |
| 32 |
keyword: self.query.to_string(), |
| 33 |
..SuggestionQuery::default() |
| 34 |
}; |
| 35 |
// Format the message now so it doesn't take up time in the benchmark. |
| 36 |
let should_match_message = format!("should_match for query: {:?}", query); |
| 37 |
IterationInput { |
| 38 |
query, |
| 39 |
should_match_message, |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
fn benchmarked_code(&self, store: &Self::GlobalInput, i_input: Self::IterationInput) { |
| 44 |
let suggestions = store |
| 45 |
.query(i_input.query) |
| 46 |
.unwrap_or_else(|e| panic!("Error querying store: {e}")); |
| 47 |
|
| 48 |
// Make sure matches were returned or not as expected. Otherwise the |
| 49 |
// benchmark might not be testing what it's intended to test. |
| 50 |
assert_eq!( |
| 51 |
!suggestions.is_empty(), |
| 52 |
self.should_match, |
| 53 |
"{}", |
| 54 |
i_input.should_match_message, |
| 55 |
); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
pub fn all_benchmarks() -> Vec<(&'static str, QueryBenchmark)> { |
| 60 |
vec![ |
| 61 |
// Fakespot queries, these attempt to perform prefix matches with various character |
| 62 |
// lengths. |
| 63 |
// |
| 64 |
// The query code will only do a prefix match if the total input length is > 3 chars. |
| 65 |
// Therefore, to test shorter prefixes we use 2-term queries. |
| 66 |
( |
| 67 |
"query-fakespot-hand-s", |
| 68 |
QueryBenchmark { |
| 69 |
provider: SuggestionProvider::Fakespot, |
| 70 |
query: "hand s", |
| 71 |
should_match: true, |
| 72 |
} |
| 73 |
), |
| 74 |
( |
| 75 |
"query-fakespot-hand-sa", |
| 76 |
QueryBenchmark { |
| 77 |
provider: SuggestionProvider::Fakespot, |
| 78 |
query: "hand sa", |
| 79 |
should_match: true, |
| 80 |
} |
| 81 |
), |
| 82 |
( |
| 83 |
"query-fakespot-hand-san", |
| 84 |
QueryBenchmark { |
| 85 |
provider: SuggestionProvider::Fakespot, |
| 86 |
query: "hand san", |
| 87 |
should_match: true, |
| 88 |
} |
| 89 |
), |
| 90 |
( |
| 91 |
"query-fakespot-sani", |
| 92 |
QueryBenchmark { |
| 93 |
provider: SuggestionProvider::Fakespot, |
| 94 |
query: "sani", |
| 95 |
should_match: true, |
| 96 |
} |
| 97 |
), |
| 98 |
( |
| 99 |
"query-fakespot-sanit", |
| 100 |
QueryBenchmark { |
| 101 |
provider: SuggestionProvider::Fakespot, |
| 102 |
query: "sanit", |
| 103 |
should_match: true, |
| 104 |
} |
| 105 |
), |
| 106 |
( |
| 107 |
"query-fakespot-saniti", |
| 108 |
QueryBenchmark { |
| 109 |
provider: SuggestionProvider::Fakespot, |
| 110 |
query: "saniti", |
| 111 |
should_match: false, |
| 112 |
}, |
| 113 |
), |
| 114 |
|
| 115 |
// weather: no matches |
| 116 |
( |
| 117 |
"query-weather-no-match-1", |
| 118 |
QueryBenchmark { |
| 119 |
provider: SuggestionProvider::Weather, |
| 120 |
query: "nomatch", |
| 121 |
should_match: false, |
| 122 |
}, |
| 123 |
), |
| 124 |
( |
| 125 |
"query-weather-no-match-2", |
| 126 |
QueryBenchmark { |
| 127 |
provider: SuggestionProvider::Weather, |
| 128 |
query: "no match", |
| 129 |
should_match: false, |
| 130 |
}, |
| 131 |
), |
| 132 |
( |
| 133 |
"query-weather-no-match-3", |
| 134 |
QueryBenchmark { |
| 135 |
provider: SuggestionProvider::Weather, |
| 136 |
query: "no match either", |
| 137 |
should_match: false, |
| 138 |
}, |
| 139 |
), |
| 140 |
( |
| 141 |
"query-weather-no-match-long-1", |
| 142 |
QueryBenchmark { |
| 143 |
provider: SuggestionProvider::Weather, |
| 144 |
query: "city1 city2 state1 state2 keyword1 keyword2 keyword3", |
| 145 |
should_match: false, |
| 146 |
}, |
| 147 |
), |
| 148 |
( |
| 149 |
"query-weather-no-match-long-2", |
| 150 |
QueryBenchmark { |
| 151 |
provider: SuggestionProvider::Weather, |
| 152 |
query: "this does not match anything especially not a weather suggestion but nevertheless it is a very long query which as previously mentioned doesn't match anything at all", |
| 153 |
should_match: false, |
| 154 |
}, |
| 155 |
), |
| 156 |
( |
| 157 |
"query-weather-no-match-keyword-prefix", |
| 158 |
QueryBenchmark { |
| 159 |
provider: SuggestionProvider::Weather, |
| 160 |
query: "wea", |
| 161 |
should_match: false, |
| 162 |
}, |
| 163 |
), |
| 164 |
( |
| 165 |
"query-weather-no-match-city-abbr", |
| 166 |
QueryBenchmark { |
| 167 |
provider: SuggestionProvider::Weather, |
| 168 |
query: "ny", |
| 169 |
should_match: false, |
| 170 |
}, |
| 171 |
), |
| 172 |
( |
| 173 |
"query-weather-no-match-airport-code", |
| 174 |
QueryBenchmark { |
| 175 |
provider: SuggestionProvider::Weather, |
| 176 |
query: "pdx", |
| 177 |
should_match: false, |
| 178 |
}, |
| 179 |
), |
| 180 |
( |
| 181 |
"query-weather-no-match-airport-code-region", |
| 182 |
QueryBenchmark { |
| 183 |
provider: SuggestionProvider::Weather, |
| 184 |
query: "pdx or", |
| 185 |
should_match: false, |
| 186 |
}, |
| 187 |
), |
| 188 |
|
| 189 |
// weather: keyword only |
| 190 |
( |
| 191 |
"query-weather-keyword-only", |
| 192 |
QueryBenchmark { |
| 193 |
provider: SuggestionProvider::Weather, |
| 194 |
query: "weather", |
| 195 |
should_match: true, |
| 196 |
}, |
| 197 |
), |
| 198 |
|
| 199 |
// weather: city only |
| 200 |
( |
| 201 |
"query-weather-city-only", |
| 202 |
QueryBenchmark { |
| 203 |
provider: SuggestionProvider::Weather, |
| 204 |
query: "new york", |
| 205 |
should_match: true, |
| 206 |
}, |
| 207 |
), |
| 208 |
|
| 209 |
// weather: city + region |
| 210 |
( |
| 211 |
"query-weather-city-region-los-angeles-c", |
| 212 |
QueryBenchmark { |
| 213 |
provider: SuggestionProvider::Weather, |
| 214 |
query: "los angeles c", |
| 215 |
should_match: true, |
| 216 |
}, |
| 217 |
), |
| 218 |
( |
| 219 |
"query-weather-city-region-los-angeles-ca", |
| 220 |
QueryBenchmark { |
| 221 |
provider: SuggestionProvider::Weather, |
| 222 |
query: "los angeles ca", |
| 223 |
should_match: true, |
| 224 |
}, |
| 225 |
), |
| 226 |
( |
| 227 |
"query-weather-city-region-la-ca", |
| 228 |
QueryBenchmark { |
| 229 |
provider: SuggestionProvider::Weather, |
| 230 |
query: "la ca", |
| 231 |
should_match: true, |
| 232 |
}, |
| 233 |
), |
| 234 |
( |
| 235 |
"query-weather-city-region-ny-ny", |
| 236 |
QueryBenchmark { |
| 237 |
provider: SuggestionProvider::Weather, |
| 238 |
query: "ny ny", |
| 239 |
should_match: true, |
| 240 |
}, |
| 241 |
), |
| 242 |
|
| 243 |
// weather: keyword + city |
| 244 |
( |
| 245 |
"query-weather-keyword-city-n", |
| 246 |
QueryBenchmark { |
| 247 |
provider: SuggestionProvider::Weather, |
| 248 |
query: "weather n", |
| 249 |
should_match: true, |
| 250 |
}, |
| 251 |
), |
| 252 |
( |
| 253 |
"query-weather-keyword-city-ne", |
| 254 |
QueryBenchmark { |
| 255 |
provider: SuggestionProvider::Weather, |
| 256 |
query: "weather ne", |
| 257 |
should_match: true, |
| 258 |
}, |
| 259 |
), |
| 260 |
( |
| 261 |
"query-weather-keyword-city-new", |
| 262 |
QueryBenchmark { |
| 263 |
provider: SuggestionProvider::Weather, |
| 264 |
query: "weather new", |
| 265 |
should_match: true, |
| 266 |
}, |
| 267 |
), |
| 268 |
( |
| 269 |
"query-weather-keyword-city-new-york", |
| 270 |
QueryBenchmark { |
| 271 |
provider: SuggestionProvider::Weather, |
| 272 |
query: "weather new york", |
| 273 |
should_match: true, |
| 274 |
}, |
| 275 |
), |
| 276 |
( |
| 277 |
"query-weather-keyword-city-ny", |
| 278 |
QueryBenchmark { |
| 279 |
provider: SuggestionProvider::Weather, |
| 280 |
query: "weather ny", |
| 281 |
should_match: true, |
| 282 |
}, |
| 283 |
), |
| 284 |
( |
| 285 |
"query-weather-keyword-city-pdx", |
| 286 |
QueryBenchmark { |
| 287 |
provider: SuggestionProvider::Weather, |
| 288 |
query: "weather pdx", |
| 289 |
should_match: true, |
| 290 |
}, |
| 291 |
), |
| 292 |
|
| 293 |
// weather: keyword + city + region |
| 294 |
( |
| 295 |
"query-weather-keyword-city-region-los-angeles-c", |
| 296 |
QueryBenchmark { |
| 297 |
provider: SuggestionProvider::Weather, |
| 298 |
query: "weather los angeles c", |
| 299 |
should_match: true, |
| 300 |
}, |
| 301 |
), |
| 302 |
( |
| 303 |
"query-weather-keyword-city-region-los-angeles-ca", |
| 304 |
QueryBenchmark { |
| 305 |
provider: SuggestionProvider::Weather, |
| 306 |
query: "weather los angeles ca", |
| 307 |
should_match: true, |
| 308 |
}, |
| 309 |
), |
| 310 |
( |
| 311 |
"query-weather-keyword-city-region-la-ca", |
| 312 |
QueryBenchmark { |
| 313 |
provider: SuggestionProvider::Weather, |
| 314 |
query: "weather la ca", |
| 315 |
should_match: true, |
| 316 |
}, |
| 317 |
), |
| 318 |
( |
| 319 |
"query-weather-keyword-city-region-ny-ny", |
| 320 |
QueryBenchmark { |
| 321 |
provider: SuggestionProvider::Weather, |
| 322 |
query: "weather ny ny", |
| 323 |
should_match: true, |
| 324 |
}, |
| 325 |
), |
| 326 |
( |
| 327 |
"query-weather-keyword-city-region-pdx-or", |
| 328 |
QueryBenchmark { |
| 329 |
provider: SuggestionProvider::Weather, |
| 330 |
query: "weather pdx or", |
| 331 |
should_match: true, |
| 332 |
}, |
| 333 |
), |
| 334 |
|
| 335 |
// weather: city + keyword |
| 336 |
( |
| 337 |
"query-weather-city-keyword-new-york-w", |
| 338 |
QueryBenchmark { |
| 339 |
provider: SuggestionProvider::Weather, |
| 340 |
query: "new york w", |
| 341 |
should_match: true, |
| 342 |
}, |
| 343 |
), |
| 344 |
( |
| 345 |
"query-weather-city-keyword-new-york-we", |
| 346 |
QueryBenchmark { |
| 347 |
provider: SuggestionProvider::Weather, |
| 348 |
query: "new york we", |
| 349 |
should_match: true, |
| 350 |
}, |
| 351 |
), |
| 352 |
( |
| 353 |
"query-weather-city-keyword-new-york-wea", |
| 354 |
QueryBenchmark { |
| 355 |
provider: SuggestionProvider::Weather, |
| 356 |
query: "new york wea", |
| 357 |
should_match: true, |
| 358 |
}, |
| 359 |
), |
| 360 |
( |
| 361 |
"query-weather-city-keyword-new-york-weather", |
| 362 |
QueryBenchmark { |
| 363 |
provider: SuggestionProvider::Weather, |
| 364 |
query: "new york weather", |
| 365 |
should_match: true, |
| 366 |
}, |
| 367 |
), |
| 368 |
( |
| 369 |
"query-weather-city-keyword-ny-w", |
| 370 |
QueryBenchmark { |
| 371 |
provider: SuggestionProvider::Weather, |
| 372 |
query: "ny w", |
| 373 |
should_match: true, |
| 374 |
}, |
| 375 |
), |
| 376 |
( |
| 377 |
"query-weather-city-keyword-ny-weather", |
| 378 |
QueryBenchmark { |
| 379 |
provider: SuggestionProvider::Weather, |
| 380 |
query: "ny weather", |
| 381 |
should_match: true, |
| 382 |
}, |
| 383 |
), |
| 384 |
|
| 385 |
// weather: city + region + keyword |
| 386 |
( |
| 387 |
"query-weather-city-region-keyword-los-angeles-w", |
| 388 |
QueryBenchmark { |
| 389 |
provider: SuggestionProvider::Weather, |
| 390 |
query: "los angeles ca w", |
| 391 |
should_match: true, |
| 392 |
}, |
| 393 |
), |
| 394 |
( |
| 395 |
"query-weather-city-region-keyword-los-angeles-we", |
| 396 |
QueryBenchmark { |
| 397 |
provider: SuggestionProvider::Weather, |
| 398 |
query: "los angeles ca we", |
| 399 |
should_match: true, |
| 400 |
}, |
| 401 |
), |
| 402 |
( |
| 403 |
"query-weather-city-region-keyword-los-angeles-wea", |
| 404 |
QueryBenchmark { |
| 405 |
provider: SuggestionProvider::Weather, |
| 406 |
query: "los angeles ca wea", |
| 407 |
should_match: true, |
| 408 |
}, |
| 409 |
), |
| 410 |
( |
| 411 |
"query-weather-city-region-keyword-los-angeles-weather", |
| 412 |
QueryBenchmark { |
| 413 |
provider: SuggestionProvider::Weather, |
| 414 |
query: "los angeles ca weather", |
| 415 |
should_match: true, |
| 416 |
}, |
| 417 |
), |
| 418 |
( |
| 419 |
"query-weather-city-region-keyword-la-ca-weather", |
| 420 |
QueryBenchmark { |
| 421 |
provider: SuggestionProvider::Weather, |
| 422 |
query: "la ca weather", |
| 423 |
should_match: true, |
| 424 |
}, |
| 425 |
), |
| 426 |
( |
| 427 |
"query-weather-city-region-keyword-ny-ny-weather", |
| 428 |
QueryBenchmark { |
| 429 |
provider: SuggestionProvider::Weather, |
| 430 |
query: "ny ny weather", |
| 431 |
should_match: true, |
| 432 |
}, |
| 433 |
), |
| 434 |
] |
| 435 |
} |