-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathRequestBenchmarks.cs
126 lines (107 loc) · 4.42 KB
/
RequestBenchmarks.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
using MessagePipe;
using Microsoft.Extensions.DependencyInjection;
namespace Mediator.Benchmarks.Request;
public sealed record SomeRequest(Guid Id) : IRequest<SomeResponse>, MediatR.IRequest<SomeResponse>;
public sealed record SomeResponse(Guid Id);
public sealed class SomeHandlerClass
: IRequestHandler<SomeRequest, SomeResponse>,
MediatR.IRequestHandler<SomeRequest, SomeResponse>,
IAsyncRequestHandler<SomeRequest, SomeResponse>
{
private static readonly SomeResponse _response = new SomeResponse(Guid.NewGuid());
private static readonly Task<SomeResponse> _tResponse = Task.FromResult(_response);
private static ValueTask<SomeResponse> _vtResponse => new ValueTask<SomeResponse>(_response);
public ValueTask<SomeResponse> Handle(SomeRequest request, CancellationToken cancellationToken) => _vtResponse;
Task<SomeResponse> MediatR.IRequestHandler<SomeRequest, SomeResponse>.Handle(
SomeRequest request,
CancellationToken cancellationToken
) => _tResponse;
public ValueTask<SomeResponse> InvokeAsync(SomeRequest request, CancellationToken cancellationToken) => _vtResponse;
}
[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(Mediator.ServiceLifetime)]
public ServiceLifetime ServiceLifetime { get; set; }
[GlobalSetup]
public void Setup()
{
var services = new ServiceCollection();
services.AddMediator();
services.AddMediatR(opts =>
{
opts.Lifetime = Mediator.ServiceLifetime;
opts.RegisterServicesFromAssembly(typeof(SomeHandlerClass).Assembly);
});
services.AddMessagePipe(opts =>
{
opts.InstanceLifetime = Mediator.ServiceLifetime switch
{
ServiceLifetime.Singleton => InstanceLifetime.Singleton,
ServiceLifetime.Scoped => InstanceLifetime.Scoped,
ServiceLifetime.Transient => InstanceLifetime.Transient,
_ => throw new InvalidOperationException(),
};
});
_serviceProvider = services.BuildServiceProvider();
#pragma warning disable CS0162 // Unreachable code detected
if (Mediator.ServiceLifetime == ServiceLifetime.Scoped)
{
_serviceScope = _serviceProvider.CreateScope();
_serviceProvider = _serviceScope.ServiceProvider;
}
#pragma warning restore CS0162 // Unreachable code detected
_mediator = _serviceProvider.GetRequiredService<IMediator>();
_concreteMediator = _serviceProvider.GetRequiredService<Mediator>();
_mediatr = _serviceProvider.GetRequiredService<MediatR.IMediator>();
_messagePipeHandler = _serviceProvider.GetRequiredService<IAsyncRequestHandler<SomeRequest, SomeResponse>>();
_handler = _serviceProvider.GetRequiredService<SomeHandlerClass>();
_request = new(Guid.NewGuid());
}
[GlobalCleanup]
public void Cleanup()
{
if (_serviceScope is not null)
_serviceScope.Dispose();
else
(_serviceProvider as IDisposable)?.Dispose();
}
[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]
public ValueTask<SomeResponse> SendRequest_MessagePipe()
{
return _messagePipeHandler.InvokeAsync(_request, CancellationToken.None);
}
[Benchmark(Baseline = true)]
public ValueTask<SomeResponse> SendRequest_Baseline()
{
return _handler.Handle(_request, CancellationToken.None);
}
}