/
test_set_get_delete.c
87 lines (70 loc) · 1.86 KB
/
test_set_get_delete.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
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <getopt.h>
#include "libmemcached/memcached.h"
#define KEY_PREFIX "key"
#define VAL_PREFIX "val"
void usage(void)
{
printf("usage: ./test_set_get_delete <host> <port> <num>\n");
exit(1);
}
void pexit(const char* msg)
{
perror(msg);
exit(1);
}
int main(int argc, char* argv[])
{
if(argc < 4) { usage(); }
char* host = argv[1];
unsigned short port = atoi(argv[2]);
if(port == 0) { usage(); }
uint32_t num = atoi(argv[3]);
if(num == 0) { usage(); }
memcached_st* mc = memcached_create(NULL);
if(mc == NULL) { pexit("memcached_create"); }
memcached_server_add(mc, host, port);
// memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
char kbuf[strlen(KEY_PREFIX) + 11];
char vbuf[strlen(VAL_PREFIX) + 11];
while(1) {
uint32_t i;
for(i=0; i < num; ++i) {
int klen = sprintf(kbuf, KEY_PREFIX "%d", i);
int vlen = sprintf(vbuf, VAL_PREFIX "%d", i);
printf("set '%s' = '%s'\n", kbuf, vbuf);
memcached_set(mc, kbuf, klen, vbuf, vlen, 0, 0);
}
for(i=0; i < num; ++i) {
int klen = sprintf(kbuf, KEY_PREFIX "%d", i);
int vlen = sprintf(vbuf, VAL_PREFIX "%d", i);
size_t vallen;
uint32_t flags;
memcached_return rc;
char* val = memcached_get(mc, kbuf, klen, &vallen, &flags, &rc);
if(!val) {
fprintf(stderr, "** key '%s' not found **\n", kbuf);
} else if(vallen != vlen || memcmp(val, vbuf, vlen) != 0) {
fprintf(stderr, "** key '%s' not match ** '", kbuf);
fwrite(val, vallen, 1, stderr);
fprintf(stderr, "'\n");
} else {
printf("get '%s' = '", kbuf);
fwrite(val, vallen, 1, stdout);
printf("'\n");
}
}
for(i=0; i < num; ++i) {
int klen = sprintf(kbuf, KEY_PREFIX "%d", i);
printf("delete '%s'\n", kbuf);
memcached_delete(mc, kbuf, klen, 0);
}
}
return 0;
}