use std::fmt::Write as _;
use std::hint::black_box;

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

use limb::worktree::parse_porcelain;

fn gen_porcelain(n: usize) -> String {
    let mut s = String::with_capacity(n * 96);
    for i in 0..n {
        let _ = writeln!(s, "worktree /repos/wt{i}");
        let _ = writeln!(s, "HEAD {i:040x}");
        let _ = writeln!(s, "branch refs/heads/wt{i}");
        s.push('\n');
    }
    s
}

fn bench_parse_porcelain(c: &mut Criterion) {
    let mut group = c.benchmark_group("parse_porcelain");
    for n in [10usize, 100, 1000] {
        let input = gen_porcelain(n);
        group.throughput(Throughput::Elements(n as u64));
        group.bench_with_input(BenchmarkId::from_parameter(n), &input, |b, input| {
            b.iter(|| black_box(parse_porcelain(black_box(input.as_str()))));
        });
    }
    group.finish();
}

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