dotnet / BenchmarkDotNet

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

[Question] Is there a guide or code example for benchmarking programmatically? #1445

Closed tebjan closed 4 years ago

tebjan commented 4 years ago

Is there a guide or code example for benchmarking a method programmatically? Similar to this, but including adding some user method to test: https://github.com/dotnet/BenchmarkDotNet#simplicity

The reason I'm asking is that I'm working in a live-programming environment that doesn't have support for attributes for static compilation/reflection: https://visualprogramming.net/

So I have to set up the benchmark by code only at runtime by doing something like this:

benchmarknet

adamsitnik commented 4 years ago

Hi @tebjan

We don't really have a good alternative to what you have described (finding public methods with benchmark attribute etc).

We don't offer passing lambdas etc. The only thing that comes to my mind is using this method:

https://github.com/dotnet/BenchmarkDotNet/blob/be769f8e5012cdd818f2af7c6c9d9f18872c054a/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs#L34

but it still requires you to use reflection to find the right methods and it requires a public type to be passed as well.

I am going to close this issue as this is a by-design limitation and we can't change it.

Please feel free to ask more questions and reopen the issue.

Thanks, Adam

tebjan commented 4 years ago

That's very helpful. Thanks for the info, I'll try with this static Run method and see what's possible.