Permalink
Fetching contributors…
Cannot retrieve contributors at this time
101 lines (81 sloc) 2.23 KB
#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: ./check_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) {
printf("s"); fflush(stdout);
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);
memcached_return ret = memcached_set(mc, kbuf, klen, vbuf, vlen, 0, 0);
if(ret != MEMCACHED_SUCCESS) {
fprintf(stderr, "set failed %d '%s'\n", ret, kbuf);
}
}
printf("g"); fflush(stdout);
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 || vallen != vlen || memcmp(val, vbuf, vlen) != 0) {
fprintf(stderr, "false NEGATIVE get '%s'\n", kbuf);
}
}
printf("d"); fflush(stdout);
for(i=0; i < num; ++i) {
int klen = sprintf(kbuf, KEY_PREFIX "%d", i);
memcached_return ret = memcached_delete(mc, kbuf, klen, 0);
if(ret != MEMCACHED_SUCCESS) {
fprintf(stderr, "delete not performed %d '%s'\n", ret, kbuf);
}
}
printf("g"); fflush(stdout);
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, "false POSITIVE get '%s'\n", kbuf);
}
}
}
return 0;
}