From 855c97ce1b2bb52497661520e701ea9a061d1b5a Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 1 Jul 2014 17:12:09 +0200 Subject: [PATCH] Latency monitor: don't add new samples in the same second. Instead we update the old sample with the new latency if it is greater. --- src/latency.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/latency.c b/src/latency.c index 198036df..649381ef 100644 --- a/src/latency.c +++ b/src/latency.c @@ -69,6 +69,8 @@ void latencyMonitorInit(void) { * server.latency_monitor_threshold. */ void latencyAddSample(char *event, mstime_t latency) { struct latencyTimeSeries *ts = dictFetchValue(server.latency_events,event); + time_t now = time(NULL); + int prev; /* Create the time series if it does not exist. */ if (ts == NULL) { @@ -79,6 +81,15 @@ void latencyAddSample(char *event, mstime_t latency) { dictAdd(server.latency_events,zstrdup(event),ts); } + /* If the previous sample is in the same second, we update our old sample + * if this latency is > of the old one, or just return. */ + prev = (ts->idx + LATENCY_TS_LEN - 1) % LATENCY_TS_LEN; + if (ts->samples[prev].time == now) { + if (latency > ts->samples[prev].latency) + ts->samples[prev].latency = latency; + return; + } + ts->samples[ts->idx].time = time(NULL); ts->samples[ts->idx].latency = latency; if (latency > ts->max) ts->max = latency; -- GitLab