Analyze the samples available for a given event and return a structure * populate with different metrics, average, MAD, min, max, and so forth. * Check latency.h definition of struct latencyStats for more info. * If the specified event has no elements the structure is populate with * zero values. */
| 175 | * If the specified event has no elements the structure is populate with |
| 176 | * zero values. */ |
| 177 | void analyzeLatencyForEvent(char *event, struct latencyStats *ls) { |
| 178 | struct latencyTimeSeries *ts = (latencyTimeSeries*)dictFetchValue(g_pserver->latency_events,event); |
| 179 | int j; |
| 180 | uint64_t sum; |
| 181 | |
| 182 | ls->all_time_high = ts ? ts->max : 0; |
| 183 | ls->avg = 0; |
| 184 | ls->min = 0; |
| 185 | ls->max = 0; |
| 186 | ls->mad = 0; |
| 187 | ls->samples = 0; |
| 188 | ls->period = 0; |
| 189 | if (!ts) return; |
| 190 | |
| 191 | /* First pass, populate everything but the MAD. */ |
| 192 | sum = 0; |
| 193 | for (j = 0; j < LATENCY_TS_LEN; j++) { |
| 194 | if (ts->samples[j].time == 0) continue; |
| 195 | ls->samples++; |
| 196 | if (ls->samples == 1) { |
| 197 | ls->min = ls->max = ts->samples[j].latency; |
| 198 | } else { |
| 199 | if (ls->min > ts->samples[j].latency) |
| 200 | ls->min = ts->samples[j].latency; |
| 201 | if (ls->max < ts->samples[j].latency) |
| 202 | ls->max = ts->samples[j].latency; |
| 203 | } |
| 204 | sum += ts->samples[j].latency; |
| 205 | |
| 206 | /* Track the oldest event time in ls->period. */ |
| 207 | if (ls->period == 0 || ts->samples[j].time < ls->period) |
| 208 | ls->period = ts->samples[j].time; |
| 209 | } |
| 210 | |
| 211 | /* So far avg is actually the sum of the latencies, and period is |
| 212 | * the oldest event time. We need to make the first an average and |
| 213 | * the second a range of seconds. */ |
| 214 | if (ls->samples) { |
| 215 | ls->avg = sum / ls->samples; |
| 216 | ls->period = time(NULL) - ls->period; |
| 217 | if (ls->period == 0) ls->period = 1; |
| 218 | } |
| 219 | |
| 220 | /* Second pass, compute MAD. */ |
| 221 | sum = 0; |
| 222 | for (j = 0; j < LATENCY_TS_LEN; j++) { |
| 223 | int64_t delta; |
| 224 | |
| 225 | if (ts->samples[j].time == 0) continue; |
| 226 | delta = (int64_t)ls->avg - ts->samples[j].latency; |
| 227 | if (delta < 0) delta = -delta; |
| 228 | sum += delta; |
| 229 | } |
| 230 | if (ls->samples) ls->mad = sum / ls->samples; |
| 231 | } |
| 232 | |
| 233 | /* Create a human readable report of latency events for this KeyDB instance. */ |
| 234 | sds createLatencyReport(void) { |
no test coverage detected