use std::hint::black_box;

use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};

use limb::fuzzy::Fuzzy;

fn gen_haystacks(n: usize) -> Vec<String> {
    (0..n)
        .map(|i| format!("repo{}/feat-{:04x} feat-{:04x}", i % 10, i, i))
        .collect()
}

fn bench_fuzzy_score(c: &mut Criterion) {
    let haystacks = gen_haystacks(1000);
    let mut group = c.benchmark_group("fuzzy_score");
    for n in [10usize, 100, 1000] {
        let subset: Vec<&str> = haystacks.iter().take(n).map(String::as_str).collect();
        group.throughput(Throughput::Elements(n as u64));
        group.bench_with_input(BenchmarkId::from_parameter(n), &subset, |b, subset| {
            let mut f = Fuzzy::new();
            f.set_query("feat");
            b.iter(|| {
                let mut total: u32 = 0;
                for h in subset {
                    if let Some(s) = f.score(black_box(h)) {
                        total = total.wrapping_add(s);
                    }
                }
                black_box(total)
            });
        });
    }
    group.finish();
}

criterion_group!(benches, bench_fuzzy_score);
criterion_main!(benches);
