Explorar o código

Measure slow HTTP info handler requests

SybexXx hai 1 día
pai
achega
64877493da
Modificáronse 1 ficheiros con 28 adicións e 0 borrados
  1. 28 0
      code/main/server_main.cpp

+ 28 - 0
code/main/server_main.cpp

@@ -17,6 +17,7 @@
 
 #include "MainFlowControl.h"
 #include "esp_log.h"
+#include "esp_timer.h"
 #include "basic_auth.h"
 #include "esp_chip_info.h"
 
@@ -30,6 +31,31 @@ std::string starttime = "";
 
 static const char *TAG = "MAIN SERVER";
 
+// Measure the complete handler lifetime, including response transmission.
+// Only report slow requests to keep diagnostic logging overhead low.
+class HttpHandlerTiming
+{
+public:
+    explicit HttpHandlerTiming(const char *uri)
+        : requestUri(uri), startUs(esp_timer_get_time())
+    {
+    }
+
+    ~HttpHandlerTiming()
+    {
+        const int64_t elapsedUs = esp_timer_get_time() - startUs;
+        if (elapsedUs >= 250000)
+        {
+            ESP_LOGW(TAG, "Slow HTTP handler: %s took %lld ms",
+                     requestUri, (long long)(elapsedUs / 1000));
+        }
+    }
+
+private:
+    const char *requestUri;
+    int64_t startUs;
+};
+
 // Writes JSON in small chunks so a complete response buffer is never allocated.
 class JsonChunkWriter
 {
@@ -240,6 +266,8 @@ private:
 /* An HTTP GET handler */
 esp_err_t info_get_handler(httpd_req_t *req)
 {
+    HttpHandlerTiming timing(req->uri);
+
 #ifdef DEBUG_DETAIL_ON      
     LogFile.WriteHeapInfo("info_get_handler - Start");    
 #endif