suggest/benchmarks/
query.rs

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
5use crate::{
6    benchmarks::{new_store, BenchmarkWithInput},
7    SuggestStore, SuggestionProvider, SuggestionQuery,
8};
9
10pub struct QueryBenchmark {
11    provider: SuggestionProvider,
12    query: &'static str,
13    should_match: bool,
14}
15
16pub struct IterationInput {
17    query: SuggestionQuery,
18    should_match_message: String,
19}
20
21impl 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
59pub fn all_benchmarks() -> Vec<(&'static str, QueryBenchmark)> {
60    vec![
61        // The query code will only do a prefix match if the total input length is > 3 chars.
62
63        // weather: no matches
64        (
65            "query-weather-no-match-1",
66            QueryBenchmark {
67                provider: SuggestionProvider::Weather,
68                query: "nomatch",
69                should_match: false,
70            },
71        ),
72        (
73            "query-weather-no-match-2",
74            QueryBenchmark {
75                provider: SuggestionProvider::Weather,
76                query: "no match",
77                should_match: false,
78            },
79        ),
80        (
81            "query-weather-no-match-3",
82            QueryBenchmark {
83                provider: SuggestionProvider::Weather,
84                query: "no match either",
85                should_match: false,
86            },
87        ),
88        (
89            "query-weather-no-match-long-1",
90            QueryBenchmark {
91                provider: SuggestionProvider::Weather,
92                query: "city1 city2 state1 state2 keyword1 keyword2 keyword3",
93                should_match: false,
94            },
95        ),
96        (
97            "query-weather-no-match-long-2",
98            QueryBenchmark {
99                provider: SuggestionProvider::Weather,
100                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",
101                should_match: false,
102            },
103        ),
104        (
105            "query-weather-no-match-keyword-prefix",
106            QueryBenchmark {
107                provider: SuggestionProvider::Weather,
108                query: "wea",
109                should_match: false,
110            },
111        ),
112        (
113            "query-weather-no-match-city-abbr",
114            QueryBenchmark {
115                provider: SuggestionProvider::Weather,
116                query: "ny",
117                should_match: false,
118            },
119        ),
120        (
121            "query-weather-no-match-airport-code",
122            QueryBenchmark {
123                provider: SuggestionProvider::Weather,
124                query: "pdx",
125                should_match: false,
126            },
127        ),
128        (
129            "query-weather-no-match-airport-code-region",
130            QueryBenchmark {
131                provider: SuggestionProvider::Weather,
132                query: "pdx or",
133                should_match: false,
134            },
135        ),
136
137        // weather: keyword only
138        (
139            "query-weather-keyword-only",
140            QueryBenchmark {
141                provider: SuggestionProvider::Weather,
142                query: "weather",
143                should_match: true,
144            },
145        ),
146
147        // weather: city only
148        (
149            "query-weather-city-only",
150            QueryBenchmark {
151                provider: SuggestionProvider::Weather,
152                query: "new york",
153                should_match: true,
154            },
155        ),
156
157        // weather: city + region
158        (
159            "query-weather-city-region-los-angeles-c",
160            QueryBenchmark {
161                provider: SuggestionProvider::Weather,
162                query: "los angeles c",
163                should_match: true,
164            },
165        ),
166        (
167            "query-weather-city-region-los-angeles-ca",
168            QueryBenchmark {
169                provider: SuggestionProvider::Weather,
170                query: "los angeles ca",
171                should_match: true,
172            },
173        ),
174        (
175            "query-weather-city-region-la-ca",
176            QueryBenchmark {
177                provider: SuggestionProvider::Weather,
178                query: "la ca",
179                should_match: true,
180            },
181        ),
182        (
183            "query-weather-city-region-ny-ny",
184            QueryBenchmark {
185                provider: SuggestionProvider::Weather,
186                query: "ny ny",
187                should_match: true,
188            },
189        ),
190
191        // weather: keyword + city
192        (
193            "query-weather-keyword-city-n",
194            QueryBenchmark {
195                provider: SuggestionProvider::Weather,
196                query: "weather n",
197                should_match: true,
198            },
199        ),
200        (
201            "query-weather-keyword-city-ne",
202            QueryBenchmark {
203                provider: SuggestionProvider::Weather,
204                query: "weather ne",
205                should_match: true,
206            },
207        ),
208        (
209            "query-weather-keyword-city-new",
210            QueryBenchmark {
211                provider: SuggestionProvider::Weather,
212                query: "weather new",
213                should_match: true,
214            },
215        ),
216        (
217            "query-weather-keyword-city-new-york",
218            QueryBenchmark {
219                provider: SuggestionProvider::Weather,
220                query: "weather new york",
221                should_match: true,
222            },
223        ),
224        (
225            "query-weather-keyword-city-ny",
226            QueryBenchmark {
227                provider: SuggestionProvider::Weather,
228                query: "weather ny",
229                should_match: true,
230            },
231        ),
232        (
233            "query-weather-keyword-city-pdx",
234            QueryBenchmark {
235                provider: SuggestionProvider::Weather,
236                query: "weather pdx",
237                should_match: true,
238            },
239        ),
240
241        // weather: keyword + city + region
242        (
243            "query-weather-keyword-city-region-los-angeles-c",
244            QueryBenchmark {
245                provider: SuggestionProvider::Weather,
246                query: "weather los angeles c",
247                should_match: true,
248            },
249        ),
250        (
251            "query-weather-keyword-city-region-los-angeles-ca",
252            QueryBenchmark {
253                provider: SuggestionProvider::Weather,
254                query: "weather los angeles ca",
255                should_match: true,
256            },
257        ),
258        (
259            "query-weather-keyword-city-region-la-ca",
260            QueryBenchmark {
261                provider: SuggestionProvider::Weather,
262                query: "weather la ca",
263                should_match: true,
264            },
265        ),
266        (
267            "query-weather-keyword-city-region-ny-ny",
268            QueryBenchmark {
269                provider: SuggestionProvider::Weather,
270                query: "weather ny ny",
271                should_match: true,
272            },
273        ),
274        (
275            "query-weather-keyword-city-region-pdx-or",
276            QueryBenchmark {
277                provider: SuggestionProvider::Weather,
278                query: "weather pdx or",
279                should_match: true,
280            },
281        ),
282
283        // weather: city + keyword
284        (
285            "query-weather-city-keyword-new-york-w",
286            QueryBenchmark {
287                provider: SuggestionProvider::Weather,
288                query: "new york w",
289                should_match: true,
290            },
291        ),
292        (
293            "query-weather-city-keyword-new-york-we",
294            QueryBenchmark {
295                provider: SuggestionProvider::Weather,
296                query: "new york we",
297                should_match: true,
298            },
299        ),
300        (
301            "query-weather-city-keyword-new-york-wea",
302            QueryBenchmark {
303                provider: SuggestionProvider::Weather,
304                query: "new york wea",
305                should_match: true,
306            },
307        ),
308        (
309            "query-weather-city-keyword-new-york-weather",
310            QueryBenchmark {
311                provider: SuggestionProvider::Weather,
312                query: "new york weather",
313                should_match: true,
314            },
315        ),
316        (
317            "query-weather-city-keyword-ny-w",
318            QueryBenchmark {
319                provider: SuggestionProvider::Weather,
320                query: "ny w",
321                should_match: true,
322            },
323        ),
324        (
325            "query-weather-city-keyword-ny-weather",
326            QueryBenchmark {
327                provider: SuggestionProvider::Weather,
328                query: "ny weather",
329                should_match: true,
330            },
331        ),
332
333        // weather: city + region + keyword
334        (
335            "query-weather-city-region-keyword-los-angeles-w",
336            QueryBenchmark {
337                provider: SuggestionProvider::Weather,
338                query: "los angeles ca w",
339                should_match: true,
340            },
341        ),
342        (
343            "query-weather-city-region-keyword-los-angeles-we",
344            QueryBenchmark {
345                provider: SuggestionProvider::Weather,
346                query: "los angeles ca we",
347                should_match: true,
348            },
349        ),
350        (
351            "query-weather-city-region-keyword-los-angeles-wea",
352            QueryBenchmark {
353                provider: SuggestionProvider::Weather,
354                query: "los angeles ca wea",
355                should_match: true,
356            },
357        ),
358        (
359            "query-weather-city-region-keyword-los-angeles-weather",
360            QueryBenchmark {
361                provider: SuggestionProvider::Weather,
362                query: "los angeles ca weather",
363                should_match: true,
364            },
365        ),
366        (
367            "query-weather-city-region-keyword-la-ca-weather",
368            QueryBenchmark {
369                provider: SuggestionProvider::Weather,
370                query: "la ca weather",
371                should_match: true,
372            },
373        ),
374        (
375            "query-weather-city-region-keyword-ny-ny-weather",
376            QueryBenchmark {
377                provider: SuggestionProvider::Weather,
378                query: "ny ny weather",
379                should_match: true,
380            },
381        ),
382    ]
383}