Skip to content

Instantly share code, notes, and snippets.

@dspezia
Created October 8, 2011 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dspezia/1272522 to your computer and use it in GitHub Desktop.
Save dspezia/1272522 to your computer and use it in GitHub Desktop.
Redis benchmark program for huge pages testing
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "hiredis.h"
#define CHECK(X) if ( !X || X->type == REDIS_REPLY_ERROR ) { printf("Error\n"); exit(-1); }
void fill( redisContext *c, int n )
{
redisReply *reply;
unsigned int i,j,k;
unsigned int count = 0;
reply = redisCommand(c,"SELECT %d",n);
CHECK(reply);
freeReplyObject(reply);
reply = redisCommand(c,"FLUSHDB");
CHECK(reply);
freeReplyObject(reply);
for (i=0; i<1000000; ++i )
{
char key[32];
char val[1024];
int cmd = 0;
for (j=0; j<10; ++j )
{
++count;
sprintf( key,"%08X",count);
for ( k=0; k<j*10; ++k ) val[k] = 'a' + k%26;
val[k] = 0;
redisAppendCommand(c,"SET %s %s",key,val );
redisAppendCommand(c,"LPUSH in%d %s", j, key);
redisAppendCommand(c,"RPUSH out%d %s", j, key);
redisAppendCommand(c,"SADD s%d %s", j, key );
redisAppendCommand(c,"HMSET h%d %s %s", j, key, val );
cmd += 5;
}
while ( cmd-- > 0 )
{
int r = redisGetReply(c, (void **) &reply );
if ( r == REDIS_ERR ) { printf("Error\n"); exit(-1); }
CHECK(reply);
freeReplyObject(reply);
}
}
}
void query( redisContext *c, int n )
{
redisReply *reply;
unsigned int i,j,k;
unsigned int count = 0;
reply = redisCommand(c,"SELECT %d",n);
CHECK(reply);
freeReplyObject(reply);
for (i=0; i<1000000; ++i )
{
char key[32];
char val[1024];
int cmd = 0;
for (j=0; j<10; ++j )
{
count = rand() % 10000000;
sprintf( key,"%08X",count);
redisAppendCommand(c,"GET %s",key );
redisAppendCommand(c,"LRANGE in%d %d %d", j, 0, j );
redisAppendCommand(c,"LRANGE out%d %d %d", j, -j-1, -1 );
redisAppendCommand(c,"SRANDMEMBER s%d", j );
redisAppendCommand(c,"HGET h%d %s", j, key );
cmd += 5;
}
while ( cmd-- > 0 )
{
int r = redisGetReply(c, (void **) &reply);
if ( r == REDIS_ERR ) { printf("Error\n"); exit(-1); }
CHECK(reply);
freeReplyObject(reply);
}
}
}
void update( redisContext *c, int n )
{
redisReply *reply;
unsigned int i,j,k;
unsigned int count = 0;
reply = redisCommand(c,"SELECT %d",n);
CHECK(reply);
freeReplyObject(reply);
for (i=0; i<1000000; ++i )
{
char key[32];
char val[1024];
int cmd = 0;
for (j=0; j<10; ++j )
{
count = rand() % 10000000;
sprintf( key,"%08X",count);
redisAppendCommand(c,"SET %s %s",key, key );
redisAppendCommand(c,"SPOP s%d", j );
cmd += 2;
}
while ( cmd-- > 0 )
{
int r = redisGetReply(c, (void **) &reply);
if ( r == REDIS_ERR ) { printf("Error\n"); exit(-1); }
CHECK(reply);
freeReplyObject(reply);
}
sleep(1);
}
}
const char *CRLF="\r\n";
const char *USED="used_memory_human:";
const char *CMD ="total_commands_processed:";
void redisTop( redisContext *c )
{
redisReply *reply;
char *p1, *p2;
char bU[32], bC[32];
long nCmd, nCmdPrev,n;
nCmd = nCmdPrev = n = 0;
for (;;)
{
reply = redisCommand(c,"INFO");
CHECK(reply);
if ( reply->type == REDIS_REPLY_STRING )
{
p1 = strstr(reply->str, USED );
if (!p1) continue;
p1 += strlen(USED);
p2 = strstr(p1,CRLF);
memcpy(bU,p1,p2-p1);
bU[p2-p1] = 0;
p1 = strstr(p2, CMD );
if (!p1) continue;
p1 += strlen(CMD);
p2 = strstr(p1,CRLF);
memcpy(bC,p1,p2-p1);
bC[p2-p1] = 0;
nCmd = atol(bC);
if ( n )
printf("%-30.30s %-30.30s %10d\n", bU, bC, nCmd-nCmdPrev );
nCmdPrev = nCmd;
++n;
}
freeReplyObject(reply);
sleep(1);
}
}
int main(int argc, char *argv[] ) {
redisContext *c;
int n = 0;
int db = 0;
if ( argc != 3 )
{
fprintf(stderr,"Usage: %s num db\n",argv[0]);
exit(-1);
}
n = atoi(argv[1]);
db = atoi(argv[2]);
c = redisConnectUnix("/tmp/redis.sock");
if (c->err) {
printf("Connection error: %s\n", c->errstr);
exit(1);
}
switch(n)
{
case 0:
fill(c,db);
break;
case 1:
query(c,db);
break;
case 2:
update(c,db);
break;
case 3:
redisTop(c);
break;
}
redisFree(c);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment