dotnet / BenchmarkDotNet

Powerful .NET library for benchmarking
https://benchmarkdotnet.org
MIT License
10.42k stars 958 forks source link

Allow a [Cleanup] method to be specified #215

Closed mattwarren closed 8 years ago

mattwarren commented 8 years ago

To mirror the invocations of the [Setup] method, i.e.

This is useful when there is a resource that needs cleaning up after each set of iterations

adamsitnik commented 8 years ago

Good idea! I would also Dispose the object if it is IDisposable (at the end)

mattwarren commented 8 years ago

@adamsitnik I'm not following, which object do you mean?

adamsitnik commented 8 years ago

@mattwarren the one that is implementing Benchmark method(s)

AndreyAkinshin commented 8 years ago

@mattwarren, can you write an example of benchmark where we need a cleanup method?

mattwarren commented 8 years ago

This idea came from this tweet, but I'm not really sure of the exact scenario, I'll try and find out.

DenisIstomin commented 8 years ago

I think that Aaronontheweb (as a contributor to NBench) at his tweet said about "Cleanup" logic after each iteration. But BenchmarkDotNet uses "Setup" attribute for set of iterations called "Launch" (I hope so). That's why I think that "Cleanup" attribute could be used at benchmarks that do some work with "out of the process" resources - filesystem, for example.

AndreyAkinshin commented 8 years ago

It makes sense, but I still want to have a real use-case for cleanup.

nblumhardt commented 8 years ago

Hi all - thanks for the great project!

We're using BenchmarkDotNet to profile a new Serilog "async" sink, and we need to:

...after running a benchmark. Without [Cleanup] it's hard to do this properly.

Does this cover the scenario information you need to consider including this feature?

Thanks!

nblumhardt commented 8 years ago

Ah, now I see the commit above - great! Sorry about the noise :-)

adamsitnik commented 8 years ago

@nblumhardt If you don't want to wait for 0.9.9 release you can download the pre-release package from our development feed <add key="appveyor-bdn" value="https://ci.appveyor.com/nuget/benchmarkdotnet" />

If that's not an option you can add a finalizer to your benchmark class. But personally I don't like this workaround.