From 2a6489d926a625fd441c089992f2aed61dcd82df Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20Vavru=C5=A1a?= Date: Thu, 28 May 2015 17:07:51 +0200 Subject: [PATCH] modules/stats: count slow answers (>1000ms) --- modules/stats/stats.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/modules/stats/stats.c b/modules/stats/stats.c index 357b4d5e2..dfc7cf576 100644 --- a/modules/stats/stats.c +++ b/modules/stats/stats.c @@ -38,6 +38,14 @@ /* Defaults */ #define DEBUG_MSG(qry, fmt...) QRDEBUG(qry, "stat", fmt) +/** @internal Subtract time (best effort) */ +float time_diff(struct timeval *begin, struct timeval *end) +{ + return (end->tv_sec - begin->tv_sec) * 1000 + + (end->tv_usec - begin->tv_usec) / 1000.0; + +} + /** @internal Add to map counter */ static inline void stat_add(map_t *map, const char *key, ssize_t incr) { @@ -79,10 +87,18 @@ static int collect(knot_layer_t *ctx) collect_answer(map, param->answer); /* Count cached and unresolved */ if (!EMPTY_LIST(rplan->resolved)) { - struct kr_query *qry = TAIL(rplan->resolved); - if (qry->flags & QUERY_CACHED) { + struct kr_query *last = TAIL(rplan->resolved); + if (last->flags & QUERY_CACHED) { stat_add(map, "answer.cached", 1); } + /* Count slow queries (>1000ms) */ + struct kr_query *first = HEAD(rplan->resolved); + struct timeval now; + gettimeofday(&now, NULL); + float elapsed = time_diff(&first->timestamp, &now); + if (elapsed > 1000.0) { + stat_add(map, "answer.slow", 1); + } } else { stat_add(map, "answer.unresolved", 1); } -- 2.47.3