mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-23 00:28:26 -05:00
Microsecond latency resolution in redis-benchmark
This commit is contained in:
parent
ec8f06675a
commit
8146e31677
@ -45,17 +45,10 @@
|
|||||||
#include "adlist.h"
|
#include "adlist.h"
|
||||||
#include "zmalloc.h"
|
#include "zmalloc.h"
|
||||||
|
|
||||||
#define REPLY_INT 0
|
|
||||||
#define REPLY_RETCODE 1
|
|
||||||
#define REPLY_BULK 2
|
|
||||||
#define REPLY_MBULK 3
|
|
||||||
|
|
||||||
#define CLIENT_CONNECTING 0
|
#define CLIENT_CONNECTING 0
|
||||||
#define CLIENT_SENDQUERY 1
|
#define CLIENT_SENDQUERY 1
|
||||||
#define CLIENT_READREPLY 2
|
#define CLIENT_READREPLY 2
|
||||||
|
|
||||||
#define MAX_LATENCY 5000
|
|
||||||
|
|
||||||
#define REDIS_NOTUSED(V) ((void) V)
|
#define REDIS_NOTUSED(V) ((void) V)
|
||||||
|
|
||||||
static struct config {
|
static struct config {
|
||||||
@ -75,7 +68,7 @@ static struct config {
|
|||||||
int keepalive;
|
int keepalive;
|
||||||
long long start;
|
long long start;
|
||||||
long long totlatency;
|
long long totlatency;
|
||||||
int *latency;
|
long long *latency;
|
||||||
char *title;
|
char *title;
|
||||||
list *clients;
|
list *clients;
|
||||||
int quiet;
|
int quiet;
|
||||||
@ -89,7 +82,8 @@ typedef struct _client {
|
|||||||
sds obuf;
|
sds obuf;
|
||||||
unsigned int written; /* bytes of 'obuf' already written */
|
unsigned int written; /* bytes of 'obuf' already written */
|
||||||
int replytype;
|
int replytype;
|
||||||
long long start; /* start time in milliseconds */
|
long long start; /* start time of a request */
|
||||||
|
long long latency; /* request latency */
|
||||||
} *client;
|
} *client;
|
||||||
|
|
||||||
/* Prototypes */
|
/* Prototypes */
|
||||||
@ -97,6 +91,16 @@ static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask);
|
|||||||
static void createMissingClients(client c);
|
static void createMissingClients(client c);
|
||||||
|
|
||||||
/* Implementation */
|
/* Implementation */
|
||||||
|
static long long ustime(void) {
|
||||||
|
struct timeval tv;
|
||||||
|
long long ust;
|
||||||
|
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
ust = ((long)tv.tv_sec)*1000000;
|
||||||
|
ust += tv.tv_usec;
|
||||||
|
return ust;
|
||||||
|
}
|
||||||
|
|
||||||
static long long mstime(void) {
|
static long long mstime(void) {
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
long long mst;
|
long long mst;
|
||||||
@ -136,7 +140,8 @@ static void resetClient(client c) {
|
|||||||
aeCreateFileEvent(config.el,c->context->fd,AE_WRITABLE,writeHandler,c);
|
aeCreateFileEvent(config.el,c->context->fd,AE_WRITABLE,writeHandler,c);
|
||||||
c->written = 0;
|
c->written = 0;
|
||||||
c->state = CLIENT_SENDQUERY;
|
c->state = CLIENT_SENDQUERY;
|
||||||
c->start = mstime();
|
c->start = ustime();
|
||||||
|
c->latency = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void randomizeClientKey(client c) {
|
static void randomizeClientKey(client c) {
|
||||||
@ -153,12 +158,6 @@ static void randomizeClientKey(client c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void clientDone(client c) {
|
static void clientDone(client c) {
|
||||||
long long latency;
|
|
||||||
config.donerequests ++;
|
|
||||||
latency = mstime() - c->start;
|
|
||||||
if (latency > MAX_LATENCY) latency = MAX_LATENCY;
|
|
||||||
config.latency[latency]++;
|
|
||||||
|
|
||||||
if (config.donerequests == config.requests) {
|
if (config.donerequests == config.requests) {
|
||||||
freeClient(c);
|
freeClient(c);
|
||||||
aeStop(config.el);
|
aeStop(config.el);
|
||||||
@ -182,6 +181,11 @@ static void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
|
|||||||
REDIS_NOTUSED(fd);
|
REDIS_NOTUSED(fd);
|
||||||
REDIS_NOTUSED(mask);
|
REDIS_NOTUSED(mask);
|
||||||
|
|
||||||
|
/* Calculate latency only for the first read event. This means that the
|
||||||
|
* server already sent the reply and we need to parse it. Parsing overhead
|
||||||
|
* is not part of the latency, so calculate it only once, here. */
|
||||||
|
if (c->latency < 0) c->latency = ustime()-(c->start);
|
||||||
|
|
||||||
if (redisBufferRead(c->context) != REDIS_OK) {
|
if (redisBufferRead(c->context) != REDIS_OK) {
|
||||||
fprintf(stderr,"Error: %s\n",c->context->errstr);
|
fprintf(stderr,"Error: %s\n",c->context->errstr);
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -190,10 +194,13 @@ static void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
|
|||||||
fprintf(stderr,"Error: %s\n",c->context->errstr);
|
fprintf(stderr,"Error: %s\n",c->context->errstr);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if (reply != NULL)
|
if (reply != NULL) {
|
||||||
|
if (config.donerequests < config.requests)
|
||||||
|
config.latency[config.donerequests++] = c->latency;
|
||||||
clientDone(c);
|
clientDone(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
|
static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
|
||||||
client c = privdata;
|
client c = privdata;
|
||||||
@ -203,7 +210,8 @@ static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
|
|||||||
|
|
||||||
if (c->state == CLIENT_CONNECTING) {
|
if (c->state == CLIENT_CONNECTING) {
|
||||||
c->state = CLIENT_SENDQUERY;
|
c->state = CLIENT_SENDQUERY;
|
||||||
c->start = mstime();
|
c->start = ustime();
|
||||||
|
c->latency = -1;
|
||||||
}
|
}
|
||||||
if (sdslen(c->obuf) > c->written) {
|
if (sdslen(c->obuf) > c->written) {
|
||||||
void *ptr = c->obuf+c->written;
|
void *ptr = c->obuf+c->written;
|
||||||
@ -258,8 +266,12 @@ static void createMissingClients(client c) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int compareLatency(const void *a, const void *b) {
|
||||||
|
return (*(long long*)a)-(*(long long*)b);
|
||||||
|
}
|
||||||
|
|
||||||
static void showLatencyReport(void) {
|
static void showLatencyReport(void) {
|
||||||
int j, seen = 0;
|
int i, curlat = 0;
|
||||||
float perc, reqpersec;
|
float perc, reqpersec;
|
||||||
|
|
||||||
reqpersec = (float)config.donerequests/((float)config.totlatency/1000);
|
reqpersec = (float)config.donerequests/((float)config.totlatency/1000);
|
||||||
@ -271,11 +283,13 @@ static void showLatencyReport(void) {
|
|||||||
printf(" %d bytes payload\n", config.datasize);
|
printf(" %d bytes payload\n", config.datasize);
|
||||||
printf(" keep alive: %d\n", config.keepalive);
|
printf(" keep alive: %d\n", config.keepalive);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
for (j = 0; j <= MAX_LATENCY; j++) {
|
|
||||||
if (config.latency[j]) {
|
qsort(config.latency,config.requests,sizeof(long long),compareLatency);
|
||||||
seen += config.latency[j];
|
for (i = 0; i < config.requests; i++) {
|
||||||
perc = ((float)seen*100)/config.donerequests;
|
if (config.latency[i]/1000 != curlat || i == (config.requests-1)) {
|
||||||
printf("%.2f%% <= %d milliseconds\n", perc, j);
|
curlat = config.latency[i]/1000;
|
||||||
|
perc = ((float)(i+1)*100)/config.requests;
|
||||||
|
printf("%.2f%% <= %d milliseconds\n", perc, curlat);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("%.2f requests per second\n\n", reqpersec);
|
printf("%.2f requests per second\n\n", reqpersec);
|
||||||
@ -285,7 +299,6 @@ static void showLatencyReport(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void prepareForBenchmark(char *title) {
|
static void prepareForBenchmark(char *title) {
|
||||||
memset(config.latency,0,sizeof(int)*(MAX_LATENCY+1));
|
|
||||||
config.title = title;
|
config.title = title;
|
||||||
config.start = mstime();
|
config.start = mstime();
|
||||||
config.donerequests = 0;
|
config.donerequests = 0;
|
||||||
@ -400,13 +413,12 @@ int main(int argc, char **argv) {
|
|||||||
config.idlemode = 0;
|
config.idlemode = 0;
|
||||||
config.latency = NULL;
|
config.latency = NULL;
|
||||||
config.clients = listCreate();
|
config.clients = listCreate();
|
||||||
config.latency = zmalloc(sizeof(int)*(MAX_LATENCY+1));
|
|
||||||
|
|
||||||
config.hostip = "127.0.0.1";
|
config.hostip = "127.0.0.1";
|
||||||
config.hostport = 6379;
|
config.hostport = 6379;
|
||||||
config.hostsocket = NULL;
|
config.hostsocket = NULL;
|
||||||
|
|
||||||
parseOptions(argc,argv);
|
parseOptions(argc,argv);
|
||||||
|
config.latency = zmalloc(sizeof(long long)*config.requests);
|
||||||
|
|
||||||
if (config.keepalive == 0) {
|
if (config.keepalive == 0) {
|
||||||
printf("WARNING: keepalive disabled, you probably need 'echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse' for Linux and 'sudo sysctl -w net.inet.tcp.msl=1000' for Mac OS X in order to use a lot of clients/requests\n");
|
printf("WARNING: keepalive disabled, you probably need 'echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse' for Linux and 'sudo sysctl -w net.inet.tcp.msl=1000' for Mac OS X in order to use a lot of clients/requests\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user