feat: log how long each API request takes

Every request through the locked API handlers logs lock wait, handler
time, and the split into build, JSON print and send.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Sonnet 5.5 committed 2026-11-10 01:46:05 +13:00
1 parent f85021d40d
commit d4118072f6
1 file changed
+24
+24
View File
@@ -7,6 +7,7 @@
#include "api_util.h"
#include "cJSON.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "bench.h"
#include "engine.h"
#include "espnow_tx.h"
@@ -31,9 +32,15 @@ static const struct {
/* ---------------------------------------------------------------- helpers */
// Time spent printing JSON and sending the reply for the current request (the httpd task handles
// one request at a time), reported by the LOCKED wrapper's log line
static int64_t s_print_us, s_send_us;
esp_err_t api_json(httpd_req_t *req, const char *status, cJSON *obj, bool own)
{
int64_t t0 = esp_timer_get_time();
char *s = cJSON_PrintUnformatted(obj);
s_print_us += esp_timer_get_time() - t0;
if (own) {
cJSON_Delete(obj);
}
@@ -42,7 +49,9 @@ esp_err_t api_json(httpd_req_t *req, const char *status, cJSON *obj, bool own)
}
httpd_resp_set_status(req, status);
httpd_resp_set_type(req, "application/json");
int64_t t1 = esp_timer_get_time();
esp_err_t ret = httpd_resp_send(req, s, HTTPD_RESP_USE_STRLEN);
s_send_us += esp_timer_get_time() - t1;
free(s);
return ret;
}
@@ -464,12 +473,27 @@ static esp_err_t db_handler_inner(httpd_req_t *req)
return api_error(req, "405 Method Not Allowed", "method not allowed");
}
static void log_timing(httpd_req_t *req, int64_t lock_us, int64_t handle_us)
{
ESP_LOGI("http", "%s %s: lock %.1f ms, handle %.1f ms = build %.1f + print %.1f + send %.1f",
req->method == HTTP_GET ? "GET" : req->method == HTTP_PUT ? "PUT" : req->method == HTTP_POST ? "POST" : "DEL",
req->uri, lock_us / 1000.0, handle_us / 1000.0, (handle_us - s_print_us - s_send_us) / 1000.0,
s_print_us / 1000.0, s_send_us / 1000.0);
}
// Logs how long each request took: waiting for the store lock, then the handler, split into
// building the data, printing JSON and sending (send includes the radio/TCP hand-off)
#define LOCKED(name) \
static esp_err_t name(httpd_req_t *req) \
{ \
int64_t t0 = esp_timer_get_time(); \
store_lock(); \
int64_t t1 = esp_timer_get_time(); \
s_print_us = s_send_us = 0; \
esp_err_t r = name##_inner(req); \
int64_t t2 = esp_timer_get_time(); \
store_unlock(); \
log_timing(req, t1 - t0, t2 - t1); \
return r; \
}
LOCKED(collection_handler)