基于Redis的C客户端Hiredis的一个测试代码

安装过程见 :

http://blog.csdn.net/a600423444/article/details/7203276

 

自己写的一个测试代码,主要是set ,get和del功能

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

#include "hiredis.h"

static inline double micro_time(){
struct timeval tim;
double ret;
gettimeofday(&tim, NULL);
ret = tim.tv_sec+(tim.tv_usec/1000000.0);
return ret;
}

 

int main(void) {
unsigned int j;
redisContext *c;
redisReply *reply;

long i;
long loopTime=300000;



srand ( time(NULL) );

struct timeval timeout = { 1, 500000 }; // 1.5 seconds
c = redisConnectWithTimeout((char*)"127.0.0.2", 6379, timeout);
if (c->err) {
printf("Connection error: %s\n", c->errstr);
exit(1);
}


double start = micro_time();
double setCost=0.0;
double delCost=0.0;


printf("start test ----------%ld\n\n",loopTime);
for(i=0;i<loopTime;i++){
char str[25];
sprintf(str,"%ld",i);

reply = redisCommand(c,"SET %s %s", str, str);
// printf("SET: %s\n", reply->str);
freeReplyObject(reply);
}

printf("set over \n");

for(i=0;i<loopTime;i++){
char str[25];
sprintf(str,"%ld",i);

reply = redisCommand(c,"GET %s",str);
printf("GET : %s\n", reply->str);
freeReplyObject(reply);
}

for(i=0;i<loopTime;i++){
char str[25];
sprintf(str,"%ld",i);

reply = redisCommand(c,"DEL %s", str);
//printf("DEL (binary API): %s\n", reply->str);
freeReplyObject(reply);
}

printf("del over \n");



delCost = micro_time()-start;
printf("total use time : %f\n\n",delCost);


return 0;
}

posted on 2012-05-14 14:37  aitilang  阅读(757)  评论(0编辑  收藏  举报

导航