forked from microsoft/garnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
295 lines (259 loc) · 11.9 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CommandLine;
using Garnet.client;
using Garnet.common;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
namespace Resp.benchmark
{
/// <summary>
/// Benchmarking client. Example command-line args:
/// "--op GET -t 1,32"
/// "--op GET -t 1,2,4,8,16,32,64"
/// "--op INCR -t 1,2,4,8,16,32 --valuelength 0"
/// "--op ZADDREM --dbsize 2048 -b 1024 -t 1,2"
/// "--op PFADD -t 32 --batchsize 1024 --dbsize 64"
/// "--op BITCOUNT -t 1 --dbsize 256 --valuelength 1048576"
/// "--op BITOP_AND -t 1 --dbsize 256 --valuelength 1048576"
/// "--op SETBIT -t 1 --dbsize 256 --valuelength 1048576"
/// "--op BITFIELD_SET -t 1 --dbsize 256 --valuelength 1048576"
/// "--op GEOADDREM --dbsize 2048 -b 256 -t 1,2"
/// </summary>
class Program
{
public static IConnectionMultiplexer redis;
public static ILoggerFactory loggerFactory;
public static PeriodicCheckpointer pc = null;
static ILoggerFactory CreateLoggerFactory(Options opts)
{
return LoggerFactory.Create(builder =>
{
if (!opts.DisableConsoleLogger)
{
builder.AddProvider(new BenchmarkLoggerProvider(Console.Out));
}
// Optional: Flush log output to file.
if (opts.FileLogger != null)
builder.AddFile(opts.FileLogger);
builder.SetMinimumLevel(opts.LogLevel);
});
}
static void PrintBenchMarkSummary(Options opts)
{
Console.WriteLine("<<<<<<< Benchmark Configuration >>>>>>>>");
Console.WriteLine($"benchmarkType: {(opts.Online ? "Online" : "Throughput")}");
Console.WriteLine($"skipLoad: {(opts.SkipLoad ? "Enabled" : "Disabled")}");
Console.WriteLine($"DBsize: {opts.DbSize}");
Console.WriteLine($"TotalOps: {opts.TotalOps}");
Console.WriteLine($"Op Benchmark: {opts.Op}");
Console.WriteLine($"KeyLength: {opts.KeyLength}");
Console.WriteLine($"ValueLength: {opts.ValueLength}");
Console.WriteLine($"BatchSize: {string.Join(",", opts.BatchSize.ToList())}");
Console.WriteLine($"RunTime: {opts.RunTime}");
Console.WriteLine($"NumThreads: {string.Join(",", opts.NumThreads.ToList())}");
Console.WriteLine($"Auth: {opts.Auth}");
Console.WriteLine($"Load Using SET: {(opts.LSet ? "Enabled" : "Disabled")}");
Console.WriteLine($"ClientType used for benchmarking: {opts.Client}");
if (opts.Online)
{
Console.WriteLine($"Pool: {opts.Pool}");
Console.WriteLine($"OpWorkload {String.Join(',', opts.OpWorkload)}");
Console.WriteLine($"OpPercent {String.Join(',', opts.OpPercent)}");
if (opts.Ttl > 0)
{
Console.WriteLine($"Ttl: {opts.Ttl}");
}
if (opts.SortedSetCardinality > 0)
{
Console.WriteLine($"Sorted set cardinality: {opts.SortedSetCardinality}");
}
Console.WriteLine($"Intra thread parallelism: {opts.IntraThreadParallelism}");
Console.WriteLine($"SyncMode: {opts.SyncMode}");
}
if (opts.Op == OpType.SETEX && opts.Ttl > 0)
{
Console.WriteLine($"Ttl: {opts.Ttl}");
}
Console.WriteLine($"TLS: {(opts.EnableTLS ? "Enabled" : "Disabled")}");
Console.WriteLine($"ClientHistogram: {opts.ClientHistogram}");
ThreadPool.SetMinThreads(workerThreads: 1000, completionPortThreads: 1000);
ThreadPool.GetMinThreads(out var minWorkerThreads, out var minCompletionPortThreads);
Console.WriteLine($"minWorkerThreads: {minWorkerThreads}");
Console.WriteLine($"minCompletionPortThreads: {minCompletionPortThreads}");
Console.WriteLine("----------------------------------");
}
static bool DisabledFeatures(Options opts)
{
HashSet<OpType> seRedisSupportOpSet = new();
seRedisSupportOpSet.Add(OpType.GET);
seRedisSupportOpSet.Add(OpType.SET);
seRedisSupportOpSet.Add(OpType.SETEX);
seRedisSupportOpSet.Add(OpType.PING);
if (opts.Online)
{
if (opts.Op != OpType.GET)
{
Console.WriteLine($"OpType not supported for online benchmark (defaults to get/set)");
return true;
}
}
if (opts.Online)
{
var batches = opts.BatchSize.ToList();
if (batches.Count != 1 || batches[0] != 1)
{
Console.WriteLine("Batch size parameter should be one entry of size 1, for the online benchmark, setting to [ 1 ]. Use --itp to control batch size instead.");
opts.BatchSize = [1];
}
if (opts.DbSize < opts.SortedSetCardinality)
{
Console.WriteLine("DB size cannot be smaller than SortedSet cardinality");
return true;
}
if (opts.Client == ClientType.LightClient && opts.Pool)
{
Console.WriteLine("Pooling of LightClient is not supported");
return true;
}
if (opts.ClientHistogram && opts.Pool)
{
Console.WriteLine("Client hisogram and pool are not supported at the same time.");
return true;
}
}
else
{
if (opts.Op == OpType.SETEX && opts.Ttl <= 0)
{
Console.WriteLine("TTL must be positive for SETEX operations.");
return true;
}
}
if (opts.EnableTLS && opts.CertFileName == null)
{
Console.WriteLine("Certificate file name is required for TLS");
return true;
}
if (!opts.EnableTLS && opts.CertFileName != null)
{
Console.WriteLine("Certificate file name is not required for non-TLS");
return true;
}
return false;
}
static void Main(string[] args)
{
ParserResult<Options> result = Parser.Default.ParseArguments<Options>(args);
if (result.Tag == ParserResultType.NotParsed) return;
var opts = result.MapResult(o => o, xs => new Options());
if (DisabledFeatures(opts))
return;
loggerFactory = CreateLoggerFactory(opts);
WaitForServer(opts);
if (opts.SaveFreqSecs > 0)
{
pc = new PeriodicCheckpointer(opts.SaveFreqSecs * 1000);
new Thread(() => pc.Start(opts.Address, opts.Port)).Start();
}
if (opts.Client == ClientType.SERedis)
redis = ConnectionMultiplexer.Connect(BenchUtils.GetConfig(opts.Address, opts.Port, useTLS: opts.EnableTLS, tlsHost: opts.TlsHost));
PrintBenchMarkSummary(opts);
if (opts.Op == OpType.PFADD || opts.Op == OpType.PFCOUNT || opts.Op == OpType.PFMERGE)
RunHLLBenchmark(opts);
else
RunBasicCommandsBenchmark(opts);
pc?.Stop();
}
static void WaitForServer(Options opts)
{
using var client = new GarnetClientSession(opts.Address, opts.Port, new(), tlsOptions: opts.EnableTLS ? BenchUtils.GetTlsOptions(opts.TlsHost, opts.CertFileName, opts.CertPassword) : null);
while (true)
{
try
{
client.Connect();
client.Execute("QUIT");
}
catch
{
Console.WriteLine($"Waiting for server at {opts.Address}:{opts.Port}");
Thread.Sleep(1000);
continue;
}
break;
}
}
static void RunBasicCommandsBenchmark(Options opts)
{
int[] threadBench = opts.NumThreads.ToArray();
int keyLen = opts.KeyLength;
int valueLen = opts.ValueLength;
if (opts.Op == OpType.PUBLISH || opts.Op == OpType.SPUBLISH || opts.Op == OpType.ZADD || opts.Op == OpType.ZREM || opts.Op == OpType.ZADDREM || opts.Op == OpType.PING || opts.Op == OpType.GEOADD || opts.Op == OpType.GEOADDREM || opts.Op == OpType.SETEX || opts.Op == OpType.ZCARD || opts.Op == OpType.ZADDCARD)
opts.SkipLoad = true;
//if we have scripts ops we need to load them in memory
if (opts.Op == OpType.SCRIPTGET || opts.Op == OpType.SCRIPTSET || opts.Op == OpType.SCRIPTRETKEY)
{
unsafe
{
var onResponseDelegate = new LightClient.OnResponseDelegateUnsafe(ReqGen.OnResponse);
using var client = new LightClient(opts.Address, opts.Port, (int)OpType.GET, onResponseDelegate, opts.DbSize, opts.EnableTLS ? BenchUtils.GetTlsOptions(opts.TlsHost, opts.CertFileName, opts.CertPassword) : null);
client.Connect();
client.Authenticate(opts.Auth);
BenchUtils.LoadSetGetScripts(client, out BenchUtils.sha1SetScript, out BenchUtils.sha1GetScript);
}
}
if (opts.Online)
{
if (opts.SkipLoad)
throw new Exception("Skipload not supported with --online");
var bench = new RespOnlineBench(opts, runDuration: opts.RunTime == -1 ? int.MaxValue : opts.RunTime, loggerFactory: loggerFactory);
bench.Run();
return;
}
else if (opts.Txn)
{
if (opts.SkipLoad)
throw new Exception("Skipload not supported with --txn");
var bench = new TxnPerfBench(opts, runDuration: opts.RunTime == -1 ? int.MaxValue : opts.RunTime);
bench.LoadData();
bench.Run();
return;
}
else
{
var bench = new RespPerfBench(opts, 0, redis);
if (!opts.SkipLoad)
bench.LoadData(keyLen: keyLen, valueLen: valueLen, numericValue: opts.Op == OpType.INCR);
foreach (int BatchSize in opts.BatchSize)
bench.Run(
opts.Op,
opts.TotalOps,
threadBench, runTime: TimeSpan.FromSeconds(opts.RunTime),
keyLen: keyLen,
valueLen: valueLen,
BatchSize: BatchSize,
ttl: opts.Ttl);
}
}
static void RunHLLBenchmark(Options opts)
{
var bench = new RespPerfBench(opts, 0, redis);
int[] threadBench = opts.NumThreads.ToArray();
int loadThreads = 8;
int loadBatchSize = opts.DbSize / loadThreads;
loadBatchSize = opts.DbSize < 4096 ? loadBatchSize : 4096;
if (opts.SkipLoad && opts.Op != OpType.PFADD)
throw new Exception(opts.Op + " cannot be benchmarked using the skipload option");
if (!opts.SkipLoad)
bench.LoadHLLData(loadDbThreads: loadThreads, BatchSize: loadBatchSize);
//PFCOUNT, PFMERGE
foreach (int BatchSize in opts.BatchSize)
bench.Run(opts.Op, opts.TotalOps, threadBench, keyLen: opts.KeyLength, valueLen: opts.ValueLength, BatchSize: BatchSize);
}
}
}