Skip to content

Commit

Permalink
Extend LATENCY LATEST to add sum / cnt stats (#1570)
Browse files Browse the repository at this point in the history
Currently LATENCY LATEST only has the following fields:
- Event name.
- Unix timestamp of the latest latency spike for the event.
- Latest event latency in millisecond.
- All-time maximum latency for this event.

This PR introduced these fields:
- sum: the all-time sum latency for this event.
- cnt: the event count that trigger the latency, with the sum we can
calc the avg.

Signed-off-by: Binbin <binloveplay1314@qq.com>
  • Loading branch information
enjoy-binbin authored Feb 4, 2025
1 parent aced268 commit 6eaf088
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/latency.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ void latencyAddSample(const char *event, mstime_t latency) {
ts = zmalloc(sizeof(*ts));
ts->idx = 0;
ts->max = 0;
ts->sum = 0;
ts->cnt = 0;
memset(ts->samples, 0, sizeof(ts->samples));
dictAdd(server.latency_events, zstrdup(event), ts);
}

if (latency > ts->max) ts->max = latency;
ts->sum += latency;
ts->cnt++;

/* 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. */
Expand Down Expand Up @@ -612,11 +616,13 @@ void latencyCommandReplyWithLatestEvents(client *c) {
struct latencyTimeSeries *ts = dictGetVal(de);
int last = (ts->idx + LATENCY_TS_LEN - 1) % LATENCY_TS_LEN;

addReplyArrayLen(c, 4);
addReplyArrayLen(c, 6);
addReplyBulkCString(c, event);
addReplyLongLong(c, ts->samples[last].time);
addReplyLongLong(c, ts->samples[last].latency);
addReplyLongLong(c, ts->max);
addReplyLongLong(c, ts->sum);
addReplyLongLong(c, ts->cnt);
}
dictReleaseIterator(di);
}
Expand Down
2 changes: 2 additions & 0 deletions src/latency.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ struct latencySample {
struct latencyTimeSeries {
int idx; /* Index of the next sample to store. */
uint32_t max; /* Max latency observed for this event. */
uint32_t sum; /* Sum latency observed for this event. */
uint32_t cnt; /* Count observed for this event. */
struct latencySample samples[LATENCY_TS_LEN]; /* Latest history. */
};

Expand Down
11 changes: 8 additions & 3 deletions tests/unit/latency-monitor.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,17 @@ tags {"needs:debug"} {
puts $res
}

# See the previous "Test latency events logging" test for each call.
foreach event $res {
lassign $event eventname time latency max
lassign $event eventname time latency max sum cnt
assert {$eventname eq "command"}
if {!$::no_latency} {
assert {$max >= 450 & $max <= 650}
assert {$time == $last_time}
# To avoid timing issues, each event decreases by 50 and
# increases by 150 to increase the range.
assert_equal $time $last_time
assert_range $max 450 650 ;# debug sleep 0.5
assert_range $sum 1050 1650 ;# debug sleep 0.3 + 0.4 + 0.5
assert_equal $cnt 3
}
break
}
Expand Down

0 comments on commit 6eaf088

Please sign in to comment.