Created
September 15, 2016 14:34
-
-
Save jcansdale/e40c894c01fb67ddc9431d02b3d82cca to your computer and use it in GitHub Desktop.
How to quickly preview benchmarks using TestDriven.Net.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add the `BenchmarkDotNet.TestDriven` NuGet package. | |
using System; | |
using System.Reflection; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.TestDriven; | |
// Let TestDriven.Net know how to run benchmarks. | |
[assembly: BenchmarkTestRunner(typeof(FastAndDirtyConfig))] | |
namespace BenchTests | |
{ | |
// You can `Run Test(s)` on any benchmark class or method. | |
public class DelegateBenchmark | |
{ | |
MethodInfo method; | |
[Setup] | |
public void Setup() | |
{ | |
method = typeof(DelegateBenchmark).GetMethod(nameof(TargetMethod)); | |
} | |
public static void TargetMethod() { } | |
[Benchmark] | |
public object CreateDelegate() | |
{ | |
return method.CreateDelegate(typeof(Action)).DynamicInvoke(); | |
} | |
[Benchmark] | |
public object ProxyDelegate() | |
{ | |
return new Action(new ProxyMethod(method).Invoke).DynamicInvoke(); | |
} | |
class ProxyMethod | |
{ | |
MethodInfo method; | |
internal ProxyMethod(MethodInfo method) | |
{ | |
this.method = method; | |
} | |
internal void Invoke() | |
{ | |
method.Invoke(null, null); | |
} | |
} | |
} | |
} | |
// Tweet me if you find any issues! @jcansdale |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment