use super::common::*;

pub(crate) fn bench_file_diff_syntax_prepare(c: &mut Criterion) {
    let lines = env_usize("GITCOMET_BENCH_FILE_DIFF_SYNTAX_LINES", 4_000);
    let line_bytes = env_usize("GITCOMET_BENCH_FILE_DIFF_SYNTAX_LINE_BYTES", 128);
    let fixture = FileDiffSyntaxPrepareFixture::new_json_proxy(lines, line_bytes);
    fixture.prewarm();

    let mut group = c.benchmark_group("file_diff_syntax_prepare");
    group.sample_size(10);
    group.warm_up_time(Duration::from_secs(1));

    let mut cold_nonce = 0u64;
    group.bench_function(
        BenchmarkId::from_parameter("file_diff_syntax_prepare_cold"),
        |b| {
            b.iter_batched_ref(
                || {
                    cold_nonce = cold_nonce.wrapping_add(1);
                    fixture.prepare_cold_source(cold_nonce)
                },
                |source| fixture.run_prepare_cold_from_source(source),
                BatchSize::PerIteration,
            )
        },
    );
    group.bench_function(
        BenchmarkId::from_parameter("file_diff_syntax_prepare_warm"),
        |b| b.iter(|| fixture.run_prepare_warm()),
    );
    group.finish();

    let cold_bench = "file_diff_syntax_prepare/file_diff_syntax_prepare_cold";
    let warm_bench = "file_diff_syntax_prepare/file_diff_syntax_prepare_warm";
    cold_nonce = cold_nonce.wrapping_add(1);
    let cold_source = fixture.prepare_cold_source(cold_nonce);
    if measure_sidecar_allocations_if_selected(cold_bench, || {
        fixture.run_prepare_cold_from_source(&cold_source)
    })
    .is_some()
    {
        emit_allocation_only_sidecar(cold_bench);
    }
    if measure_sidecar_allocations_if_selected(warm_bench, || fixture.run_prepare_warm()).is_some()
    {
        emit_allocation_only_sidecar(warm_bench);
    }
}
