forked from momentohq/client-sdk-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
471 lines (417 loc) · 19.5 KB
/
Program.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
using System.Diagnostics;
using HdrHistogram;
using Microsoft.Extensions.Logging;
using Momento.Sdk;
using Momento.Sdk.Auth;
using Momento.Sdk.Config;
using Momento.Sdk.Exceptions;
using Momento.Sdk.Responses;
namespace MomentoLoadGen
{
public record CsharpLoadGeneratorOptions
(
LogLevel logLevel,
TimeSpan showStatsInterval,
int cacheItemPayloadBytes,
int numberOfConcurrentRequests,
int maxRequestsPerSecond,
TimeSpan howLongToRun
);
enum AsyncSetGetResult
{
SUCCESS,
UNAVAILABLE,
TIMEOUT,
LIMIT_EXCEEDED,
RST_STREAM,
UNKNOWN,
CANCELLED
};
internal class CsharpLoadGeneratorContext
{
public Stopwatch StartTime;
public Recorder GetLatencies;
public Recorder SetLatencies;
public int GlobalRequestCount;
public int LastWorkerStatsPrintRequestCount;
public int GlobalSuccessCount;
public int GlobalUnavailableCount;
public int GlobalTimeoutExceededCount;
public int GlobalLimitExceededCount;
public int GlobalUnknownCount;
public int GlobalCancelledCount;
public int GlobalRstStreamCount;
public CsharpLoadGeneratorContext()
{
StartTime = System.Diagnostics.Stopwatch.StartNew();
GetLatencies = HistogramFactory.With64BitBucketSize().WithValuesFrom(1).WithValuesUpTo(TimeStamp.Minutes(1)).WithPrecisionOf(1).WithThreadSafeWrites().WithThreadSafeReads().Create();
SetLatencies = HistogramFactory.With64BitBucketSize().WithValuesFrom(1).WithValuesUpTo(TimeStamp.Minutes(1)).WithPrecisionOf(1).WithThreadSafeWrites().WithThreadSafeReads().Create();
GlobalRequestCount = 0;
LastWorkerStatsPrintRequestCount = 0;
GlobalSuccessCount = 0;
GlobalTimeoutExceededCount = 0;
GlobalLimitExceededCount = 0;
GlobalUnknownCount = 0;
GlobalCancelledCount = 0;
GlobalRstStreamCount = 0;
GlobalUnavailableCount = 0;
}
};
public class CsharpLoadGenerator
{
const int CACHE_ITEM_TTL_SECONDS = 60;
const string CACHE_NAME = "dotnet-momento-loadgen";
private readonly ILoggerFactory _loggerFactory;
private readonly ILogger<CsharpLoadGenerator> _logger;
private readonly IConfiguration _momentoClientConfig;
private readonly CsharpLoadGeneratorOptions _options;
private readonly string _cacheValue;
public CsharpLoadGenerator(IConfiguration momentoClientConfig, CsharpLoadGeneratorOptions options)
{
_loggerFactory = momentoClientConfig.LoggerFactory;
_logger = _loggerFactory.CreateLogger<CsharpLoadGenerator>();
_momentoClientConfig = momentoClientConfig;
_options = options;
_cacheValue = new String('x', _options.cacheItemPayloadBytes);
}
public async Task Run()
{
var authProvider = new EnvMomentoTokenProvider("MOMENTO_AUTH_TOKEN");
using (ICacheClient momento = new CacheClient(
_momentoClientConfig,
authProvider,
TimeSpan.FromSeconds(CACHE_ITEM_TTL_SECONDS)
))
{
try
{
await momento.CreateCacheAsync(CACHE_NAME);
}
catch (AlreadyExistsException)
{
_logger.LogInformation("cache '{0}' already exists", CACHE_NAME);
}
var workerDelayBetweenRequests = Convert.ToInt32(Math.Floor((1000.0 * _options.numberOfConcurrentRequests) / (_options.maxRequestsPerSecond * 1)));
Console.WriteLine($"Targeting a max of {_options.maxRequestsPerSecond} requests per second (delay between requests: {workerDelayBetweenRequests})");
Console.WriteLine($"Running {_options.numberOfConcurrentRequests} concurrent requests for {_options.howLongToRun}");
var context = new CsharpLoadGeneratorContext();
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
var asyncTasks = Enumerable.Range(0, _options.numberOfConcurrentRequests).Select<int, Task>(workerId =>
LaunchAndRunWorkers(
momento,
context,
workerId + 1,
workerDelayBetweenRequests,
cancellationTokenSource.Token
)
);
var statsPrinterTask = LaunchStatsPrinterTask(
context,
_options.showStatsInterval,
cancellationTokenSource.Token
);
asyncTasks.Append(statsPrinterTask);
cancellationTokenSource.CancelAfter(_options.howLongToRun);
// this will ensure that the program exits promptly if one of the async
// tasks throws an uncaught exception.
var firstResult = await Task.WhenAny(asyncTasks);
await firstResult;
await Task.WhenAll(asyncTasks);
}
_logger.LogInformation("Done");
}
private async Task LaunchAndRunWorkers(
ICacheClient client,
CsharpLoadGeneratorContext context,
int workerId,
int delayMillisBetweenRequests,
CancellationToken token
)
{
for (var i = 1; !token.IsCancellationRequested; i++)
{
await IssueAsyncSetGet(client, context, workerId, i, delayMillisBetweenRequests);
}
}
private async Task LaunchStatsPrinterTask(CsharpLoadGeneratorContext context, TimeSpan showStatsInterval, CancellationToken token)
{
var setsAccumulatingHistogram = new LongHistogram(TimeStamp.Minutes(1), 1);
var getsAccumulatingHistogram = new LongHistogram(TimeStamp.Minutes(1), 1);
while (!token.IsCancellationRequested)
{
try {
await Task.Delay(showStatsInterval, token);
}
finally {
PrintStats(setsAccumulatingHistogram, getsAccumulatingHistogram, context);
}
}
}
private void PrintStats(LongHistogram setsAccumulatingHistogram, LongHistogram getsAccumulatingHistogram, CsharpLoadGeneratorContext context)
{
var setsIntervalHistogram = context.SetLatencies.GetIntervalHistogram();
setsAccumulatingHistogram.Add(setsIntervalHistogram);
var getsIntervalHistogram = context.GetLatencies.GetIntervalHistogram();
getsAccumulatingHistogram.Add(getsIntervalHistogram);
Console.WriteLine($@"
cumulative stats:
total requests: {context.GlobalRequestCount} ({Tps(context, context.GlobalRequestCount)} tps)
success: {context.GlobalSuccessCount} ({PercentRequests(context, context.GlobalSuccessCount)}%) ({Tps(context, context.GlobalSuccessCount)} tps)
unavailable: {context.GlobalUnavailableCount} ({PercentRequests(context, context.GlobalUnavailableCount)}%)
timeout exceeded: {context.GlobalTimeoutExceededCount} ({PercentRequests(context, context.GlobalTimeoutExceededCount)}%)
limit exceeded: {context.GlobalLimitExceededCount} ({PercentRequests(context, context.GlobalLimitExceededCount)}%)
cancelled: {context.GlobalCancelledCount} ({PercentRequests(context, context.GlobalCancelledCount)}%)
unknown: {context.GlobalUnknownCount} ({PercentRequests(context, context.GlobalUnknownCount)}%)
rst stream: {context.GlobalRstStreamCount} ({PercentRequests(context, context.GlobalRstStreamCount)}%)
cumulative set latencies:
{OutputHistogramSummary(setsAccumulatingHistogram)}
cumulative get latencies:
{OutputHistogramSummary(getsAccumulatingHistogram)}
");
_logger.LogInformation($"Load gen data point:\t{_options.numberOfConcurrentRequests}\t{Tps(context, context.GlobalRequestCount)}\t{getsAccumulatingHistogram.GetValueAtPercentile(50)}\t{getsAccumulatingHistogram.GetValueAtPercentile(99.9)}");
}
private async Task IssueAsyncSetGet(ICacheClient client, CsharpLoadGeneratorContext context, int workerId, int operationId, int delayMillisBetweenRequests)
{
var cacheKey = $"worker{workerId}operation{operationId}";
var setStartTime = System.Diagnostics.Stopwatch.StartNew();
var result = await ExecuteRequestAndUpdateContextCounts(
context,
() => IssueSetRequest(client, cacheKey, _cacheValue)
);
if (result is CacheSetResponse.Success setSuccess)
{
var setDuration = setStartTime.ElapsedMilliseconds;
context.SetLatencies.RecordValue(setDuration);
if (setDuration < delayMillisBetweenRequests)
{
await Task.Delay((int)(delayMillisBetweenRequests - setDuration));
}
}
var getStartTime = System.Diagnostics.Stopwatch.StartNew();
var getResponse = await ExecuteRequestAndUpdateContextCounts(
context,
() => IssueGetRequest(client, cacheKey)
);
if (getResponse is CacheGetResponse.Hit hitResponse)
{
var getDuration = getStartTime.ElapsedMilliseconds;
context.GetLatencies.RecordValue(getDuration);
if (getDuration < delayMillisBetweenRequests)
{
await Task.Delay((int)(delayMillisBetweenRequests - getDuration));
}
}
}
private async Task<TResult> ExecuteRequestAndUpdateContextCounts<TResult>(
CsharpLoadGeneratorContext context,
Func<Task<Tuple<AsyncSetGetResult, TResult>>> block
)
{
var result = await block();
UpdateContextCountsForRequest(context, result.Item1);
return result.Item2;
}
private async Task<Tuple<AsyncSetGetResult, CacheGetResponse?>> IssueGetRequest(ICacheClient client, String cacheKey)
{
var getResponse = await client.GetAsync(CACHE_NAME, cacheKey);
if (getResponse is CacheGetResponse.Hit hit)
{
return Tuple.Create<AsyncSetGetResult, CacheGetResponse?>(AsyncSetGetResult.SUCCESS, hit);
}
else if (getResponse is CacheGetResponse.Miss miss)
{
return Tuple.Create<AsyncSetGetResult, CacheGetResponse?>(AsyncSetGetResult.SUCCESS, miss);
}
else if (getResponse is CacheGetResponse.Error error)
{
return Tuple.Create<AsyncSetGetResult, CacheGetResponse?>(ConvertErrorToAsyncSetGetResult(error.ErrorCode, error.InnerException), null);
}
else
{
throw new ApplicationException($"Unsupported get response: {getResponse}");
}
}
private async Task<Tuple<AsyncSetGetResult, CacheSetResponse?>> IssueSetRequest(ICacheClient client, String cacheKey, String cacheValue)
{
var setResponse = await client.SetAsync(CACHE_NAME, cacheKey, cacheValue);
if (setResponse is CacheSetResponse.Success success)
{
return Tuple.Create<AsyncSetGetResult, CacheSetResponse?>(AsyncSetGetResult.SUCCESS, success);
}
else if (setResponse is CacheSetResponse.Error error)
{
return Tuple.Create<AsyncSetGetResult, CacheSetResponse?>(ConvertErrorToAsyncSetGetResult(error.ErrorCode, error.InnerException), null);
}
else
{
throw new ApplicationException($"Unsupported set response: {setResponse}");
}
}
private AsyncSetGetResult ConvertErrorToAsyncSetGetResult(MomentoErrorCode errorCode, SdkException ex)
{
if (errorCode == MomentoErrorCode.SERVER_UNAVAILABLE)
{
_logger.LogError("SERVER UNAVAILABLE: {}", ex);
return AsyncSetGetResult.UNAVAILABLE;
}
else if (errorCode == MomentoErrorCode.INTERNAL_SERVER_ERROR)
{
_logger.LogError("INTERNAL SERVER ERROR: {}", ex);
return AsyncSetGetResult.UNAVAILABLE;
}
else if (errorCode == MomentoErrorCode.TIMEOUT_ERROR)
{
_logger.LogError("TIMEOUT ERROR: {}", ex);
return AsyncSetGetResult.TIMEOUT;
}
else if (errorCode == MomentoErrorCode.LIMIT_EXCEEDED_ERROR)
{
return AsyncSetGetResult.LIMIT_EXCEEDED;
}
else if (errorCode is MomentoErrorCode.UNKNOWN_ERROR or MomentoErrorCode.UNKNOWN_SERVICE_ERROR)
{
return AsyncSetGetResult.UNKNOWN;
}
else if (errorCode == MomentoErrorCode.CANCELLED_ERROR)
{
return AsyncSetGetResult.CANCELLED;
}
_logger.LogError("UNCAUGHT EXCEPTION: {}", ex);
throw new ApplicationException($"Unsupported error code: {errorCode}");
}
private static void UpdateContextCountsForRequest(
CsharpLoadGeneratorContext context,
AsyncSetGetResult result
)
{
Interlocked.Increment(ref context.GlobalRequestCount);
var updated = result switch
{
AsyncSetGetResult.SUCCESS => Interlocked.Increment(ref context.GlobalSuccessCount),
AsyncSetGetResult.UNAVAILABLE => Interlocked.Increment(ref context.GlobalUnavailableCount),
AsyncSetGetResult.TIMEOUT => Interlocked.Increment(ref context.GlobalTimeoutExceededCount),
AsyncSetGetResult.LIMIT_EXCEEDED => Interlocked.Increment(ref context.GlobalLimitExceededCount),
AsyncSetGetResult.RST_STREAM => Interlocked.Increment(ref context.GlobalRstStreamCount),
AsyncSetGetResult.UNKNOWN => Interlocked.Increment(ref context.GlobalUnknownCount),
AsyncSetGetResult.CANCELLED => Interlocked.Increment(ref context.GlobalCancelledCount),
_ => throw new Exception($"Unrecognized result: {result}"),
};
return;
}
private static double Tps(CsharpLoadGeneratorContext context, int requestCount)
{
return Math.Round((requestCount * 1000.0) / context.StartTime.ElapsedMilliseconds);
}
private static string PercentRequests(CsharpLoadGeneratorContext context, int count)
{
if (context.GlobalRequestCount == 0)
{
return "0";
}
return Math.Round((Convert.ToDouble(count) / context.GlobalRequestCount) * 100.0, 1).ToString();
}
private static string OutputHistogramSummary(HistogramBase histogram)
{
return $@"
count: {histogram.TotalCount}
p50: {histogram.GetValueAtPercentile(50)}
p90: {histogram.GetValueAtPercentile(90)}
p99: {histogram.GetValueAtPercentile(99)}
p99.9: {histogram.GetValueAtPercentile(99.9)}
max: {histogram.GetMaxValue()}
";
}
}
internal class Program
{
static ILoggerFactory InitializeLogging(LogLevel minLogLevel)
{
return LoggerFactory.Create(builder =>
{
builder.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "hh:mm:ss ";
});
builder.AddFilter("Grpc.Net.Client", LogLevel.Error);
builder.SetMinimumLevel(minLogLevel);
});
}
const string PERFORMANCE_INFORMATION_MESSAGE = @"
Thanks for trying out our basic c# load generator! This tool is
included to allow you to experiment with performance in your environment
based on different configurations. It's very simplistic, and only intended
to give you a quick way to explore the performance of the Momento client
running in a dotnet application.
Since performance will be impacted by network latency, you'll get the best
results if you run on a cloud VM in the same region as your Momento cache.
Check out the configuration settings at the bottom of the 'MomentoLoadGen/Program.cs'
file to see how different configurations impact performance.
If you have questions or need help experimenting further, please reach out to us!
";
static async Task Main(string[] args)
{
CsharpLoadGeneratorOptions loadGeneratorOptions = new CsharpLoadGeneratorOptions(
///
/// Controls the verbosity of the output during the run.
///
logLevel: LogLevel.Debug,
///
/// Each time this amount of time has passed, statistics about throughput and latency
/// will be printed.
///
///
showStatsInterval: TimeSpan.FromSeconds(5),
///
/// Controls the size of the payload that will be used for the cache items in
/// the load test. Smaller payloads will generally provide lower latencies than
/// larger payloads.
///
cacheItemPayloadBytes: 100,
///
/// Controls the number of concurrent requests that will be made (via asynchronous
/// function calls) by the load test. Increasing this number may improve throughput,
/// but it will also increase CPU consumption. As CPU usage increases and there
/// is more contention between the concurrent function calls, client-side latencies
/// may increase.
///
numberOfConcurrentRequests: 50,
///
/// Sets an upper bound on how many requests per second will be sent to the server.
/// Momento caches have a default throttling limit of 100 requests per second,
/// so if you raise this, you may observe throttled requests. Contact
/// [email protected] to inquire about raising your limits.
///
maxRequestsPerSecond: 100,
///
/// Controls how long the load test will run.
///
howLongToRun: TimeSpan.FromMinutes(1)
);
using (ILoggerFactory loggerFactory = InitializeLogging(loadGeneratorOptions.logLevel))
{
///
/// This is the configuration that will be used for the Momento client. Choose from
/// our pre-built configurations that are optimized for Laptop vs InRegion environments,
/// or build your own.
///
IConfiguration config = Configurations.Laptop.V1(loggerFactory);
CsharpLoadGenerator loadGenerator = new CsharpLoadGenerator(
config,
loadGeneratorOptions
);
try
{
await loadGenerator.Run();
Console.WriteLine("success!");
Console.WriteLine(PERFORMANCE_INFORMATION_MESSAGE);
}
catch (Exception e)
{
Console.WriteLine("ERROR!: {0}", e);
}
}
}
}
}