From 03c488d40419578bd72655fcc5137332c97de28e Mon Sep 17 00:00:00 2001 From: Chris Fauerbach Date: Wed, 28 Mar 2012 17:52:29 -0400 Subject: [PATCH 1/2] adding support for unix sockets, in the -s parameter like redis-cli Changes made in .c files only. if a -s is supplied, it overrides the host port and host ip fields e.g. ./redis-stat -s /tmp/redisunix.socket --- redis-load.c | 14 +++++++++++--- redis-stat.c | 24 +++++++++++++++++++----- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/redis-load.c b/redis-load.c index 8939c04..6e2ff19 100644 --- a/redis-load.c +++ b/redis-load.c @@ -75,6 +75,7 @@ static struct config { int longtail; int longtail_order; char *hostip; + char *unixSocket; int hostport; int keepalive; long long start; @@ -150,8 +151,11 @@ static void clientDisconnected(const redisAsyncContext *context, int status) { static client createClient(void) { client c = zmalloc(sizeof(struct _client)); - - c->context = redisAsyncConnect(config.hostip,config.hostport); + if (config.unixSocket){ + c->context = redisAsyncConnectUnix(config.unixSocket); + }else{ + c->context = redisAsyncConnect(config.hostip,config.hostport); + } c->context->data = c; redisAsyncSetDisconnectCallback(c->context,clientDisconnected); if (c->context->err) { @@ -348,6 +352,7 @@ static void usage(char *wrong) { "Usage: redis-load ... options ...\n\n" " host Server hostname (default 127.0.0.1)\n" " port Server port (default 6379)\n" +" -s Unix socket to connect using\n" " clients Number of parallel connections (default 50)\n" " requests Total number of requests (default 10k)\n" " mindatasize Min data size of string values in bytes (default 1)\n" @@ -437,6 +442,9 @@ static void parseOptions(int argc, char **argv) { } else if (!strcmp(argv[i],"port") && !lastarg) { config.hostport = atoi(argv[i+1]); i++; + } else if (!strcmp(argv[i],"-s") && !lastarg) { + config.unixSocket = argv[i+1]; + i++; } else if (!strcmp(argv[i],"datasize") && !lastarg) { config.datasize_max = config.datasize_min = atoi(argv[i+1]); i++; @@ -565,7 +573,7 @@ int main(int argc, char **argv) { config.hostip = "127.0.0.1"; config.hostport = 6379; - + config.unixSocket = 0; parseOptions(argc,argv); config.databuf = zmalloc(config.datasize_max); diff --git a/redis-stat.c b/redis-stat.c index d50117a..d8a54a7 100644 --- a/redis-stat.c +++ b/redis-stat.c @@ -33,6 +33,7 @@ #define STAT_LATENCY 4 static struct config { + char *unixSocket; char *hostip; int hostport; redisContext *context; @@ -85,7 +86,11 @@ static redisReply *reconnectingCommand(const char *cmd) { fflush(stdout); redisFree(c); - c = redisConnect(config.hostip,config.hostport); + if (config.unixSocket){ + c = config.context = redisConnectUnix(config.unixSocket); + }else{ + c = redisConnect(config.hostip,config.hostport); + } usleep(config.delay*1000); } @@ -532,6 +537,7 @@ static void usage(char *wrong) { "Options:\n" " host Server hostname (default 127.0.0.1)\n" " port Server port (default 6379)\n" +" -s Unix socket to connect using\n" " delay Delay between requests (default: 1000 ms, 1 second).\n" " samplesize Number of keys to sample for 'vmpage' stat.\n" " logscale User power-of-two logarithmic scale in graphs.\n" @@ -545,10 +551,13 @@ static int parseOptions(int argc, char **argv) { for (i = 1; i < argc; i++) { int lastarg = (i == (argc-1)); - if (!strcmp(argv[i],"host") && !lastarg) { + if (!strcmp(argv[i],"-s" ) && !lastarg) { + config.unixSocket = argv[i+1]; + i++; + } else if (!strcmp(argv[i],"host" ) && !lastarg) { config.hostip = argv[i+1]; i++; - } else if (!strcmp(argv[i],"port") && !lastarg) { + } else if (!strcmp(argv[i],"port" ) && !lastarg) { config.hostport = atoi(argv[i+1]); i++; } else if (!strcmp(argv[i],"delay") && !lastarg) { @@ -572,6 +581,7 @@ static int parseOptions(int argc, char **argv) { } else if (!strcmp(argv[i],"help")) { usage(NULL); } else { + printf("Missing something? %s", argv[i]); usage(argv[i]); } } @@ -587,10 +597,14 @@ int main(int argc, char **argv) { config.delay = 1000; config.samplesize = 10000; config.logscale = 0; - + config.unixSocket = 0;//"/tmp/redis.sock"; parseOptions(argc,argv); - c = config.context = redisConnect(config.hostip,config.hostport); + if (config.unixSocket){ + c = config.context = redisConnectUnix(config.unixSocket); + }else{ + c = redisConnect(config.hostip,config.hostport); + } if (c->err) { fprintf(stderr, "Error connecting to Redis: %s\n", c->errstr); exit(1); From b508c38d33638f78d516ed6759af078b21b324a0 Mon Sep 17 00:00:00 2001 From: Chris Fauerbach Date: Thu, 29 Mar 2012 09:27:27 -0400 Subject: [PATCH 2/2] adding README --- README | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 README diff --git a/README b/README new file mode 100644 index 0000000..53db8b9 --- /dev/null +++ b/README @@ -0,0 +1,28 @@ +README +----------------------------- +This code was forked from the official redis-tools repository on March 28,2012. +The source for this can be found at: https://github.com/antirez/redis-tools + +Building +----------------------------- +To compile, simply: +make clean +make + +Running +---------------------------- +The only reason this was forked was to add a -s parameter to both redis-stat and redis-load. + +The -s parameter allows you to specify a unix socket in place of a TCP socket to execute, exactly like redis-cli + +e.g. +./redis-stat -s /tmp/redisunix.sock + +as long as you have the following in your redis.conf file + +unixsocket /tmp/redisunix.sock + + +Good luck! + +