Created
August 14, 2017 07:48
-
-
Save sunxiaoguang/f92c705bff965eb6854da1e5ffb7b4af to your computer and use it in GitHub Desktop.
hgetall optimization sample data generator and benchmark code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <hiredis.h> | |
#include <sys/time.h> | |
long long elapsed(struct timeval *start, struct timeval *end) { | |
return (end->tv_sec * 1000000l + end->tv_usec) - (start->tv_sec * 1000000l + start->tv_usec); | |
} | |
void perf(redisContext *c, int loops, const char *command, const char *ocommand, const char *key, size_t ksize) { | |
int idx = 0; | |
long long oelapsed, nelapsed, boost; | |
struct timeval start, end; | |
redisReply *reply; | |
printf("===================== %s %s vs %s %s =====================\n", command, key, ocommand, key); | |
gettimeofday(&start, NULL); | |
for (idx = 0; idx < loops; ++ idx) { | |
redisAppendCommand(c,"%s %b",command,key,ksize); | |
} | |
for (idx = 0; idx < loops; ++idx) { | |
redisGetReply(c, (void **) &reply); | |
freeReplyObject(reply); | |
} | |
gettimeofday(&end, NULL); | |
printf("%d iterations of %s took %lld us\n",loops,command,oelapsed = elapsed(&start,&end)); | |
gettimeofday(&start, NULL); | |
for (idx = 0; idx < loops; ++ idx) { | |
redisAppendCommand(c,"%s %b",ocommand,key,ksize); | |
} | |
for (idx = 0; idx < loops; ++idx) { | |
redisGetReply(c,(void **)&reply); | |
freeReplyObject(reply); | |
} | |
gettimeofday(&end, NULL); | |
printf("%d iterations of %s took %lld us\n",loops,ocommand,nelapsed = elapsed(&start,&end)); | |
boost = oelapsed-nelapsed; | |
printf("%lld us/%.2g%% boost per iteration\n",boost/loops,((float)boost)*100/oelapsed); | |
} | |
int main(int argc, char **argv) { | |
int idx, kidx; | |
redisContext *c; | |
redisReply *reply; | |
const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1"; | |
int port = (argc > 2) ? atoi(argv[2]) : 16379; | |
struct timeval timeout = { 1, 500000 }; // 1.5 seconds | |
c = redisConnectWithTimeout(hostname,port,timeout); | |
if (c == NULL || c->err) { | |
if (c) { | |
printf("Connection error: %s\n", c->errstr); | |
redisFree(c); | |
} else { | |
printf("Connection error: can't allocate redis context\n"); | |
} | |
exit(1); | |
} | |
char key[32]; | |
long long oelapsed, nelapsed; | |
int loops = 1000; | |
for (kidx = 1; kidx <= 6; ++kidx) { | |
perf(c,loops,"hgetall","hgetallo",key,snprintf(key,sizeof(key),"h%d",kidx)); | |
perf(c,loops,"hkeys","hkeyso",key,snprintf(key,sizeof(key),"h%d",kidx)); | |
perf(c,loops,"hvals","hvalso",key,snprintf(key,sizeof(key),"h%d",kidx)); | |
} | |
/* Disconnects and frees the context */ | |
redisFree(c); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <hiredis.h> | |
#include <sys/time.h> | |
int main(int argc, char **argv) { | |
unsigned int idx; | |
redisContext *c; | |
redisReply *reply; | |
const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1"; | |
int port = (argc > 2) ? atoi(argv[2]) : 16379; | |
struct timeval timeout = { 1,500000 }; // 1.5 seconds | |
c = redisConnectWithTimeout(hostname,port,timeout); | |
if (c == NULL || c->err) { | |
if (c) { | |
printf("Connection error: %s\n",c->errstr); | |
redisFree(c); | |
} else { | |
printf("Connection error: can't allocate redis context\n"); | |
} | |
exit(1); | |
} | |
for (idx = 0; idx < 1000; ++ idx) { | |
freeReplyObject(redisCommand(c,"hset h1 %d %d",idx,idx + 1)); | |
} | |
for (idx = 0; idx < 2000; ++ idx) { | |
freeReplyObject(redisCommand(c,"hset h2 %d %d",idx,idx + 1)); | |
} | |
for (idx = 0; idx < 4000; ++ idx) { | |
freeReplyObject(redisCommand(c,"hset h3 %d %d",idx,idx + 1)); | |
} | |
for (idx = 0; idx < 8000; ++ idx) { | |
freeReplyObject(redisCommand(c,"hset h4 %d %d",idx,idx + 1)); | |
} | |
for (idx = 0; idx < 16000; ++ idx) { | |
freeReplyObject(redisCommand(c,"hset h5 %d %d",idx,idx + 1)); | |
} | |
for (idx = 0; idx < 20000; ++ idx) { | |
freeReplyObject(redisCommand(c,"hset h6 %d %d",idx,idx + 1)); | |
} | |
/* Disconnects and frees the context */ | |
redisFree(c); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment