-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemcached-benchmark.c
More file actions
352 lines (279 loc) · 6.67 KB
/
memcached-benchmark.c
File metadata and controls
352 lines (279 loc) · 6.67 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#define _GNU_SOURCE
#include <stdarg.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <pthread.h>
#include <limits.h>
#include <sys/time.h>
static void die(const char *err, ...)
{
va_list params;
va_start(params, err);
verrx(EXIT_FAILURE, err, params);
va_end(params);
}
static void *xmalloc(size_t size)
{
void *ptr;
ptr = malloc(size);
if (!ptr)
die("malloc: out of memory");
return ptr;
}
static void xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(start_routine)(void*), void *arg)
{
int ret;
ret = pthread_create(thread, attr, start_routine, arg);
if (ret)
die("pthread_create: %d", ret);
}
static int tcp_nodelay;
#ifdef CHUNKD_BENCHMARK
#include <chunkc.h>
static void do_chunkd_set(struct st_client *stc, const char *key,
size_t key_length, char *value, size_t value_length)
{
bool ret;
ret = stc_put_inline(stc, key, key_length, value, value_length, 0);
if (!ret)
warnx("stc_put failed");
}
static void do_chunkd_get(struct st_client *stc, const char *key,
size_t key_length)
{
char *value;
size_t value_length;
value = stc_get_inline(stc, key, key_length, &value_length);
if (!value) {
warnx("stc_get failed");
return;
}
free(value);
}
static struct st_client *stc_new2(const char *server)
{
char *host;
char *port;
host = strdupa(server);
host = strtok(host, ":");
if (!host)
return NULL;
port = strtok(NULL, ":");
if (!port)
return NULL;
return stc_new(host, atoi(port), "testuser", "testuser", false);
}
static void run(const int id, const char *server, const int value_length,
const int requests, const int command)
{
char key[20];
char *value;
int i;
struct st_client *stc;
bool ret;
value = xmalloc(value_length);
memset(value, '*', value_length);
stc = stc_new2(server);
if (!stc)
die("stc_new failed");
ret = stc_table_openz(stc, "testtable",
command == 'w' ? CHF_TBL_CREAT : 0);
if (!ret)
die("stc_table_openz failed");
for (i = 0; i < requests; i++) {
sprintf(key, "%04d-%011d", id, i);
if (command == 'w')
do_chunkd_set(stc, key, 16, value, value_length);
else
do_chunkd_get(stc, key, 16);
}
stc_free(stc);
free(value);
}
#else /* CHUNKD_BENCHMARK */
#include <libmemcached/memcached.h>
static void do_memcached_set(memcached_st *memc, const char *key,
size_t key_length, const char *value, size_t value_length)
{
memcached_return ret;
ret = memcached_set(memc, key, key_length, value, value_length, 0, 0);
if (ret != MEMCACHED_SUCCESS)
warnx("memcached_set: %s", memcached_strerror(memc, ret));
}
static void do_memcached_get(memcached_st *memc, const char *key,
size_t key_length)
{
memcached_return ret;
char *value;
size_t value_length;
uint32_t flags;
value = memcached_get(memc, key, key_length, &value_length,
&flags, &ret);
if (ret != MEMCACHED_SUCCESS) {
warnx("memcached_get: %s", memcached_strerror(memc, ret));
return;
}
free(value);
}
static void run(const int id, const char *server, const int value_length,
const int requests, const int command)
{
char key[20];
char *value;
struct memcached_st memc;
struct memcached_server_st *servers;
memcached_return ret;
int i;
value = xmalloc(value_length);
memset(value, '*', value_length);
memcached_create(&memc);
servers = memcached_servers_parse(server);
ret = memcached_server_push(&memc, servers);
if (ret != MEMCACHED_SUCCESS)
die("memcached_server_push: %d", ret);
if (tcp_nodelay) {
ret = memcached_behavior_set(&memc,
MEMCACHED_BEHAVIOR_TCP_NODELAY, 1);
if (ret != MEMCACHED_SUCCESS)
die("memcached_behavior_set: %d", ret);
}
for (i = 0; i < requests; i++) {
sprintf(key, "%04d-%011d", id, i);
if (command == 'w')
do_memcached_set(&memc, key, 16, value, value_length);
else
do_memcached_get(&memc, key, 16);
}
memcached_server_list_free(servers);
memcached_free(&memc);
free(value);
}
#endif /* CHUNKD_BENCHMARK */
static unsigned long value_length = 100UL;
static unsigned long requests = 100000UL;
static char *server = "127.0.0.1:21201";
/* 'r' for Read, 'w' for Write request */
static int command = 'w';
static unsigned int threads = 8;
static int verbose;
static void parse_options(int argc, char **argv)
{
int c;
while ((c = getopt(argc, argv, "n:l:s:t:rwvd")) != -1) {
switch(c) {
case 'n':
requests = atol(optarg);
break;
case 'l':
value_length = atol(optarg);
break;
case 's':
server = optarg;
break;
case 't':
threads = atoi(optarg);
break;
case 'r':
case 'w':
command = c;
break;
case 'v':
verbose = 1;
break;
case 'd':
tcp_nodelay = 1;
break;
default:
die("invalid option: %c", c);
}
}
}
struct benchmark_thread_data {
int id;
long time_ms;
};
static long time_diff(struct timeval a, struct timeval b)
{
int us, s;
us = (int)(a.tv_usec - b.tv_usec);
s = (int)(a.tv_sec - b.tv_sec);
return s * 1000 + us / 1000;
}
static void *benchmark_thread(void *arg)
{
struct benchmark_thread_data *data = arg;
struct timeval start, end;
gettimeofday(&start, NULL);
run(data->id, server, value_length, requests, command);
gettimeofday(&end, NULL);
data->time_ms = time_diff(end, start);
return NULL;
}
static void wait_threads(pthread_t *threads, int n)
{
int i;
for (i = 0; i < n; i++) {
void *value;
int ret;
ret = pthread_join(threads[i], &value);
if (ret) {
warnx("pthread_join: %d", ret);
continue;
}
if (value != NULL)
warnx("thread returns non NULL value %p", value);
}
}
static void benchmark(void)
{
int i;
long sum = 0;
long min_ms = LONG_MAX, max_ms = 0, avg_ms;
pthread_t *tid;
struct benchmark_thread_data *data;
#ifdef CHUNKD_BENCHMARK
stc_init();
#endif
tid = xmalloc(sizeof(tid[0]) * threads);
data = xmalloc(sizeof(data[0]) * threads);
for (i = 0; i < threads; i++) {
data[i].id = i;
xpthread_create(&tid[i], NULL, benchmark_thread, &data[i]);
}
wait_threads(tid, threads);
#define _MIN(a, b) ((a) < (b) ? (a) : (b))
#define _MAX(a, b) ((a) < (b) ? (b) : (a))
for (i = 0; i < threads; i++) {
long ms = data[i].time_ms;
sum += ms;
min_ms = _MIN(min_ms, ms);
max_ms = _MAX(max_ms, ms);
}
avg_ms = sum / threads;
printf("%d %ld.%03ld %ld.%03ld %ld.%03ld\n", threads,
avg_ms / 1000, avg_ms % 1000,
min_ms / 1000, min_ms % 1000,
max_ms / 1000, max_ms % 1000);
if (verbose) {
unsigned long long total_bytes;
unsigned long long bytes_per_msec;
total_bytes = value_length;
total_bytes *= threads;
total_bytes *= requests;
bytes_per_msec = total_bytes / avg_ms;
printf("Throughput: %llu KB/sec\n",
bytes_per_msec * 1000UL / 1024UL);
}
free(data);
free(tid);
}
int main(int argc, char **argv)
{
parse_options(argc, argv);
benchmark();
return 0;
}