Skip to content

Instantly share code, notes, and snippets.

@antirez
Created November 27, 2015 17:39
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 antirez/31d4fd73ef18834414e9 to your computer and use it in GitHub Desktop.
Save antirez/31d4fd73ef18834414e9 to your computer and use it in GitHub Desktop.
diff --git a/src/redis-cli.c b/src/redis-cli.c
index be37a91..82b8e04 100644
--- a/src/redis-cli.c
+++ b/src/redis-cli.c
@@ -1263,8 +1263,9 @@ static int evalMode(int argc, char **argv) {
* Latency and latency history modes
*--------------------------------------------------------------------------- */
-#define LATENCY_SAMPLE_RATE 10 /* milliseconds. */
+#define LATENCY_SAMPLE_RATE 1 /* milliseconds. */
#define LATENCY_HISTORY_DEFAULT_INTERVAL 15000 /* milliseconds. */
+#define LATENCY_BRUST_SIZE 1000
static void latencyMode(void) {
redisReply *reply;
long long start, latency, min = 0, max = 0, tot = 0, count = 0;
@@ -1273,6 +1274,7 @@ static void latencyMode(void) {
LATENCY_HISTORY_DEFAULT_INTERVAL;
double avg;
long long history_start = mstime();
+ int req = 0;
if (!context) exit(1);
while(1) {
@@ -1284,26 +1286,38 @@ static void latencyMode(void) {
}
latency = mstime()-start;
freeReplyObject(reply);
- count++;
- if (count == 1) {
- min = max = tot = latency;
- avg = (double) latency;
- } else {
- if (latency < min) min = latency;
- if (latency > max) max = latency;
- tot += latency;
- avg = (double) tot/count;
+
+ if (req > 10 && req < LATENCY_BRUST_SIZE-10) {
+ count++;
+ if (count == 1) {
+ min = max = tot = latency;
+ avg = (double) latency;
+ } else {
+ if (latency < min) min = latency;
+ if (latency > max) max = latency;
+ tot += latency;
+ avg = (double) tot/count;
+ }
+ printf("\x1b[0G\x1b[2Kmin: %lld, max: %lld, avg: %.2f (%lld samples)
+ min, max, avg, count);
+ fflush(stdout);
+ if (config.latency_history && mstime()-history_start > history_inter
+ {
+ printf(" -- %.2f seconds range\n", (float)(mstime()-history_star
+ history_start = mstime();
+ min = max = tot = count = 0;
+ }
}
- printf("\x1b[0G\x1b[2Kmin: %lld, max: %lld, avg: %.2f (%lld samples)",
- min, max, avg, count);
- fflush(stdout);
- if (config.latency_history && mstime()-history_start > history_interval)
- {
- printf(" -- %.2f seconds range\n", (float)(mstime()-history_start)/1
- history_start = mstime();
- min = max = tot = count = 0;
+
+ /* Sleeping at every request creates scheduling artifacts that will
+ * report a latency higher than needed. So instead we do requests
+ * in brusts of 50, and wait a longer time later. We only sample the
+ * latency in the middle of brusts. */
+ req++;
+ if (req == LATENCY_BRUST_SIZE) {
+ req = 0;
+ usleep((LATENCY_SAMPLE_RATE * 1000)*LATENCY_BRUST_SIZE);
}
- usleep(LATENCY_SAMPLE_RATE * 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment