httpd performance
Posted: Mon Dec 23, 2024 11:01 am
I'm trying to make a web server. After experimenting with Arduino core and the WebServer library I realised that ESP-IDF's httpd would be better because it has useful features like websockets and connection keep-alive. However I noticed that httpd takes longer to respond to requests, which surprised me as it is the standard server in the ESP-IDF.
So I compared the two using this Arduino program
And I found that in AP mode, Arduino's WebServer takes in average 19 ms to respond, whereas ESP-IDF httpd takes 61 ms. In station mode, it's 197 vs 493 ms. I tried multiple ESP32, on both Arduino IDE and ESP-IDF, and always get similar results. I want to switch to ESP-IDF but I need to know where this slowness comes from and how to improve the performance, since Arduino is able to do it faster. Even for an ESP32 I think that 500 ms is too slow for a simple HTTP request. Do you have any ideas?
So I compared the two using this Arduino program
Code: Select all
#include <WiFi.h>
#include <WebServer.h>
extern "C" {
#include "esp_http_server.h"
}
WebServer arduinoWebServer(81); // Arduino web server
httpd_handle_t espWebServer; // ESP-IDF web server
void setup() {
Serial.begin(115200);
WiFi.begin("ssid", "password");
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.print("Connected, IP address: "); Serial.println(WiFi.localIP());
// WiFi.softAPConfig(IPAddress(192, 168, 4, 7), IPAddress(192, 168, 4, 254), IPAddress(255, 255, 255, 0));
// WiFi.softAP("esptest", "12345678");
// Arduino web server
arduinoWebServer.on("/hello", HTTP_GET, arduinoHandler);
arduinoWebServer.begin();
// ESP-IDF web server
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.server_port = 82;
httpd_start(&espWebServer, &config);
httpd_uri_t helloUri = {
.uri = "/hello",
.method = HTTP_GET,
.handler = espHandler,
.user_ctx = NULL
};
httpd_register_uri_handler(espWebServer, &helloUri);
}
void loop() {
arduinoWebServer.handleClient();
}
void arduinoHandler() {
arduinoWebServer.send(200, "text/html", "<h1>Hello</h1>");
}
esp_err_t espHandler(httpd_req_t *req) {
httpd_resp_set_type(req, "text/html");
httpd_resp_send(req, "<h1>Hello</h1>", HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}