Sfoglia il codice sorgente

Instrument duration of all HTTP handler requests

SybexXx 1 giorno fa
parent
commit
9c9f338fce
1 ha cambiato i file con 38 aggiunte e 0 eliminazioni
  1. 38 0
      code/components/jomjol_wlan/basic_auth.cpp

+ 38 - 0
code/components/jomjol_wlan/basic_auth.cpp

@@ -2,6 +2,8 @@
 #include "read_wlanini.h"
 #include <esp_tls_crypto.h>
 #include <esp_log.h>
+#include "esp_timer.h"
+#include "ClassLogFile.h"
 
 
 #define HTTPD_401 "401 UNAUTHORIZED"
@@ -15,6 +17,41 @@ typedef struct {
 
 basic_auth_info_t basic_auth_info = { NULL, NULL };
 
+// All URI handlers are registered through APPLY_BASIC_AUTH_FILTER. Measuring here
+// gives us a common duration for every HTTP endpoint, including static HTML/CSS/JS
+// and image handlers. Time spent waiting in the ESP-IDF server queue is not included.
+class HttpRequestTiming
+{
+public:
+    explicit HttpRequestTiming(httpd_req_t *request)
+        : uri(request->uri ? request->uri : "<unknown>"),
+          method(request->method == HTTP_GET ? "GET" :
+                 request->method == HTTP_POST ? "POST" :
+                 request->method == HTTP_PUT ? "PUT" :
+                 request->method == HTTP_DELETE ? "DELETE" : "OTHER"),
+          startUs(esp_timer_get_time())
+    {
+    }
+
+    ~HttpRequestTiming()
+    {
+        const int64_t elapsedUs = esp_timer_get_time() - startUs;
+        // Catch noticeable handler stalls without logging every normal poll.
+        if (elapsedUs >= 100000)
+        {
+            LogFile.WriteToFile(ESP_LOG_WARN, TAG,
+                                std::string("Slow HTTP request: ") + method + " " + uri +
+                                " handler time " +
+                                std::to_string(static_cast<long long>(elapsedUs / 1000)) + " ms");
+        }
+    }
+
+private:
+    std::string uri;
+    const char *method;
+    int64_t startUs;
+};
+
 void init_basic_auth() {
     if (!wlan_config.http_username.empty() && !wlan_config.http_password.empty()) {
         basic_auth_info.username = wlan_config.http_username.c_str();
@@ -50,6 +87,7 @@ static char *http_auth_basic(const char *username, const char *password)
 
 esp_err_t basic_auth_request_filter(httpd_req_t *req, esp_err_t original_handler(httpd_req_t *))
 {
+    HttpRequestTiming requestTiming(req);
     char *buf = NULL;
     size_t buf_len = 0;
     esp_err_t ret = ESP_OK;