Skip to content

Commit

Permalink
Adding benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogard committed Jan 2, 2020
1 parent 9977853 commit 11e6058
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 7 deletions.
13 changes: 10 additions & 3 deletions MediatR.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2036
# Visual Studio Version 16
VisualStudioVersion = 16.0.29613.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{6267E2ED-942C-497D-BFC9-B3CE0AFC276F}"
EndProject
Expand Down Expand Up @@ -45,7 +45,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.DryIocZero
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.Lamar", "samples\MediatR.Examples.Lamar\MediatR.Examples.Lamar.csproj", "{F014598D-8B85-4D7E-942A-3493107ABE43}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediatR.Examples.PublishStrategies", "samples\MediatR.Examples.PublishStrategies\MediatR.Examples.PublishStrategies.csproj", "{867EBA13-62F4-4525-8F92-B0AD828EE6D4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.PublishStrategies", "samples\MediatR.Examples.PublishStrategies\MediatR.Examples.PublishStrategies.csproj", "{867EBA13-62F4-4525-8F92-B0AD828EE6D4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Benchmarks", "test\MediatR.Benchmarks\MediatR.Benchmarks.csproj", "{1FA62162-F8F1-4CAD-B08E-8DCA603395AD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -113,6 +115,10 @@ Global
{867EBA13-62F4-4525-8F92-B0AD828EE6D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{867EBA13-62F4-4525-8F92-B0AD828EE6D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{867EBA13-62F4-4525-8F92-B0AD828EE6D4}.Release|Any CPU.Build.0 = Release|Any CPU
{1FA62162-F8F1-4CAD-B08E-8DCA603395AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1FA62162-F8F1-4CAD-B08E-8DCA603395AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FA62162-F8F1-4CAD-B08E-8DCA603395AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FA62162-F8F1-4CAD-B08E-8DCA603395AD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -133,6 +139,7 @@ Global
{49AC9726-ECDF-45C0-B5B8-B4AB7F0E6B46} = {95CFF0CD-87A6-4CB6-A99F-42EAD0829E37}
{F014598D-8B85-4D7E-942A-3493107ABE43} = {95CFF0CD-87A6-4CB6-A99F-42EAD0829E37}
{867EBA13-62F4-4525-8F92-B0AD828EE6D4} = {95CFF0CD-87A6-4CB6-A99F-42EAD0829E37}
{1FA62162-F8F1-4CAD-B08E-8DCA603395AD} = {962C5ACA-AB2B-4E9B-9EBB-7E7EE28CDBB1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D58286E3-878B-4ACB-8E76-F61E708D4339}
Expand Down
6 changes: 6 additions & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>MediatR.Examples.AspNetCore</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>MediatR.Examples.AspNetCore</PackageId>
Expand All @@ -13,8 +13,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
``` ini

BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18363
Intel Core i7-7700HQ CPU 2.80GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.1.100
[Host] : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT
DefaultJob : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT


```
| Method | Mean | Error | StdDev |
|------------------------ |----------:|----------:|----------:|
| SendingRequests | 23.748 us | 0.8423 us | 2.2915 us |
| PublishingNotifications | 9.289 us | 0.2322 us | 0.6587 us |
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Method,Job,AnalyzeLaunchVariance,EvaluateOverhead,MaxAbsoluteError,MaxRelativeError,MinInvokeCount,MinIterationTime,OutlierMode,Affinity,EnvironmentVariables,Jit,Platform,Runtime,AllowVeryLargeObjects,Concurrent,CpuGroups,Force,HeapAffinitizeMask,HeapCount,NoAffinitize,RetainVm,Server,PowerPlan,Arguments,BuildConfiguration,Clock,EngineFactory,NuGetReferences,Toolchain,IsMutator,InvocationCount,IterationCount,IterationTime,LaunchCount,MaxIterationCount,MaxWarmupIterationCount,MinIterationCount,MinWarmupIterationCount,RunStrategy,UnrollFactor,WarmupCount,Mean,Error,StdDev
SendingRequests,Default,False,Default,Default,Default,Default,Default,Default,11111111,Empty,RyuJit,X64,Core,False,True,False,True,Default,Default,False,False,False,Default,Default,Default,Default,Default,Default,Default,Default,1,Default,Default,Default,Default,Default,Default,Default,Default,16,Default,23.748 us,0.8423 us,2.2915 us
PublishingNotifications,Default,False,Default,Default,Default,Default,Default,Default,11111111,Empty,RyuJit,X64,Core,False,True,False,True,Default,Default,False,False,False,Default,Default,Default,Default,Default,Default,Default,Default,1,Default,Default,Default,Default,Default,Default,Default,Default,16,Default,9.289 us,0.2322 us,0.6587 us
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>MediatR.Benchmarks.Benchmarks-20200102-124854</title>

<style type="text/css">
table { border-collapse: collapse; display: block; width: 100%; overflow: auto; }
td, th { padding: 6px 13px; border: 1px solid #ddd; }
tr { background-color: #fff; border-top: 1px solid #ccc; }
tr:nth-child(even) { background: #f8f8f8; }
</style>
</head>
<body>
<pre><code>
BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18363
Intel Core i7-7700HQ CPU 2.80GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.1.100
[Host] : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT
DefaultJob : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT
</code></pre>
<pre><code></code></pre>

<table>
<thead><tr><th> Method</th><th>Mean</th><th>Error</th><th>StdDev</th>
</tr>
</thead><tbody><tr><td>SendingRequests</td><td>23.748 us</td><td>0.8423 us</td><td>2.2915 us</td>
</tr><tr><td>PublishingNotifications</td><td>9.289 us</td><td>0.2322 us</td><td>0.6587 us</td>
</tr></tbody></table>
</body>
</html>
50 changes: 50 additions & 0 deletions test/MediatR.Benchmarks/Benchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;
using MediatR.Examples;
using MediatR.Pipeline;
using Microsoft.Extensions.DependencyInjection;

namespace MediatR.Benchmarks
{
[DotTraceDiagnoser]
public class Benchmarks
{
private IMediator _mediator;
private readonly Ping _request = new Ping {Message = "Hello World"};
private readonly Pinged _notification = new Pinged();

[GlobalSetup]
public void GlobalSetup()
{
var services = new ServiceCollection();

services.AddSingleton(TextWriter.Null);

services.AddMediatR(typeof(Ping));

services.AddScoped(typeof(IPipelineBehavior<,>), typeof(GenericPipelineBehavior<,>));
services.AddScoped(typeof(IRequestPreProcessor<>), typeof(GenericRequestPreProcessor<>));
services.AddScoped(typeof(IRequestPostProcessor<,>), typeof(GenericRequestPostProcessor<,>));

var provider = services.BuildServiceProvider();

_mediator = provider.GetRequiredService<IMediator>();
}

[Benchmark]
public Task SendingRequests()
{
return _mediator.Send(_request);
}

[Benchmark]
public Task PublishingNotifications()
{
return _mediator.Publish(_notification);
}
}
}
119 changes: 119 additions & 0 deletions test/MediatR.Benchmarks/DotTraceDiagnoser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using BenchmarkDotNet.Analysers;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Diagnostics.Windows;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Validators;

namespace MediatR.Benchmarks
{
internal sealed class DotTraceDiagnoserAttribute : Attribute, IConfigSource
{
public DotTraceDiagnoserAttribute()
{
var manualConfig = ManualConfig.CreateEmpty();
manualConfig.Add(new DotTraceDiagnoser());
Config = manualConfig;
}

public IConfig Config { get; }
}

internal sealed class DotTraceDiagnoser : IDiagnoser
{
private Process _process;
private string _saveLocation;

public DotTraceDiagnoser()
{
_saveLocation = $"C:\\temp\\MyProject\\{DateTimeOffset.Now.UtcDateTime:yyyyMMddTHHmmss}.bench.dtp";
}

/// <inheritdoc />
public RunMode GetRunMode(BenchmarkCase benchmarkCase) => RunMode.ExtraRun;

/// <inheritdoc />
public void Handle(HostSignal signal, DiagnoserActionParameters parameters)
{
switch (signal)
{
case HostSignal.BeforeActualRun:
try
{
var dotTracePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
@"JetBrains\Installations\dotTrace192\ConsoleProfiler.exe");
var startInfo = new ProcessStartInfo(
dotTracePath,
$"attach {parameters.Process.Id} --save-to={_saveLocation} --profiling-type=Sampling")
{
RedirectStandardError
= true,
RedirectStandardOutput = true,
WindowStyle = ProcessWindowStyle.Normal,
UseShellExecute = false,
};
Console.WriteLine(startInfo.FileName);
Console.WriteLine(startInfo.Arguments);
_process = new Process
{
StartInfo = startInfo
};
_process.ErrorDataReceived += (sender, eventArgs) => Console.Error.WriteLine(eventArgs.Data);
_process.OutputDataReceived += (sender, eventArgs) => Console.WriteLine(eventArgs.Data);
_process.Start();
_process.BeginErrorReadLine();
_process.BeginOutputReadLine();
_process.Exited += (sender, args) => { _process.Dispose(); };
}
catch (Exception e)
{
Console.Error.WriteLine(e.StackTrace);
throw;
}
break;
case HostSignal.AfterActualRun:
break;
case HostSignal.BeforeAnythingElse:
break;
case HostSignal.AfterAll:
break;
case HostSignal.SeparateLogic:
break;
case HostSignal.BeforeProcessStart:
break;
case HostSignal.AfterProcessExit:
break;
default:
throw new ArgumentOutOfRangeException(nameof(signal), signal, null);
}
}

/// <inheritdoc />
public IEnumerable<Metric> ProcessResults(DiagnoserResults results) => Enumerable.Empty<Metric>();

/// <inheritdoc />
public void DisplayResults(ILogger logger) { }

/// <inheritdoc />
public IEnumerable<ValidationError> Validate(ValidationParameters validationParameters) =>
Enumerable.Empty<ValidationError>();

/// <inheritdoc />
public IEnumerable<string> Ids => new[] { nameof(DotTraceDiagnoser) };

/// <inheritdoc />
public IEnumerable<IExporter> Exporters => Enumerable.Empty<IExporter>();

public IEnumerable<IAnalyser> Analysers { get; } = Enumerable.Empty<IAnalyser>();
}
}
23 changes: 23 additions & 0 deletions test/MediatR.Benchmarks/MediatR.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>portable</DebugType>
<DebugSymbols>true</DebugSymbols>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<Configuration>Release</Configuration>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.11.5" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\samples\MediatR.Examples\MediatR.Examples.csproj" />
</ItemGroup>
</Project>
9 changes: 9 additions & 0 deletions test/MediatR.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using BenchmarkDotNet.Running;

namespace MediatR.Benchmarks
{
public class Program
{
public static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
}

0 comments on commit 11e6058

Please sign in to comment.