Add initial benchmark setup

This adds all the bootstrapping to quickly add benchmarks for pieces afterwards. Instructions for running added to the README.
This commit is contained in:
Nick Craver
2018-02-01 21:24:08 -05:00
parent a4825de77e
commit 38d7faeda9
6 changed files with 94 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net462</TargetFrameworks>
<Configuration>Release</Configuration>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Ben.Demystifier\Ben.Demystifier.csproj" />
<PackageReference Include="BenchmarkDotNet" Version="0.10.12" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,18 @@
using System;
using System.Diagnostics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Jobs;
namespace Ben.Demystifier.Benchmarks
{
[ClrJob, CoreJob]
[Config(typeof(Config))]
public class ExceptionTests
{
[Benchmark(Baseline = true, Description = ".ToString()")]
public string Baseline() => new Exception().ToString();
[Benchmark(Description = "Demystify().ToString()")]
public string Demystify() => new Exception().Demystify().ToString();
}
}

View File

@@ -0,0 +1,48 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using System;
using System.Linq;
using System.Reflection;
namespace Ben.Demystifier.Benchmarks
{
public static class Program
{
private const string BenchmarkSuffix = "Tests";
public static void Main(string[] args)
{
var benchmarks = Assembly.GetEntryAssembly()
.DefinedTypes.Where(t => t.Name.EndsWith(BenchmarkSuffix))
.ToDictionary(t => t.Name.Substring(0, t.Name.Length - BenchmarkSuffix.Length), t => t, StringComparer.OrdinalIgnoreCase);
if (args.Length > 0 && args[0].Equals("all", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Running full benchmarks suite");
benchmarks.Select(pair => pair.Value).ToList().ForEach(action => BenchmarkRunner.Run(action));
return;
}
if (args.Length == 0 || !benchmarks.ContainsKey(args[0]))
{
Console.WriteLine("Please, select benchmark, list of available:");
benchmarks
.Select(pair => pair.Key)
.ToList()
.ForEach(Console.WriteLine);
Console.WriteLine("All");
return;
}
BenchmarkRunner.Run(benchmarks[args[0]]);
Console.Read();
}
}
internal class Config : ManualConfig
{
public Config() => Add(new MemoryDiagnoser());
}
}