Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions benchmarks/Mediator.Benchmarks/Mediator.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
<Using Include="BenchmarkDotNet.Attributes" />
<Using Include="BenchmarkDotNet.Order" />
<Using Include="BenchmarkDotNet.Configs" />
<Using Include="BenchmarkDotNet.Jobs" />
<Using Include="BenchmarkDotNet.Diagnosers" />
<Using Include="BenchmarkDotNet.Loggers" />
<Using Include="BenchmarkDotNet.Diagnostics.Windows.Configs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.1" />
<PackageReference Include="MediatR" Version="10.0.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
<PackageReference Include="MessagePipe" Version="1.7.1" />
Expand Down
3 changes: 3 additions & 0 deletions benchmarks/Mediator.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using BenchmarkDotNet.Running;

ConsoleLogger.Default.WriteLine($"Running with lifetime: Impl={Mediator.Mediator.ServiceLifetime}");
ConsoleLogger.Default.WriteLine();

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
45 changes: 41 additions & 4 deletions benchmarks/Mediator.Benchmarks/Request/RequestBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,59 @@ public sealed class SomeHandlerClass :

[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.FastestToSlowest, MethodOrderPolicy.Declared)]
[RankColumn]
//[EventPipeProfiler(EventPipeProfile.CpuSampling)]
//[DisassemblyDiagnoser]
//[InliningDiagnoser(logFailuresOnly: true, allowedNamespaces: new[] { "Mediator" })]
public class RequestBenchmarks
{
private IServiceProvider _serviceProvider;
private IServiceScope _serviceScope;
private IMediator _mediator;
private Mediator _concreteMediator;
private MediatR.IMediator _mediatr;
private IAsyncRequestHandler<SomeRequest, SomeResponse> _messagePipeHandler;
private SomeHandlerClass _handler;
private SomeRequest _request;

[Params(ServiceLifetime.Singleton)]
public ServiceLifetime ServiceLifetime { get; set; }

[GlobalSetup]
public void Setup()
{
var services = new ServiceCollection();
services.AddMediator();
services.AddMediatR(config => config.AsSingleton(), typeof(SomeHandlerClass).Assembly);
services.AddMessagePipe();
services.AddMediator(opts => opts.ServiceLifetime = ServiceLifetime);
services.AddMediatR(opts =>
{
_ = ServiceLifetime switch
{
ServiceLifetime.Singleton => opts.AsSingleton(),
ServiceLifetime.Scoped => opts.AsScoped(),
ServiceLifetime.Transient => opts.AsTransient(),
_ => throw new InvalidOperationException(),
};
}, typeof(SomeHandlerClass).Assembly);
services.AddMessagePipe(opts =>
{
opts.InstanceLifetime = ServiceLifetime switch
{
ServiceLifetime.Singleton => InstanceLifetime.Singleton,
ServiceLifetime.Scoped => InstanceLifetime.Scoped,
ServiceLifetime.Transient => InstanceLifetime.Transient,
_ => throw new InvalidOperationException(),
};
});

_serviceProvider = services.BuildServiceProvider();
if (ServiceLifetime == ServiceLifetime.Scoped)
{
#pragma warning disable CS0162 // Unreachable code detected
_serviceScope = _serviceProvider.CreateScope();
#pragma warning restore CS0162 // Unreachable code detected
_serviceProvider = _serviceScope.ServiceProvider;
}

_mediator = _serviceProvider.GetRequiredService<IMediator>();
_concreteMediator = _serviceProvider.GetRequiredService<Mediator>();
_mediatr = _serviceProvider.GetRequiredService<MediatR.IMediator>();
Expand All @@ -59,7 +93,10 @@ public void Setup()
[GlobalCleanup]
public void Cleanup()
{
(_serviceProvider as IDisposable)?.Dispose();
if (_serviceScope is not null)
_serviceScope.Dispose();
else
(_serviceProvider as IDisposable)?.Dispose();
}

[Benchmark]
Expand Down
2 changes: 1 addition & 1 deletion samples/SimpleConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
services.AddMediator(options =>
{
options.Namespace = null;
options.DefaultServiceLifetime = ServiceLifetime.Transient;
options.ServiceLifetime = ServiceLifetime.Transient;
});

// Standard handlers are added by default, but we need to add pipeline steps manually.
Expand Down
Loading