Skip to content

Instantly share code, notes, and snippets.

@sunxiaoguang
Created August 14, 2017 07:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunxiaoguang/f92c705bff965eb6854da1e5ffb7b4af to your computer and use it in GitHub Desktop.
Save sunxiaoguang/f92c705bff965eb6854da1e5ffb7b4af to your computer and use it in GitHub Desktop.
hgetall optimization sample data generator and benchmark code
#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;
}
#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