Skip to content

appending to file in SPIFFS gets slower as file grows #4291

Closed
@brianjmurrell

Description

@brianjmurrell

Basic Infos

Hardware

Hardware: NodeMCU 1.0 ESP-12E
Core Version: 2.4.0

Description

Appending to a file in SPIFFS gets slower as the file gets bigger.

Settings in IDE

Module: NodeMCU 1.0 ESP-12E
Flash Size: 4MB/3MB
CPU Frequency: 80Mhz
Flash Mode: ???
Flash Frequency: ???
Upload Using: SERIAL
Reset Method: ???

Sketch

#include <Arduino.h>
#include <FS.h>

void setup() {
  SPIFFS.begin()
}

void loop() {

  File f = SPIFFS.open("/stats.txt", "a");
    
  if (!f) {
    Serial.println("Failed to open /stats.txt");
    return;
  }
  
  f.println("A string to add to the file");

  f.close();

}

Debug Messages

NA.

What I find is that if I measure the time to run each iteration of the loop, it will start to take several thousand milliseconds to run just one iteration. At this point the file is just over 2MB.

I've tried running a sketch like this:

#include <FS.h>

void setup() {

  Serial.begin(115200);

  SPIFFS.begin();
  SPIFFS.remove("/stats.txt");

}

unsigned long last_millis = 0;

char msg[] = "1517604525,60,51,23,23,nan,14";
unsigned int msg_len = strlen(msg);

void loop() {

  unsigned long start = millis();
  File f = SPIFFS.open("/stats.txt", "a");
  if (!f) {
    Serial.println("Failed to open /stats.txt");
    return;
  }
  if (!f.println(msg)) {
      Serial.println("Failed to write to /stats.txt");
      return;
  }
  unsigned long fsize = f.size();
  f.close();
  unsigned long finish = millis();

  if (start - last_millis > 30000) {
    Serial.print("appending ");
    Serial.print(msg_len);
    Serial.print(" bytes to a file that is ");
    Serial.print(fsize);
    Serial.print(" bytes long took ");
    Serial.print(finish - start);
    Serial.println(" millis");
    last_millis = start;
  }
  
}

To get an idea of the scaling but it seemed to just stop appending after about 1MB or so. I will run it again and see how far it gets.

Metadata

Metadata

Assignees

No one assigned

    Labels

    waiting for feedbackWaiting on additional info. If it's not received, the issue may be closed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions