nvzqz / divan

Fast and simple benchmarking for Rust projects
https://nikolaivazquez.com/blog/divan/
Apache License 2.0
849 stars 24 forks source link

warmup #17

Open FilipAndersson245 opened 8 months ago

FilipAndersson245 commented 8 months ago

Hello, it would be nice if it would be possible to add warmup runs, in my benchmark case the first run is always slower as some data has to be fetched into memory, and it always drags down the "real" lowest benchmark lowest as this is done only once. I would suggest #[divan::bench(warmup=10) or something simular with the default being either zero or a 1.

nvzqz commented 8 months ago

Are you suggesting in this case that Divan would discard the first 10 samples after it finds a proper sample size?

Perhaps we could have something like:

#[divan::bench(
    // Discard at least the first 10 samples.
    warmup_sample_count = 10,
    // Discard samples until 1 second has elapsed.
    // This would use `IntoDuration` like `min_time`.
    warmup_min_time = 1,
)

In the meantime, I recommend moving out the benchmark closure and doing your own custom warmup.

#[divan::bench]
fn my_bench(bencher: divan::Bencher) {
    let work_fn = || {
        // Benchmarked work in here.
    };

    // Warmup:
    for _ in 0..1000 {
        work_fn();
    }

    // Benchmark:
    bencher.bench(work_fn);
}
FilipAndersson245 commented 8 months ago

Are you suggesting in this case that Divan would discard the first 10 samples after it finds a proper sample size?

Perhaps we could have something like:

#[divan::bench(
    // Discard at least the first 10 samples.
    warmup_sample_count = 10,
    // Discard samples until 1 second has elapsed.
    // This would use `IntoDuration` like `min_time`.
    warmup_min_time = 1,
)

Yee something like that would be very nice.

In the meantime, I recommend moving out the benchmark closure and doing your own custom warmup.

#[divan::bench]
fn my_bench(bencher: divan::Bencher) {
    let work_fn = || {
        // Benchmarked work in here.
    };

    // Warmup:
    for _ in 0..1000 {
        work_fn();
    }

    // Benchmark:
    bencher.bench(work_fn);
}

that would work for me, Thanks 👍