Просмотр исходного кода

Measure slow file and log HTTP handlers

SybexXx 1 день назад
Родитель
Сommit
b244f676f4
1 измененных файлов с 31 добавлено и 0 удалено
  1. 31 0
      code/components/jomjol_fileserver_ota/server_file.cpp

+ 31 - 0
code/components/jomjol_fileserver_ota/server_file.cpp

@@ -17,6 +17,7 @@
 
 #include "esp_err.h"
 #include "esp_log.h"
+#include "esp_timer.h"
 
 #include <iostream>
 #include <sys/types.h>
@@ -43,6 +44,33 @@
 
 static const char *TAG = "OTA FILE";
 
+// Measure file/log HTTP handlers that share the single esp_http_server task.
+// Log only slow handlers to keep the instrumentation lightweight.
+class FileHandlerTiming
+{
+public:
+    FileHandlerTiming(const char *handlerName, httpd_req_t *request)
+        : name(handlerName), uri(request->uri), startUs(esp_timer_get_time())
+    {
+    }
+
+    ~FileHandlerTiming()
+    {
+        const int64_t elapsedUs = esp_timer_get_time() - startUs;
+        if (elapsedUs >= 250000)
+        {
+            LogFile.WriteToFile(ESP_LOG_WARN, TAG,
+                                std::string("Slow HTTP file handler: ") + name + " " + uri +
+                                " took " + std::to_string((long long)(elapsedUs / 1000)) + " ms");
+        }
+    }
+
+private:
+    const char *name;
+    const char *uri;
+    int64_t startUs;
+};
+
 struct file_server_data
 {
     /* Base path of file storage */
@@ -436,6 +464,7 @@ static esp_err_t datafileact_get_last_part_handler(httpd_req_t *req)
 
 static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)
 {
+    FileHandlerTiming timing("send_datafile", req);
     LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "data_get_last_part_handler");
     FILE *fd = NULL;
     // struct stat file_stat;
@@ -522,6 +551,7 @@ static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)
 
 static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)
 {
+    FileHandlerTiming timing("send_logfile", req);
     LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "log_get_last_part_handler");
     FILE *fd = NULL;
     // struct stat file_stat;
@@ -614,6 +644,7 @@ static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)
 /* Handler to download a file kept on the server */
 static esp_err_t download_get_handler(httpd_req_t *req)
 {
+    FileHandlerTiming timing("download_get_handler", req);
     LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "download_get_handler");
     char filepath[FILE_PATH_MAX];
     FILE *fd = NULL;