|
| 1 | +use criterion::{Criterion, criterion_group, criterion_main}; |
| 2 | +use itertools::Itertools; |
| 3 | +use rand::Rng; |
| 4 | +use rand::distr::{Alphabetic, Distribution}; |
| 5 | +use rand::rngs::ThreadRng; |
| 6 | +use std::path::PathBuf; |
| 7 | + |
| 8 | +const N_PATHS: usize = 30; |
| 9 | + |
| 10 | +fn generate_paths() -> Vec<PathBuf> { |
| 11 | + const PATH_COMPONENTS_MAX: usize = 10; |
| 12 | + const PATH_COMPONENT_LEN: usize = 2; |
| 13 | + |
| 14 | + // Create a single path component -- random lowercase characters, |
| 15 | + // length of PATH_COMPONENT_LEN |
| 16 | + fn make_path_component(rng: &mut ThreadRng) -> String { |
| 17 | + (0..PATH_COMPONENT_LEN) |
| 18 | + .map(|_| { |
| 19 | + let ch = Alphabetic.sample(rng) as char; |
| 20 | + ch.to_ascii_lowercase() |
| 21 | + }) |
| 22 | + .collect() |
| 23 | + } |
| 24 | + |
| 25 | + // Create a single path -- anywhere from 1 to PATH_COMPONENTS_MAX components |
| 26 | + fn make_path(rng: &mut ThreadRng) -> PathBuf { |
| 27 | + let num_components = rng.random_range(1..=PATH_COMPONENTS_MAX); |
| 28 | + let mut path = PathBuf::new(); |
| 29 | + |
| 30 | + for _ in 0..num_components { |
| 31 | + path.push(make_path_component(rng)) |
| 32 | + } |
| 33 | + |
| 34 | + path |
| 35 | + } |
| 36 | + |
| 37 | + let mut rng = rand::rng(); |
| 38 | + |
| 39 | + // Finally, create N_PATHS of random paths |
| 40 | + (0..N_PATHS).map(|_| make_path(&mut rng)).collect() |
| 41 | +} |
| 42 | + |
| 43 | +fn ultrawide(c: &mut Criterion) { |
| 44 | + c.bench_function("ultrawide_filter_parse", |b| { |
| 45 | + b.iter_with_setup_wrapper(|runner| { |
| 46 | + let filter = generate_paths() |
| 47 | + .into_iter() |
| 48 | + .map(|p| { |
| 49 | + let p = p.display().to_string(); |
| 50 | + format!("::{p}/") |
| 51 | + }) |
| 52 | + .join(","); |
| 53 | + |
| 54 | + let filter = format!(":[{}]", filter); |
| 55 | + |
| 56 | + runner.run(move || { |
| 57 | + let filter = josh_core::filter::parse(&filter).expect("failed to parse"); |
| 58 | + std::hint::black_box(filter); |
| 59 | + }) |
| 60 | + }); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +criterion_group!(benches, ultrawide); |
| 65 | +criterion_main!(benches); |
0 commit comments