Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yannicklamprecht/454756573eb291ccffcbc69442e72f2d to your computer and use it in GitHub Desktop.
Save yannicklamprecht/454756573eb291ccffcbc69442e72f2d to your computer and use it in GitHub Desktop.
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