Преглед изворни кода

Measure slow HTTP info handler requests

SybexXx пре 1 дан
родитељ
комит
64877493da
1 измењених фајлова са 28 додато и 0 уклоњено
  1. 28 0
      code/main/server_main.cpp

+ 28 - 0
code/main/server_main.cpp

@@ -17,6 +17,7 @@
 
 
 #include "MainFlowControl.h"
 #include "MainFlowControl.h"
 #include "esp_log.h"
 #include "esp_log.h"
+#include "esp_timer.h"
 #include "basic_auth.h"
 #include "basic_auth.h"
 #include "esp_chip_info.h"
 #include "esp_chip_info.h"
 
 
@@ -30,6 +31,31 @@ std::string starttime = "";
 
 
 static const char *TAG = "MAIN SERVER";
 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.
 // Writes JSON in small chunks so a complete response buffer is never allocated.
 class JsonChunkWriter
 class JsonChunkWriter
 {
 {
@@ -240,6 +266,8 @@ private:
 /* An HTTP GET handler */
 /* An HTTP GET handler */
 esp_err_t info_get_handler(httpd_req_t *req)
 esp_err_t info_get_handler(httpd_req_t *req)
 {
 {
+    HttpHandlerTiming timing(req->uri);
+
 #ifdef DEBUG_DETAIL_ON      
 #ifdef DEBUG_DETAIL_ON      
     LogFile.WriteHeapInfo("info_get_handler - Start");    
     LogFile.WriteHeapInfo("info_get_handler - Start");    
 #endif
 #endif