Skip to content

Instantly share code, notes, and snippets.

@tanakamasayuki
Created February 7, 2023 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanakamasayuki/6fc8ac56cb30f361d3863887b4df9804 to your computer and use it in GitHub Desktop.
Save tanakamasayuki/6fc8ac56cb30f361d3863887b4df9804 to your computer and use it in GitHub Desktop.
// base https://github.com/lovyan03/M5Stack_LovyanLauncher/blob/master/LovyanLauncher/src/CBFSBench.h
#include <SPIFFS.h>
#include <FFat.h>
#include <LittleFS.h>
#define BENCH_FS 2 // 0=SPIFFS, 1=LittleFS, 2=FFat
String tmpFile{"/lovyanLauncherBench"};
#if BENCH_FS == 0
fs::SPIFFSFS benchfs = SPIFFS;
#elif BENCH_FS == 1
fs::LittleFSFS benchfs = LittleFS;
#elif BENCH_FS == 2
fs::F_Fat benchfs = FFat;
#endif
fs::FS& getFS() {
return benchfs;
};
String getStrSize(uint64_t value) {
int base = 0;
for (; base < 3 && (value >= (100 * 1024)); ++base) {
value = value >> 10;
}
String space = "";
int dig = (value == 0) ? 0 : log10(value);
for (int i = 6 - dig; i > 0; --i) space += " ";
return space + String((long)value, DEC)
+ ((base == 3) ? " GiB"
: (base == 2) ? " MiB"
: (base == 1) ? " KiB"
: (base == 0) ? " Byte"
: " ??");
}
void ExecBench() {
for (int i = 0; i < 5; ++i) {
drawResult(77 + i * 17, tmpFile, 4 << i, 8 - i);
}
int loop = 20;
Serial.printf("\n");
Serial.printf("0ByteCreate : %8d count/s\n", int((uint64_t)1000000 / getCreateTime(getFS(), tmpFile, loop)));
Serial.printf("OpenClose : %8d count/s\n", int((uint64_t)1000000 / getOpenCloseTime(getFS(), tmpFile, loop)));
Serial.printf("Remove : %8d count/s\n", int((uint64_t)1000000 / getRemoveTime(getFS(), tmpFile, loop)));
}
void drawResult(int y, const String& filepath, int kib, int loop) {
Serial.printf("\n");
Serial.printf("FileSize : %8d Byte\n", 1024 * kib);
Serial.printf("WriteSpeed : %8d KiB/s\n", int(1000000 * kib / getWriteTime(getFS(), filepath, 1024 * kib, loop)));
Serial.printf("ReadSpeed : %8d KiB/s\n", int(1000000 * kib / getReadTime(getFS(), filepath, 1024 * kib, loop)));
}
uint64_t getWriteTime(fs::FS& fs, const String& filepath, long filesize, int loop) {
File f;
uint64_t res = 0;
uint8_t buf[SPI_FLASH_SEC_SIZE];
for (int i = 0; i < SPI_FLASH_SEC_SIZE; ++i) { buf[i] = uint8_t(i & 0xFF); }
for (int i = 0; i < loop; ++i) {
long rest = filesize;
uint64_t start = micros();
f = fs.open(filepath + String(i), FILE_WRITE);
do {
long size = rest > SPI_FLASH_SEC_SIZE ? SPI_FLASH_SEC_SIZE : rest;
f.write(&buf[0], size);
rest -= size;
} while (rest > 0);
f.flush();
f.close();
res += micros() - start;
}
return res / loop;
}
uint64_t getReadTime(fs::FS& fs, const String& filepath, long filesize, int loop) {
File f;
uint64_t res = 0;
uint8_t buf[SPI_FLASH_SEC_SIZE];
for (int i = 0; i < loop; ++i) {
long rest = filesize;
uint64_t start = micros();
f = fs.open(filepath + String(i), FILE_READ);
do {
long size = rest > SPI_FLASH_SEC_SIZE ? SPI_FLASH_SEC_SIZE : rest;
f.read(&buf[0], size);
rest -= size;
} while (rest > 0);
f.close();
res += micros() - start;
fs.remove(filepath + String(i));
}
return res / loop;
}
uint64_t getCreateTime(fs::FS& fs, const String& filepath, int loop) {
uint64_t start = micros();
for (int i = 0; i < loop; ++i) {
fs.open(filepath + String(i), FILE_WRITE).close();
}
return (micros() - start) / loop;
}
uint64_t getOpenCloseTime(fs::FS& fs, const String& filepath, int loop) {
uint64_t start = micros();
for (int i = 0; i < loop; ++i) {
fs.open(filepath + String(i), FILE_READ).close();
}
return (micros() - start) / loop;
}
uint64_t getRemoveTime(fs::FS& fs, const String& filepath, int loop) {
uint64_t start = micros();
for (int i = 0; i < loop; ++i) {
fs.remove(filepath + String(i));
}
return (micros() - start) / loop;
}
void setup() {
Serial.begin(115200);
delay(500);
if (!benchfs.begin()) {
// FS is unformatted
Serial.print("FS Format ... (please wait)");
delay(100);
benchfs.format();
Serial.println("Down");
ESP.restart();
}
Serial.println("fs bench begin");
Serial.printf("fs total : %8d Byte\n", benchfs.totalBytes());
Serial.printf("fs used : %8d Byte\n", benchfs.usedBytes());
Serial.printf("fs free : %8d Byte\n", benchfs.totalBytes() - benchfs.usedBytes());
ExecBench();
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment