|
@@ -9,6 +9,7 @@
|
|
|
#include "esp_err.h"
|
|
#include "esp_err.h"
|
|
|
#include "esp_log.h"
|
|
#include "esp_log.h"
|
|
|
#include "esp_http_server.h"
|
|
#include "esp_http_server.h"
|
|
|
|
|
+#include "esp_timer.h"
|
|
|
|
|
|
|
|
#include "miniz.h"
|
|
#include "miniz.h"
|
|
|
|
|
|
|
@@ -33,6 +34,32 @@ bool endsWith(std::string const &str, std::string const &suffix)
|
|
|
|
|
|
|
|
esp_err_t send_file(httpd_req_t *req, std::string filename)
|
|
esp_err_t send_file(httpd_req_t *req, std::string filename)
|
|
|
{
|
|
{
|
|
|
|
|
+ const int64_t totalStartUs = esp_timer_get_time();
|
|
|
|
|
+ int64_t openUs = 0;
|
|
|
|
|
+ int64_t readUs = 0;
|
|
|
|
|
+ int64_t sendUs = 0;
|
|
|
|
|
+ size_t totalBytes = 0;
|
|
|
|
|
+ size_t chunkCount = 0;
|
|
|
|
|
+
|
|
|
|
|
+ auto logSlowTransfer = [&](const char *result) {
|
|
|
|
|
+ const int64_t totalUs = esp_timer_get_time() - totalStartUs;
|
|
|
|
|
+ // Static assets (HTML, JS, CSS and images) are requested during page load.
|
|
|
|
|
+ // A 50 ms threshold exposes repeated moderate delays that add up to a
|
|
|
|
|
+ // visibly slow page build, while keeping normal requests out of the log.
|
|
|
|
|
+ if (totalUs >= 50000) {
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_WARN, TAG,
|
|
|
|
|
+ std::string("Slow static file response: uri=") + req->uri +
|
|
|
|
|
+ ", file=" + filename +
|
|
|
|
|
+ ", result=" + result +
|
|
|
|
|
+ ", total=" + std::to_string(static_cast<long long>(totalUs / 1000)) + " ms" +
|
|
|
|
|
+ ", open=" + std::to_string(static_cast<long long>(openUs / 1000)) + " ms" +
|
|
|
|
|
+ ", read=" + std::to_string(static_cast<long long>(readUs / 1000)) + " ms" +
|
|
|
|
|
+ ", send=" + std::to_string(static_cast<long long>(sendUs / 1000)) + " ms" +
|
|
|
|
|
+ ", bytes=" + std::to_string(totalBytes) +
|
|
|
|
|
+ ", chunks=" + std::to_string(chunkCount));
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
std::string _filename_old = filename;
|
|
std::string _filename_old = filename;
|
|
|
struct stat file_stat;
|
|
struct stat file_stat;
|
|
|
bool _gz_file_exists = false;
|
|
bool _gz_file_exists = false;
|
|
@@ -41,6 +68,7 @@ esp_err_t send_file(httpd_req_t *req, std::string filename)
|
|
|
std::string _filename_temp = std::string(filename) + ".gz";
|
|
std::string _filename_temp = std::string(filename) + ".gz";
|
|
|
|
|
|
|
|
// Checks whether the file is available as .gz
|
|
// Checks whether the file is available as .gz
|
|
|
|
|
+ const int64_t openStartUs = esp_timer_get_time();
|
|
|
if (stat(_filename_temp.c_str(), &file_stat) == 0)
|
|
if (stat(_filename_temp.c_str(), &file_stat) == 0)
|
|
|
{
|
|
{
|
|
|
filename = _filename_temp;
|
|
filename = _filename_temp;
|
|
@@ -50,12 +78,14 @@ esp_err_t send_file(httpd_req_t *req, std::string filename)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
FILE *fd = fopen(filename.c_str(), "r");
|
|
FILE *fd = fopen(filename.c_str(), "r");
|
|
|
|
|
+ openUs += esp_timer_get_time() - openStartUs;
|
|
|
if (!fd)
|
|
if (!fd)
|
|
|
{
|
|
{
|
|
|
ESP_LOGE(TAG, "Failed to read file: %s", filename.c_str());
|
|
ESP_LOGE(TAG, "Failed to read file: %s", filename.c_str());
|
|
|
|
|
|
|
|
/* Respond with 404 Error */
|
|
/* Respond with 404 Error */
|
|
|
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
|
|
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
|
|
|
|
|
+ logSlowTransfer("open failed");
|
|
|
return ESP_FAIL;
|
|
return ESP_FAIL;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -104,11 +134,18 @@ esp_err_t send_file(httpd_req_t *req, std::string filename)
|
|
|
|
|
|
|
|
do
|
|
do
|
|
|
{
|
|
{
|
|
|
- /* Read file in chunks into the scratch buffer */
|
|
|
|
|
|
|
+ /* Measure SD/VFS read time separately from socket send time. */
|
|
|
|
|
+ int64_t phaseStartUs = esp_timer_get_time();
|
|
|
chunksize = fread(chunk, 1, SERVER_HELPER_SCRATCH_BUFSIZE, fd);
|
|
chunksize = fread(chunk, 1, SERVER_HELPER_SCRATCH_BUFSIZE, fd);
|
|
|
|
|
+ readUs += esp_timer_get_time() - phaseStartUs;
|
|
|
|
|
+
|
|
|
|
|
+ totalBytes += chunksize;
|
|
|
|
|
+ ++chunkCount;
|
|
|
|
|
|
|
|
- /* Send the buffer contents as HTTP response chunk */
|
|
|
|
|
|
|
+ /* Measure time spent sending this chunk to the HTTP client. */
|
|
|
|
|
+ phaseStartUs = esp_timer_get_time();
|
|
|
const esp_err_t sendResult = httpd_resp_send_chunk(req, chunk, chunksize);
|
|
const esp_err_t sendResult = httpd_resp_send_chunk(req, chunk, chunksize);
|
|
|
|
|
+ sendUs += esp_timer_get_time() - phaseStartUs;
|
|
|
if (sendResult != ESP_OK)
|
|
if (sendResult != ESP_OK)
|
|
|
{
|
|
{
|
|
|
fclose(fd);
|
|
fclose(fd);
|
|
@@ -120,6 +157,7 @@ esp_err_t send_file(httpd_req_t *req, std::string filename)
|
|
|
// client.
|
|
// client.
|
|
|
ESP_LOGW(TAG, "File transfer interrupted for %s (send error %d)",
|
|
ESP_LOGW(TAG, "File transfer interrupted for %s (send error %d)",
|
|
|
filename.c_str(), static_cast<int>(sendResult));
|
|
filename.c_str(), static_cast<int>(sendResult));
|
|
|
|
|
+ logSlowTransfer("send failed");
|
|
|
return ESP_FAIL;
|
|
return ESP_FAIL;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -129,6 +167,7 @@ esp_err_t send_file(httpd_req_t *req, std::string filename)
|
|
|
/* Close file after sending complete */
|
|
/* Close file after sending complete */
|
|
|
fclose(fd);
|
|
fclose(fd);
|
|
|
ESP_LOGD(TAG, "File sending complete");
|
|
ESP_LOGD(TAG, "File sending complete");
|
|
|
|
|
+ logSlowTransfer("ok");
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
return ESP_OK;
|
|
|
}
|
|
}
|