-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathReqGen.cs
More file actions
328 lines (299 loc) · 11.1 KB
/
ReqGen.cs
File metadata and controls
328 lines (299 loc) · 11.1 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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Threading;
using Garnet.common;
namespace Resp.benchmark
{
/// <summary>
/// Buffer of generated RESP requests
/// </summary>
public unsafe partial class ReqGen
{
const int MaxBatches = 1 << 16;
static int bitfieldOpCount = 3;
readonly byte[][] buffers;
readonly List<List<string>> flatRequestBuffer;
readonly int[] lens;
readonly OpType opType;
readonly bool randomGen, randomServe;
public readonly int DbSize, NumBuffs;
readonly bool zipf;
readonly int Start;
readonly byte[] valueBuffer;
readonly int hllDstMergeKeyCount = 1 << 8;
readonly double hllDstMergeKeyFraction = 0.001;
readonly ZipfGenerator zipfg;
readonly byte[] ascii_chars = System.Text.Encoding.ASCII.GetBytes("abcdefghijklmnopqrstvuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
readonly byte[] ascii_digits = System.Text.Encoding.ASCII.GetBytes("0123456789");
readonly bool verbose;
readonly bool numericValue;
readonly bool flatBufferClient;
readonly int ttl;
readonly int keyLen, valueLen;
Random r;
Random keyRandomGen;
Random valueRandomGen;
int BufferSize = 1 << 16;
int keyIndex;
int seqNo;
public int BatchCount;
public ReqGen(
int Start,
int DbSize,
int NumOps,
int BatchSize,
OpType opType,
bool randomGen = true,
bool randomServe = true,
int keyLen = default,
int valueLen = default,
bool numericValue = false,
bool verbose = true,
bool zipf = false,
bool flatBufferClient = false,
int ttl = 0)
{
NumBuffs = NumOps / BatchSize;
if (NumBuffs > MaxBatches && verbose)
{
Console.WriteLine($"Restricting #buffers to {MaxBatches} instead of {NumBuffs}");
NumBuffs = MaxBatches;
}
buffers = new byte[NumBuffs][];
flatRequestBuffer = flatBufferClient ? new List<List<string>>() : null;
lens = new int[NumBuffs];
BatchCount = BatchSize;
this.opType = opType;
seqNo = 0;
this.randomGen = randomGen;
this.randomServe = randomServe;
this.DbSize = DbSize;
this.Start = Start;
this.flatBufferClient = flatBufferClient;
this.ttl = ttl;
if (zipf)
{
this.zipf = zipf;
zipfg = new ZipfGenerator(new RandomGenerator(), DbSize, 0.99);
}
this.keyLen = keyLen == default ? NumUtils.NumDigits(DbSize) : keyLen;
this.valueLen = valueLen == default ? 8 : valueLen;
valueBuffer = new byte[this.valueLen];
this.numericValue = numericValue;
this.verbose = verbose;
int _hllDstMergeKeyCount = (int)(hllDstMergeKeyFraction * DbSize);
if (_hllDstMergeKeyCount != 0)
hllDstMergeKeyCount = _hllDstMergeKeyCount;
this.ttl = ttl;
}
public int GetBufferSize()
{
return BufferSize;
}
/// <summary>
/// Get batch of requests to serve
/// </summary>
/// <param name="len"></param>
/// <returns></returns>
public byte[] GetRequest(out int len)
{
int offset;
if (randomServe)
offset = r.Next(NumBuffs);
else
offset = (Interlocked.Increment(ref seqNo) - 1) % NumBuffs;
len = lens[offset];
return buffers[offset];
}
public List<string> GetRequestArgs()
{
int offset;
if (randomServe)
offset = r.Next(flatRequestBuffer.Count);
else
offset = (Interlocked.Increment(ref seqNo) - 1) % flatRequestBuffer.Count;
return flatRequestBuffer[offset];
}
private void InitializeRNG(int keySeed = -1, int valueSeed = -1)
{
keyIndex = Start;
r = new Random(789110123);
keyRandomGen = keySeed == -1 ? new Random(789110123) : new Random(keySeed);
valueRandomGen = valueSeed == -1 ? new Random(789110123) : new Random(valueSeed);
}
private void ConvertToSERedisInput(OpType opType)
{
for (int i = 0; i < NumBuffs; i++)
{
switch (opType)
{
case OpType.GET:
case OpType.SET:
case OpType.MSET:
var buffer = buffers[i];
flatRequestBuffer.Add(new List<string>());
ProcessArgs(i, buffer);
break;
default:
Console.WriteLine($"op {opType} not supported with SERedis! Skipping conversion to SERedis input!");
break;
}
}
}
private void ProcessArgs(int i, byte[] buffer)
{
fixed (byte* buf = buffer)
{
byte* ptr = buf;
RespReadUtils.TryReadUnsignedArrayLength(out int count, ref ptr, buf + buffer.Length);
RespReadUtils.TryReadStringWithLengthHeader(out var cmd, ref ptr, buf + buffer.Length);
for (int j = 0; j < count - 1; j++)
{
RespReadUtils.TryReadStringWithLengthHeader(out var arg, ref ptr, buf + buffer.Length);
flatRequestBuffer[i].Add(arg);
}
}
}
/// <summary>
/// Response handler
/// </summary>
/// <param name="buf"></param>
/// <param name="bytesRead"></param>
/// <param name="opType"></param>
/// <returns></returns>
public static (int, int) OnResponse(byte* buf, int bytesRead, int opType)
{
int count = 0;
switch ((OpType)opType)
{
case OpType.MGET:
case OpType.GET:
case OpType.MYDICTGET:
case OpType.SCRIPTGET:
case OpType.SCRIPTRETKEY:
for (int i = 0; i < bytesRead; i++)
if (buf[i] == '$') count++;
break;
case OpType.DEL:
case OpType.INCR:
case OpType.DBSIZE:
case OpType.PUBLISH:
case OpType.SPUBLISH:
for (int i = 0; i < bytesRead; i++)
if (buf[i] == ':') count++;
break;
case OpType.MSET:
case OpType.PING:
case OpType.PFMERGE:
case OpType.AUTH:
case OpType.SET:
case OpType.SCRIPTSET:
case OpType.SETEX:
case OpType.SETIFPM:
case OpType.MYDICTSET:
case OpType.READONLY:
for (int i = 0; i < bytesRead; i++)
if (buf[i] == '+') count++;
break;
case OpType.PFADD:
for (int i = 0; i < bytesRead; i++)
if (buf[i] == ':') count++;
break;
case OpType.MPFADD:
for (int i = 0; i < bytesRead; i++)
if (buf[i] == ':') count++;
break;
case OpType.PFCOUNT:
for (int i = 0; i < bytesRead; i++)
if (buf[i] == ':') count++;
break;
case OpType.ZADD:
case OpType.ZREM:
case OpType.ZADDREM:
case OpType.ZCARD:
case OpType.GEOADD:
case OpType.GEOADDREM:
case OpType.ZADDCARD:
case OpType.SETBIT:
case OpType.GETBIT:
case OpType.BITCOUNT:
case OpType.BITPOS:
for (int i = 0; i < bytesRead; i++)
if (buf[i] == ':') count++;
break;
case OpType.BITOP_AND:
case OpType.BITOP_OR:
case OpType.BITOP_XOR:
for (int i = 0; i < bytesRead; i++)
if (buf[i] == ':') count++;
break;
case OpType.BITOP_NOT:
for (int i = 0; i < bytesRead; i++)
if (buf[i] == ':') count += (1 + 1);
break;
case OpType.BITFIELD:
for (int i = 0; i < bytesRead; i++)
if (buf[i] == '*') count += bitfieldOpCount;
break;
case OpType.BITFIELD_GET:
case OpType.BITFIELD_SET:
case OpType.BITFIELD_INCR:
for (int i = 0; i < bytesRead; i++)
if (buf[i] == '*') count++;
break;
case OpType.XADD:
// XADD returns a bulk string with the stream ID
for (int i = 0; i < bytesRead; i++)
if (buf[i] == '$') count++;
break;
default:
break;
}
return (bytesRead, count);
}
private long LongRandom() => ((long)valueRandomGen.Next() << 32) | (long)valueRandomGen.Next();
private ulong ULongRandom()
{
ulong lsb = (ulong)(valueRandomGen.Next());
ulong msb = (ulong)(valueRandomGen.Next()) << 32;
return (msb | lsb);
}
private long RandomIntBitRange(int bitCount, bool signed)
{
if (signed)
{
long value = LongRandom();
value = (valueRandomGen.Next() & 0x1) == 0x1 ? -value : value;
value >>= (64 - bitCount);
return value;
}
else
{
ulong value = ULongRandom();
value >>= (64 - bitCount);
return (long)value;
}
}
private void RandomString()
{
if (numericValue)
{
for (int i = 0; i < valueBuffer.Length; i++)
{
// The first digit of the output string should not be zero.
if (i == 0)
valueBuffer[i] = ascii_digits[1 + valueRandomGen.Next(ascii_digits.Length - 1)];
else
valueBuffer[i] = ascii_digits[valueRandomGen.Next(ascii_digits.Length)];
}
}
else
{
for (int i = 0; i < valueBuffer.Length; i++)
valueBuffer[i] = ascii_chars[valueRandomGen.Next(ascii_chars.Length)];
}
}
}
}