| 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 https://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use criterion::{criterion_group, measurement::Measurement, BatchSize, BenchmarkGroup, Criterion}; |
| 6 |
use std::sync::Once; |
| 7 |
use suggest::benchmarks::{cleanup, geoname, ingest, query, BenchmarkWithInput}; |
| 8 |
|
| 9 |
pub fn geoname(c: &mut Criterion) { |
| 10 |
setup_viaduct(); |
| 11 |
let group = c.benchmark_group("geoname"); |
| 12 |
run_benchmarks(group, geoname::all_benchmarks()) |
| 13 |
} |
| 14 |
|
| 15 |
pub fn ingest(c: &mut Criterion) { |
| 16 |
setup_viaduct(); |
| 17 |
let mut group = c.benchmark_group("ingest"); |
| 18 |
// This needs to be 10 for now, or else the `ingest-amp-wikipedia` benchmark would take around |
| 19 |
// 100s to run which feels like too long. `ingest-amp-mobile` also would take a around 50s. |
| 20 |
group.sample_size(10); |
| 21 |
run_benchmarks(group, ingest::all_benchmarks()) |
| 22 |
} |
| 23 |
|
| 24 |
pub fn query(c: &mut Criterion) { |
| 25 |
setup_viaduct(); |
| 26 |
let group = c.benchmark_group("query"); |
| 27 |
run_benchmarks(group, query::all_benchmarks()) |
| 28 |
} |
| 29 |
|
| 30 |
fn run_benchmarks<B: BenchmarkWithInput, M: Measurement>( |
| 31 |
mut group: BenchmarkGroup<M>, |
| 32 |
benchmarks: Vec<(&'static str, B)>, |
| 33 |
) { |
| 34 |
for (name, benchmark) in benchmarks { |
| 35 |
let g_input = benchmark.global_input(); |
| 36 |
group.bench_function(name.to_string(), |b| { |
| 37 |
b.iter_batched( |
| 38 |
|| benchmark.iteration_input(), |
| 39 |
|i_input| benchmark.benchmarked_code(&g_input, i_input), |
| 40 |
// See https://docs.rs/criterion/latest/criterion/enum.BatchSize.html#variants for |
| 41 |
// a discussion of this. PerIteration is chosen for these benchmarks because the |
| 42 |
// input holds a database file handle |
| 43 |
BatchSize::PerIteration, |
| 44 |
); |
| 45 |
}); |
| 46 |
} |
| 47 |
group.finish(); |
| 48 |
} |
| 49 |
|
| 50 |
fn setup_viaduct() { |
| 51 |
static INIT: Once = Once::new(); |
| 52 |
INIT.call_once(viaduct_reqwest::use_reqwest_backend); |
| 53 |
} |
| 54 |
|
| 55 |
criterion_group!(benches, geoname, ingest, query); |
| 56 |
|
| 57 |
fn main() { |
| 58 |
benches(); |
| 59 |
criterion::Criterion::default() |
| 60 |
.configure_from_args() |
| 61 |
.final_summary(); |
| 62 |
cleanup(); |
| 63 |
} |