Skip to content

Instantly share code, notes, and snippets.

@bitbank2
Created July 12, 2024 11:26
Show Gist options
  • Save bitbank2/6484dbd5becb0e416d76a9683f76e1f9 to your computer and use it in GitHub Desktop.
Save bitbank2/6484dbd5becb0e416d76a9683f76e1f9 to your computer and use it in GitHub Desktop.
Arduino sketch to test power savings of gzip on esp32 HTTP GET requests
//
// gzip power test
// written by Larry Bank ([email protected])
// July 9, 2024
//
// This sketch uses my zlib_turbo to compare how much power
// is used by a gzip payload vs uncompressed on the Unexpected Maker FeatherS2
//
#include <WiFi.h>
#include <HTTPClient.h>
#include <zlib_turbo.h>
zlib_turbo zt;
HTTPClient http;
const char *url = "https://www.tomshardware.com/feeds/all";
const char *SSID = "your_ssid";
const char *PW = "your_password";
// 10 seconds WIFI connection timeout
#define TIMEOUT 20
void GetWebData(bool bCompress)
{
long l;
int iCount, iPayloadSize;
uint8_t *pData;
int httpCode;
WiFiClient * stream;
http.begin(url);
if (bCompress) {
http.setAcceptEncoding("gzip"); // ask for response to be compressed
}
l = millis(); // measure GET request time
httpCode = http.GET(); //send GET request
if (httpCode != 200) {
Serial.print("Error on HTTP request: ");
Serial.println(httpCode);
http.end();
} else {
iPayloadSize = http.getSize();
// Allocate a buffer to receive the compressed (gzip) response
pData = (uint8_t *)ps_malloc(iPayloadSize);
l = millis();
stream = http.getStreamPtr();
iCount = 0;
// Allow 4 seconds to receive the compressed data
while (iCount < iPayloadSize && (millis() - l) < 4000) {
if (stream->available()) {
char c = stream->read();
pData[iCount++] = c;
} else {
vTaskDelay(1); // allow time for data to receive
}
} // while
http.end(); // we're done, close the connection
l = millis() - l;
Serial.printf("Received %d byte payload in %dms\n", iPayloadSize, (int)l);
if (pData[0] == 0x1f && pData[1] == 0x8b) { // HTTPClient doesn't seem to give us access to the response headers, so we need to do this
Serial.println("The server sent gzip data");
} else {
if (bCompress) {
Serial.println("We didn't get gzip data");
bCompress = false;
}
}
if (bCompress) { // measure the time needed to decompress the data
uint8_t *pUncompressed;
int rc, iUncompSize = zt.gzip_info(pData, iPayloadSize);
if (iUncompSize > 0) {
Serial.printf("Uncompressed size = %d bytes\n", iUncompSize);
pUncompressed = (uint8_t *)ps_malloc(iUncompSize+8);
l = millis();
rc = zt.gunzip(pData, iPayloadSize, pUncompressed);
if (rc == ZT_SUCCESS) {
l = millis() - l;
Serial.printf("Decompressed %d bytes in %dms\n", iUncompSize, (int)l);
free(pUncompressed);
} else {
Serial.println("An error occurred when decompressing the gzip file.");
}
} // compressed size > 0
} // compressed data
free(pData);
} // http connection succeeded
} /* GetWebData() */
void setup()
{
int iTimeout;
Serial.begin(115200);
delay(3000); // wait for CDC-Serial to start
Serial.println("HTTP GZIP Power Test");
// Connect to wifi
WiFi.begin(SSID, PW);
iTimeout = 0;
Serial.print("Connecting to wifi");
while (WiFi.status() != WL_CONNECTED && WiFi.status() != WL_CONNECT_FAILED && iTimeout < TIMEOUT)
{
delay(500); // allow up to 10 seconds to connect
iTimeout++;
Serial.print(".");
}
if (iTimeout == TIMEOUT) {
Serial.println("\nConnection timed out!");
} else {
Serial.println("\nConnected!");
Serial.println("Sending GET request (uncompressed)");
GetWebData(false);
delay(1000); // pause for 1 second to see it on the power monitor
Serial.println("Sending GET request (compressed)");
GetWebData(true);
WiFi.disconnect();
} // wifi connection
} /* setup() */
void loop() { } /* loop() */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment