nvzqz / divan

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

feature request: clean up closure #57

Open git-girl opened 1 month ago

git-girl commented 1 month ago

hey there :3

thanks for provding this library, so far i've really enjoyed using it :> i really hope i didn't miss anything that this already exists sometimes i am a bit very silly when it comes to properly reading the docs.

request

adding an an optional closure that can be passed to a Bencher that is called after each bench run through but not counted towards the measurment time. (like i guess smth like bencher.with_cleanup(closure) that then runs somewhere at the end of bench_loop_threaded {hope i understood the code correctly there})

motivation

i am looking into benching some sql queries and i wanted to run DISCARD after every bench run so as to clear the caches a bit. and i think this is a general thing of wanting to clear a cache is something that comes up often in benching.

capacities on my end

i am doing this as a free time thing so i have time to look into this and can also imagine writing a pr. allthough i am not certain my code quality would be up to par but i mean i can try.

best regards

nvzqz commented 4 weeks ago

You can accomplish this today with Drop by returning something from the benchmark, and it will run for every iteration. But it will be after the timing, so it depends on how caches are being used if multiple iterations are included in the sample. You might want to limit it with #[divan::bench(sample_size = 1)].

To properly add this feature, it shouldn't be hard to implement by internally using the DeferStore mechanism for handling Drop.

git-girl commented 1 week ago

hey thank you for your reply and sorry for this late reply! i did try out the implementation via Drop and that felt like a nice api to use allthough i didn't figure out getting around the postgres cache even with the sample_size=1. i think i am gonna put this postgres endeavour to the side for now. i will try to find some time to try some things out with this. but i may be rather slow to get to work on it.