Created
March 17, 2016 09:14
-
-
Save ig-sinicyn/f75c982913409e8d9567 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Linq; | |
using System.Threading; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Configs; | |
using BenchmarkDotNet.Extensions; | |
using BenchmarkDotNet.Jobs; | |
using BenchmarkDotNet.Loggers; | |
using BenchmarkDotNet.Reports; | |
using BenchmarkDotNet.Running; | |
using NUnit.Framework; | |
namespace CodeDrafts.PerfTests | |
{ | |
public class SingleRunFastConfig : ManualConfig | |
{ | |
public SingleRunFastConfig() | |
{ | |
Add(Job.Dry); | |
} | |
} | |
[Config(typeof(SingleRunFastConfig))] | |
public class PerfAssertions | |
{ | |
// BASEDON: https://github.com/PerfDotNet/BenchmarkDotNet/blob/master/BenchmarkDotNet.IntegrationTests/PerformanceUnitTest.cs | |
/// <summary> | |
/// Trying make it work | |
/// </summary> | |
[Test] | |
public void Test() | |
{ | |
var logger = new AccumulationLogger(); | |
var config = DefaultConfig.Instance.With(logger); | |
var summary = BenchmarkRunner.Run<PerfAssertions>(config); | |
// Sanity checks, to be sure that the different benchmarks actually run | |
var testOutput = logger.GetLog(); | |
Assert.That(testOutput, Does.Contain("// ### Slow Benchmark called ###" + Environment.NewLine)); | |
Assert.That(testOutput, Does.Contain("// ### Fast Benchmark called ###" + Environment.NewLine)); | |
// Check that slow benchmark is actually slower than the fast benchmark! | |
var slowBenchmarkRun = summary.GetRunsFor<PerfAssertions>(r => r.SlowBenchmark()).First(); | |
var fastBenchmarkRun = summary.GetRunsFor<PerfAssertions>(r => r.FastBenchmark()).First(); | |
Assert.True( | |
slowBenchmarkRun.GetAverageNanoseconds() > fastBenchmarkRun.GetAverageNanoseconds(), | |
string.Format("Expected SlowBenchmark: {0:N2} ns to be MORE than FastBenchmark: {1:N2} ns", | |
slowBenchmarkRun.GetAverageNanoseconds(), fastBenchmarkRun.GetAverageNanoseconds())); | |
Assert.True( | |
slowBenchmarkRun.GetOpsPerSecond() < fastBenchmarkRun.GetOpsPerSecond(), | |
string.Format("Expected SlowBenchmark: {0:N2} Ops to be LESS than FastBenchmark: {1:N2} Ops", | |
slowBenchmarkRun.GetOpsPerSecond(), fastBenchmarkRun.GetOpsPerSecond())); | |
// Whilst we're at it, let's do more specific Asserts as we know what the elasped time should be | |
var slowBenchmarkReport = summary.GetReportFor<PerfAssertions>(r => r.SlowBenchmark()); | |
var fastBenchmarkReport = summary.GetReportFor<PerfAssertions>(r => r.FastBenchmark()); | |
foreach (var slowRun in slowBenchmarkReport.GetResultRuns()) | |
Assert.That(slowRun.GetAverageNanoseconds() / 1000.0 / 1000.0, Is.InRange(98, 102)); | |
foreach (var fastRun in fastBenchmarkReport.GetResultRuns()) | |
Assert.That(fastRun.GetAverageNanoseconds() / 1000.0 / 1000.0, Is.InRange(14, 17)); | |
} | |
[Benchmark] | |
public void FastBenchmark() | |
{ | |
Console.WriteLine("// ### Fast Benchmark called ###"); | |
Thread.Sleep(15); | |
} | |
[Benchmark] | |
public void SlowBenchmark() | |
{ | |
Console.WriteLine("// ### Slow Benchmark called ###"); | |
Thread.Sleep(100); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment