Repository navigation
Expand file tree
/
Copy pathRedisReadBenchmarks.cs
More file actions
244 lines (212 loc) · 7.09 KB
/
Copy pathRedisReadBenchmarks.cs
File metadata and controls
244 lines (212 loc) · 7.09 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Order;
using Microsoft.Extensions.DependencyInjection;
using RequestBatcher.Benchmarks.Infrastructure;
using StackExchange.Redis;
namespace RequestBatcher.Benchmarks.Redis;
[MemoryDiagnoser]
[RankColumn]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[SimpleJob(
RunStrategy.Monitoring,
launchCount: 1,
warmupCount: 2,
iterationCount: 8,
invocationCount: 1,
id: "Redis")]
public class RedisReadBenchmarks
{
private const string ValuePrefix = "RequestBatcher Redis read payload ";
private readonly BatchStartGate _startGate = new();
private readonly BatchExecutionCounter _executionCounter = new();
private RedisReadRequest[] _requests = [];
private RedisBenchmarkEnvironment? _environment;
private ServiceProvider? _serviceProvider;
private IRequestBatcher<RedisReadRequest>? _requestBatcher;
private int _workloadExecuted;
[Params(1_000)]
public int RequestCount { get; set; }
[Params(100)]
public int BatchSize { get; set; }
[Params(4)]
public int MaxConcurrency { get; set; }
private IDatabase Database =>
_environment?.Database ??
throw new InvalidOperationException("The benchmark has not been initialized.");
private IRequestBatcher<RedisReadRequest> RequestBatcher =>
_requestBatcher ?? throw new InvalidOperationException("The benchmark has not been initialized.");
[GlobalSetup]
public async Task GlobalSetupAsync()
{
_environment = new RedisBenchmarkEnvironment();
await _environment.StartAsync().ConfigureAwait(false);
_requests = Enumerable.Range(0, RequestCount)
.Select(requestId => new RedisReadRequest(
$"requestbatcher:read:{requestId}",
$"{ValuePrefix}{requestId}"))
.ToArray();
var seedEntries = _requests
.Select(request => new KeyValuePair<RedisKey, RedisValue>(request.Key, request.ExpectedValue))
.ToArray();
if (!await Database.StringSetAsync(seedEntries).ConfigureAwait(false))
{
throw new InvalidOperationException("Redis rejected the read benchmark seed data.");
}
var services = new ServiceCollection();
services.AddSingleton(Database);
services.AddSingleton(_startGate);
services.AddSingleton(_executionCounter);
services.AddRequestBatcher<RedisReadRequest, RedisReadBatchHandler>(
ServiceLifetime.Singleton,
options =>
{
options.BatchSize = BatchSize;
options.MaxConcurrency = MaxConcurrency;
options.MaxPendingRequests = RequestCount;
});
_serviceProvider = services.BuildServiceProvider(new ServiceProviderOptions
{
ValidateOnBuild = true,
ValidateScopes = true,
});
_requestBatcher = _serviceProvider.GetRequiredService<IRequestBatcher<RedisReadRequest>>();
}
[IterationSetup]
public void IterationSetup()
{
foreach (var request in _requests)
{
request.Result = RedisValue.Null;
}
_startGate.Reset();
_executionCounter.Reset();
Volatile.Write(ref _workloadExecuted, 0);
}
[Benchmark(Baseline = true, Description = "Direct: one GET per request")]
public async Task DirectSingleReadsAsync()
{
Volatile.Write(ref _workloadExecuted, 1);
var reads = new Task[_requests.Length];
try
{
for (var index = 0; index < _requests.Length; index++)
{
reads[index] = ReadSingleAsync(_requests[index]);
}
}
finally
{
_startGate.Release();
}
await Task.WhenAll(reads).ConfigureAwait(false);
}
[Benchmark(Description = "RequestBatcher: single-request submissions")]
public async Task RequestBatcherSingleReadsAsync()
{
Volatile.Write(ref _workloadExecuted, 1);
var reads = new Task[_requests.Length];
try
{
for (var index = 0; index < _requests.Length; index++)
{
reads[index] = RequestBatcher.ExecuteAsync(_requests[index]);
}
}
finally
{
_startGate.Release();
}
await Task.WhenAll(reads).ConfigureAwait(false);
}
[Benchmark(Description = "RequestBatcher: one explicit batch submission")]
public async Task RequestBatcherBatchReadsAsync()
{
Volatile.Write(ref _workloadExecuted, 1);
Task processing;
try
{
processing = RequestBatcher.ExecuteAsync(_requests);
}
finally
{
_startGate.Release();
}
await processing.ConfigureAwait(false);
}
[IterationCleanup(Target = nameof(DirectSingleReadsAsync))]
public void ValidateDirectReads()
{
if (Volatile.Read(ref _workloadExecuted) != 0)
{
ValidateResults();
}
}
[IterationCleanup(Targets = new[]
{
nameof(RequestBatcherSingleReadsAsync),
nameof(RequestBatcherBatchReadsAsync),
})]
public void ValidateRequestBatcherReads()
{
if (Volatile.Read(ref _workloadExecuted) == 0)
{
return;
}
ValidateResults();
ValidateBatching();
}
[GlobalCleanup]
public async Task GlobalCleanupAsync()
{
try
{
if (_serviceProvider is not null)
{
await _serviceProvider.DisposeAsync().ConfigureAwait(false);
}
}
finally
{
_serviceProvider = null;
_requestBatcher = null;
if (_environment is not null)
{
await _environment.DisposeAsync().ConfigureAwait(false);
_environment = null;
}
}
}
private async Task ReadSingleAsync(RedisReadRequest request)
{
await _startGate.WaitAsync().ConfigureAwait(false);
request.Result = await Database.StringGetAsync(request.Key).ConfigureAwait(false);
}
private void ValidateResults()
{
foreach (var request in _requests)
{
if (request.Result != request.ExpectedValue)
{
throw new InvalidOperationException(
$"Redis key '{request.Key}' returned an unexpected value.");
}
}
}
private void ValidateBatching()
{
var snapshot = _executionCounter.Snapshot;
if (snapshot.RequestCount != RequestCount)
{
throw new InvalidOperationException(
$"The batch handler processed {snapshot.RequestCount} reads; expected {RequestCount}.");
}
if (snapshot.BatchCount >= RequestCount || snapshot.MaximumBatchSize <= 1)
{
throw new InvalidOperationException(
$"Read batching did not occur: {snapshot.BatchCount} batches, " +
$"maximum size {snapshot.MaximumBatchSize}.");
}
}
}