httpd performance

maxime8848
Posts: 4
Joined: Mon Dec 23, 2024 10:48 am

httpd performance

Postby maxime8848 » 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

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;
}
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?

nopnop2002
Posts: 139
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: httpd performance

Postby nopnop2002 » Thu Dec 26, 2024 7:28 am

I have never used Arduino IDE, but it's possible that the values ​​set in sdkconfig are different between Arduino IDE and ESP-IDF.

I think you'll get the same results if you use the same sdkconfig in both Arduino IDE and ESP-IDF.

The performance of ESP-IDF may vary significantly depending on the values ​​set in sdkconfig.

MicroController
Posts: 1955
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: httpd performance

Postby MicroController » Thu Dec 26, 2024 9:55 am

Haven't used Arduino, but I too found that some responses from the HTTPD take noticeably longer than expected. Specifically, I noticed that bigger responses (a few KB) come much faster than small ones, which leads me to believe that the culprit is lwIP which may be buffering/delaying small pieces of data, presumably to try and fill up a whole TCP/IP packet before sending it out.
Try a bigger data transfer and check if you too see faster response times with that.
Not using persistent connections, i.e. closing every connection right when all data is passed to the network stack, would resolve the issue with the small responses, but at a cost.
You can also try to set TCP_NODELAY on the HTTP connection's socket and see if that changes anything.

Btw:

Code: Select all

void loop() {
  arduinoWebServer.handleClient();
}
Not a fair comparison with the IDF httpd as you're giving basically 100% of one core's CPU time to the arduinoWebServer even when it has nothing to do.

Who is online

Users browsing this forum: No registered users and 89 guests