-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBenchmark.cs
142 lines (118 loc) · 4.72 KB
/
Benchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using Immediate.Handlers.Shared;
using Microsoft.Extensions.DependencyInjection;
namespace Immediate.Handlers.Benchmarks;
public sealed record SomeRequest(Guid Id)
: Mediator.IRequest<SomeResponse>,
MediatR.IRequest<SomeResponse>;
public sealed record SomeResponse(Guid Id);
[Handler]
public sealed partial class SomeHandlerClass
: Mediator.IRequestHandler<SomeRequest, SomeResponse>,
MediatR.IRequestHandler<SomeRequest, SomeResponse>
{
private static readonly SomeResponse s_response = new(Guid.NewGuid());
private static readonly Task<SomeResponse> s_tResponse = Task.FromResult(s_response);
private static ValueTask<SomeResponse> VtResponse => new(s_response);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Bench instance method")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Not Being Tested")]
public ValueTask<SomeResponse> Handle(
SomeRequest request,
CancellationToken cancellationToken
) => VtResponse;
ValueTask<SomeResponse> Mediator.IRequestHandler<SomeRequest, SomeResponse>.Handle(
SomeRequest request,
CancellationToken cancellationToken
) => VtResponse;
Task<SomeResponse> MediatR.IRequestHandler<SomeRequest, SomeResponse>.Handle(
SomeRequest request,
CancellationToken cancellationToken
) => s_tResponse;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Not Being Tested")]
private static ValueTask<SomeResponse> HandleAsync(
SomeRequest request,
CancellationToken cancellationToken
) => VtResponse;
}
[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.FastestToSlowest, MethodOrderPolicy.Declared)]
[RankColumn]
//[EventPipeProfiler(EventPipeProfile.CpuSampling)]
//[DisassemblyDiagnoser]
//[InliningDiagnoser(logFailuresOnly: true, allowedNamespaces: new[] { "Mediator" })]
#pragma warning disable CA1707 // Identifiers should not contain underscores
public class RequestBenchmarks
{
private IServiceProvider? _serviceProvider;
private IServiceProvider? _abstractionServiceProvider;
private IServiceScope? _serviceScope;
private IServiceScope? _abstractionServiceScope;
private Mediator.IMediator? _mediator;
private Mediator.Mediator? _concreteMediator;
private MediatR.IMediator? _mediatr;
private SomeHandlerClass.Handler? _immediateHandler;
private IHandler<SomeRequest, SomeResponse>? _immediateHandlerAbstraction;
private SomeHandlerClass? _handler;
private SomeRequest? _request;
[GlobalSetup]
public void Setup()
{
var services = new ServiceCollection();
_ = services.AddBenchmarkLargeHandlers();
_ = services.AddMediator(opts => opts.ServiceLifetime = ServiceLifetime.Scoped);
_ = services.AddMediatR(
cfg => cfg.RegisterServicesFromAssemblyContaining<SomeRequest>()
);
_serviceProvider = services.BuildServiceProvider();
_serviceScope = _serviceProvider.CreateScope();
_serviceProvider = _serviceScope.ServiceProvider;
_mediator = _serviceProvider.GetRequiredService<Mediator.IMediator>();
_concreteMediator = _serviceProvider.GetRequiredService<Mediator.Mediator>();
_mediatr = _serviceProvider.GetRequiredService<MediatR.IMediator>();
_immediateHandler = _serviceProvider.GetRequiredService<SomeHandlerClass.Handler>();
_handler = _serviceProvider.GetRequiredService<SomeHandlerClass>();
_request = new(Guid.NewGuid());
_abstractionServiceScope = _serviceProvider.CreateScope();
_abstractionServiceProvider = _abstractionServiceScope.ServiceProvider;
_immediateHandlerAbstraction = _abstractionServiceProvider.GetRequiredService<IHandler<SomeRequest, SomeResponse>>();
}
[GlobalCleanup]
public void Cleanup()
{
if (_serviceScope is not null)
_serviceScope.Dispose();
else
(_serviceProvider as IDisposable)?.Dispose();
}
[Benchmark]
public ValueTask<SomeResponse> SendRequest_ImmediateHandler()
{
return _immediateHandler!.HandleAsync(_request!, CancellationToken.None);
}
[Benchmark]
public ValueTask<SomeResponse> SendRequest_ImmediateHandler_Abstraction()
{
return _immediateHandlerAbstraction!.HandleAsync(_request!, CancellationToken.None);
}
[Benchmark]
public Task<SomeResponse> SendRequest_MediatR()
{
return _mediatr!.Send(_request!, CancellationToken.None);
}
[Benchmark]
public ValueTask<SomeResponse> SendRequest_IMediator()
{
return _mediator!.Send(_request!, CancellationToken.None);
}
[Benchmark]
public ValueTask<SomeResponse> SendRequest_Mediator()
{
return _concreteMediator!.Send(_request!, CancellationToken.None);
}
[Benchmark(Baseline = true)]
public ValueTask<SomeResponse> SendRequest_Baseline()
{
return _handler!.Handle(_request!, CancellationToken.None);
}
}