forked from mrpi/redis-cplusplus-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis_single_witness_benchmark.cpp
401 lines (362 loc) · 12.8 KB
/
redis_single_witness_benchmark.cpp
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
/* Copyright (c) 2017 Stanford University
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <fcntl.h> /* For O_RDWR */
#include <unistd.h> /* For open(), creat() */
#include <signal.h>
#include <stdlib.h>
#include <cstdint>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <string>
#include <vector>
#include <thread>
#include "Atomic.h"
#include <sys/time.h>
#include <xmmintrin.h>
#include <cinttypes>
#include <typeinfo>
#include "redisclient.h"
#include "Cycles.h"
#include <iostream>
#include <atomic>
using RAMCloud::Cycles;
// Globals.
const char* hostIp = "10.10.102.101";
int objectSize = 100; // Number of bytes for value payload.
int count = 1000000; // How many repeat
int clientIndex = 0; // ClientIndex as in RAMCloud clusterPerf.
int threads = 50; // How many client threads per machine to run benchmark.
// used for throughput benchmark only.
int numWitness = 0;// send requests to witness as well as master.
bool gc = false;
redis::client* client;
//redis::client* multiClient[1000];
// Common helper functions.
uint64_t
generateRandom()
{
// Internal scratch state used by random_r 128 is the same size as
// initstate() uses for regular random(), see manpages for details.
// statebuf is malloc'ed and this memory is leaked, it could be a __thread
// buffer, but after running into linker issues with large thread local
// storage buffers, we thought better.
enum { STATE_BYTES = 128 };
static __thread char* statebuf;
// random_r's state, must be handed to each call, and seems to refer to
// statebuf in some undocumented way.
static __thread random_data buf;
if (statebuf == NULL) {
int fd = open("/dev/urandom", 00);
if (fd < 0) {
fprintf(stderr, "Couldn't open /dev/urandom");
exit(1);
}
unsigned int seed;
ssize_t bytesRead = read(fd, &seed, sizeof(seed));
close(fd);
assert(bytesRead == sizeof(seed));
statebuf = static_cast<char*>(malloc(STATE_BYTES));
initstate_r(seed, statebuf, STATE_BYTES, &buf);
}
// Each call to random returns 31 bits of randomness,
// so we need three to get 64 bits of randomness.
static_assert(RAND_MAX >= (1 << 31), "RAND_MAX too small");
int32_t lo, mid, hi;
random_r(&buf, &lo);
random_r(&buf, &mid);
random_r(&buf, &hi);
uint64_t r = (((uint64_t(hi) & 0x7FFFFFFF) << 33) | // NOLINT
((uint64_t(mid) & 0x7FFFFFFF) << 2) | // NOLINT
(uint64_t(lo) & 0x00000003)); // NOLINT
return r;
}
/**
* Generate a random string.
*
* \param str
* Pointer to location where the string generated will be stored.
* \param length
* Length of the string to be generated in bytes.
*/
void
genRandomString(char* str, const int length) {
static const char alphanum[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < length; ++i) {
str[i] = alphanum[generateRandom() % (sizeof(alphanum) - 1)];
}
str[length] = 0;
}
/**
* Given an integer value, generate a key of a given length
* that corresponds to that value.
*
* \param value
* Unique value to encapsulate in the key.
* \param length
* Total number of bytes in the resulting key. Must be at least 4.
* \param dest
* Memory block in which to write the key; must contain at
* least length bytes.
*/
void makeKey(int value, uint32_t length, char* dest)
{
memset(dest, 'x', length);
std::string str = std::to_string(value);
memcpy(dest, str.c_str(), str.size());
}
PerfUtils::Atomic<int64_t> writeThroughputTotalWrites(0);
void
writeThroughputRunner(int tid, bool gc) {
int numKeys = 2000000;
const uint16_t keyLength = 30;
char* key = new char[keyLength + 1];
char* value = new char[objectSize + 1];
redis::client* clientPtr = client;
if (tid != 0) {
std::vector<std::string> witnessIpsVec;
std::vector<int> witnessMasterIdx;
clientPtr = new redis::client(hostIp, witnessIpsVec, witnessMasterIdx);
}
//printf("New thread created! tid: %d clienId Assigned: %" PRIu64 ",
//rpcId: %" PRIu64 "\n", tid, multiClient[tid]->clientId, multiClient[tid]->lastRequestId);
uint64_t writeCount = 0;
std::vector<std::string> gcKeys;
std::vector<uint64_t> gcReqIds;
while(true) {
makeKey(static_cast<int>(generateRandom() % numKeys), keyLength, key);
genRandomString(value, objectSize);
std::string keyStr = std::string(key, keyLength);
clientPtr->witnessset(keyStr, std::string(value, objectSize));
writeCount++;
gcKeys.push_back(keyStr);
gcReqIds.push_back(clientPtr->lastRequestId);
if (writeCount % 1000 == 0) {
writeThroughputTotalWrites.add(1000);
if (gc) {
for (uint32_t ridx = 0; ridx < gcKeys.size(); ridx++) {
clientPtr->witnessgc(gcKeys.at(ridx), gcReqIds.at(ridx));
}
gcKeys.clear();
gcReqIds.clear();
}
}
}
}
void
writeThroughput(bool gc)
{
Cycles::init();
// Add startup delay.
int delayInSec = 3;
std::vector<std::thread> stdthreads;
int lastWriteTotal = 0;
uint64_t lastPrintTime = Cycles::rdtsc();
for (int tid = 0; tid < threads; ++tid) {
stdthreads.push_back(std::thread(&writeThroughputRunner, tid, gc));
Cycles::sleep(delayInSec*1000000);
// sleep(10);
uint64_t currentTime = Cycles::rdtsc();
printf("Started thread %d. Throughput: %7.2f kops/sec\n", tid,
(writeThroughputTotalWrites - lastWriteTotal) * 1e3 /
Cycles::toMicroseconds(currentTime - lastPrintTime));
lastPrintTime = currentTime;
lastWriteTotal = writeThroughputTotalWrites;
}
printf("All threads were started.\n");
while(true) {
Cycles::sleep(delayInSec*1000000);
uint64_t currentTime = Cycles::rdtsc();
printf("Total threads: %d. Throughput: %7.2f kops/sec\n", threads,
(writeThroughputTotalWrites - lastWriteTotal) * 1e3 /
Cycles::toMicroseconds(currentTime - lastPrintTime));
lastPrintTime = currentTime;
lastWriteTotal = writeThroughputTotalWrites;
}
}
// Write or overwrite randomly-chosen objects from a large table (so that there
// will be cache misses on the hash table and the object) and compute a
// cumulative distribution of write times.
void
writeDistRandom()
{
usleep(500);
int numKeys = 2000000;
if (clientIndex != 0)
return;
const uint16_t keyLength = 30;
char* key = new char[keyLength + 1];
char* value = new char[objectSize + 1];
for (int i = 0; i < 10000; ++i) {
makeKey(static_cast<int>(i), keyLength, key);
genRandomString(value, objectSize);
client->witnessset(std::string(key, keyLength), std::string(value, objectSize));
// TODO: use pipelining.
}
Cycles::init();
Cycles::sleep(10000);
// The following variable is used to stop the test after 10 seconds
// if we haven't read count keys by then.
uint64_t stop = Cycles::rdtsc() + Cycles::fromSeconds(300.0);
// Issue the writes back-to-back, and save the times.
std::vector<uint64_t> ticks;
ticks.resize(count);
for (int i = 0; i < count; i++) {
Cycles::sleep(3); // to give master time for syncing to backups.
// We generate the random number separately to avoid timing potential
// cache misses on the client side.
makeKey(static_cast<int>(generateRandom() % numKeys), keyLength, key);
genRandomString(value, objectSize);
// Do the benchmark
uint64_t start = Cycles::rdtsc();
client->witnessset(std::string(key, keyLength), std::string(value, objectSize));
uint64_t now = Cycles::rdtsc();
ticks.at(i) = now - start;
if (now >= stop) {
count = i+1;
break;
}
}
// Output the times (several comma-separated values on each line).
int valuesInLine = 0;
for (int i = 0; i < count; i++) {
if (valuesInLine >= 10) {
valuesInLine = 0;
printf("\n");
}
if (valuesInLine != 0) {
printf(",");
}
double micros = Cycles::toSeconds(ticks.at(i))*1.0e06;
printf("%.2f", micros);
valuesInLine++;
}
printf("\n");
PerfUtils::TimeTrace::print();
delete(key);
delete(value);
}
/**
* This function parses out the arguments intended for the thread library from
* a command line, and adjusts the values of argc and argv to eliminate the
* arguments that the thread library consumed.
*/
void
parseOptions(int argc, char* argv[]) {
if (argc == 0) return;
struct OptionSpecifier {
// The string that the user uses after `--`.
const char* optionName;
// The id for the option that is returned when it is recognized.
int id;
// Does the option take an argument?
bool takesArgument;
} optionSpecifiers[] = {
{"count", 'c', true},
{"clientIndex", 'i', true},
{"size", 's', true},
{"threads", 't', true},
{"witness", 'w', true}
};
const int UNRECOGNIZED = ~0;
int i = 0;
while (i < argc) {
if (argv[i][0] != '-' || argv[i][1] != '-') {
i++;
continue;
}
const char* optionName = argv[i] + 2;
int optionId = UNRECOGNIZED;
const char* optionArgument = NULL;
for (size_t k = 0;
k < sizeof(optionSpecifiers) / sizeof(OptionSpecifier); k++) {
const char* candidateName = optionSpecifiers[k].optionName;
bool needsArg = optionSpecifiers[k].takesArgument;
if (strncmp(candidateName,
optionName, strlen(candidateName)) == 0) {
if (needsArg) {
if (i + 1 >= argc) {
fprintf(stderr,
"Missing argument to option %s!\n",
candidateName);
break;
}
optionArgument = argv[i+1];
optionId = optionSpecifiers[k].id;
//argc -= 2;
i += 2;
} else {
optionId = optionSpecifiers[k].id;
//argc -= 1;
i++;
}
break;
}
}
switch (optionId) {
case 'c':
count = atoi(optionArgument);
break;
case 'i':
clientIndex = atoi(optionArgument);
break;
case 's':
objectSize = atoi(optionArgument);
break;
case 't':
threads = atoi(optionArgument);
break;
case 'w':
numWitness = atoi(optionArgument);
break;
case UNRECOGNIZED:
i++;
}
}
}
/* Catch Signal Handler functio */
void signal_callback_handler(int signum) {
((void) signum);
// printf("Caught signal SIGPIPE %d\n",signum);
}
int
main(int argc, char *argv[]) {
srand(std::time(NULL));
/* Catch Signal Handler SIGPIPE */
signal(SIGPIPE, signal_callback_handler);
parseOptions(argc, argv);
std::vector<std::string> witnessIpsVec;
std::vector<int> witnessMasterIdx;
redis::client realClient(hostIp, witnessIpsVec, witnessMasterIdx);
client = &realClient;
// for (int tid = 0; tid < threads; tid++) {
// multiClient[tid] = new redis::client(hostIp, witnessIpsVec, witnessMasterIdx);
// }
client->select(14);
client->flushdb();
if (strncmp("writeDistRandom", argv[1], 20) == 0) {
writeDistRandom();
} else if (strncmp("writeThroughput", argv[1], 20) == 0) {
writeThroughput(gc);
} else {
printf("no test was selected. (Provided argv[0]: %s\n", argv[0]);
}
// for (int tid = 0; tid < threads; tid++) {
// delete multiClient[tid];
// }
}