-
Notifications
You must be signed in to change notification settings - Fork 0
/
multimap-memcachedb-test.c
138 lines (110 loc) · 2.93 KB
/
multimap-memcachedb-test.c
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
#define _GNU_SOURCE
#include <stdarg.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <limits.h>
#include <sys/time.h>
#include <libmemcached/memcached.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 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));
}
/*
* delete, set, mget
*/
static void run(const char *server, unsigned long requests,
unsigned long value_length)
{
struct memcached_st memc;
struct memcached_server_st *servers;
const char *key = "key";
const size_t key_len = strlen(key);
char *value = xmalloc(value_length);
memcached_return ret;
int i;
memcached_create(&memc);
servers = memcached_servers_parse(server);
ret = memcached_server_push(&memc, servers);
if (ret != MEMCACHED_SUCCESS)
die("memcached_server_push: %s", memcached_strerror(&memc, ret));
ret = memcached_delete(&memc, key, key_len, 0);
if (ret != MEMCACHED_SUCCESS)
warnx("memcached_delete: %s", memcached_strerror(&memc, ret));
for (i = 0; i < requests; i++)
do_memcached_set(&memc, key, key_len, value, value_length);
const char *keys[] = { key, };
const size_t key_lens[] = { key_len, };
ret = memcached_mget(&memc, keys, key_lens, 1);
if (ret != MEMCACHED_SUCCESS)
warnx("memcached_mget: %s", memcached_strerror(&memc, ret));
char *v;
size_t length;
uint32_t flags;
for (i = 0; i < requests; i++) {
v = memcached_fetch(&memc, NULL, NULL, &length, &flags, &ret);
if (ret != MEMCACHED_SUCCESS)
break;
if (value_length != length)
warnx("unexpected value length: %zd\n", length);
free(v);
}
if (i != requests)
warnx("%zd records lost", requests - i);
v = memcached_fetch(&memc, NULL, NULL, &length, &flags, &ret);
if (ret == MEMCACHED_SUCCESS)
warnx("unexpected record");
else if (ret != MEMCACHED_END)
warnx("unexpected error: %s", memcached_strerror(&memc, ret));
memcached_server_list_free(servers);
memcached_free(&memc);
}
static unsigned long value_length = 100UL;
static unsigned long requests = 10000UL;
static char *server = "127.0.0.1:21201";
static void parse_options(int argc, char **argv)
{
int c;
while ((c = getopt(argc, argv, "n:l:s:")) != -1) {
switch(c) {
case 'n':
requests = atol(optarg);
break;
case 'l':
value_length = atol(optarg);
break;
case 's':
server = optarg;
break;
default:
die("invalid option: %c", c);
}
}
}
int main(int argc, char **argv)
{
parse_options(argc, argv);
run(server, requests, value_length);
return 0;
}