nvzqz / divan

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

Sleeping in `Bencher::with_inputs` affects benchmark time #55

Closed OliverKillane closed 1 month ago

OliverKillane commented 1 month ago

Description

with_inputs affects the benchmark time with sleep, the documentation specifies it does not.

Replicate

Example here

nvzqz commented 1 month ago

Your benchmarks are measuring dropping Vec inputs because they use bench_values. Switching to bench_refs fixes the timings by deferring drop until after the loop.

Not sure why sleeping affects timings though.

OliverKillane commented 1 month ago

D'oh, forgot the drop. That fixes it for me - much appreciated!

Is the with_sleep worth pursuing, or should we close this issue?

nvzqz commented 1 month ago

I think sleeping affecting timing is a legit issue.

OliverKillane commented 1 month ago

I have fixed the linked example, with a simpler mm_pause for busy sleeping. I need to sleep now.

nvzqz commented 1 month ago

I see the issue here. You're setting max_time = 0.1 which also takes into account input generation time.

Internally it's hitting max_time because the benchmark loop needs more iterations but then with_inputs spends more of the time budget. This causes with_sleep to only have enough time to record a single sample of 1, 8, or 64 iterations. To get accurate timings it needs to record multiple samples over more iterations.

Setting DIVAN_SAMPLE_SIZE=100 (instead of dynamic sample size) shows that sleeping in input generation doesn't affect timings.