Skip to content

Commit

Permalink
Clean up SpeedTest output, avoid div-by-0 (#8340)
Browse files Browse the repository at this point in the history
Use a formatting function to give more human-readable output formats
for flash bandwidth.  When the test starts and ends in less than one
millisecond, report as "Infinite".

Sample output:
````
Creating 512KB file, may take a while...
==> Time to write 512KB in 256b chunks = 6641 milliseconds
==> Created file size = 524288
Reading 512KB file sequentially in 256b chunks
==> Time to read 512KB sequentially in 256b chunks = 211 milliseconds = 2.48 MB/s
Reading 512KB file MISALIGNED in flash and RAM sequentially in 256b chunks
==> Time to read 512KB sequentially MISALIGNED in flash and RAM in 256b chunks = 212 milliseconds = 2.47 MB/s
Reading 512KB file in reverse by 256b chunks
==> Time to read 512KB in reverse in 256b chunks = 367 milliseconds = 1.43 MB/s
Writing 64K file in 1-byte chunks
==> Time to write 64KB in 1b chunks = 1249 milliseconds = 52.47 KB/s
Reading 64K file in 1-byte chunks
==> Time to read 64KB in 1b chunks = 296 milliseconds = 221.41 KB/s
````
  • Loading branch information
earlephilhower authored Oct 16, 2021
1 parent c312a2e commit aabfd73
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions libraries/LittleFS/examples/SpeedTest/SpeedTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@
// How large of a file to test
#define TESTSIZEKB 512

// Format speed in bytes/second. Static buffer so not re-entrant safe
const char *rate(unsigned long start, unsigned long stop, unsigned long bytes) {
static char buff[64];
if (stop == start) {
strcpy_P(buff, PSTR("Inf b/s"));
} else {
unsigned long delta = stop - start;
float r = 1000.0 * (float)bytes / (float)delta;
if (r >= 1000000.0) {
sprintf_P(buff, PSTR("%0.2f MB/s"), r / 1000000.0);
} else if (r >= 1000.0) {
sprintf_P(buff, PSTR("%0.2f KB/s"), r / 1000.0);
} else {
sprintf_P(buff, PSTR("%d bytes/s"), (int)r);
}
}
return buff;
}

void DoTest(FS *fs) {
if (!fs->format()) {
Serial.printf("Unable to format(), aborting\n");
Expand All @@ -30,7 +49,7 @@ void DoTest(FS *fs) {
}

Serial.printf("Creating %dKB file, may take a while...\n", TESTSIZEKB);
long start = millis();
unsigned long start = millis();
File f = fs->open("/testwrite.bin", "w");
if (!f) {
Serial.printf("Unable to open file for writing, aborting\n");
Expand All @@ -42,8 +61,8 @@ void DoTest(FS *fs) {
}
}
f.close();
long stop = millis();
Serial.printf("==> Time to write %dKB in 256b chunks = %ld milliseconds\n", TESTSIZEKB, stop - start);
unsigned long stop = millis();
Serial.printf("==> Time to write %dKB in 256b chunks = %lu milliseconds\n", TESTSIZEKB, stop - start);

f = fs->open("/testwrite.bin", "r");
Serial.printf("==> Created file size = %d\n", f.size());
Expand All @@ -59,7 +78,7 @@ void DoTest(FS *fs) {
}
f.close();
stop = millis();
Serial.printf("==> Time to read %dKB sequentially in 256b chunks = %ld milliseconds = %ld bytes/s\n", TESTSIZEKB, stop - start, TESTSIZEKB * 1024 / (stop - start) * 1000);
Serial.printf("==> Time to read %dKB sequentially in 256b chunks = %lu milliseconds = %s\n", TESTSIZEKB, stop - start, rate(start, stop, TESTSIZEKB * 1024));

Serial.printf("Reading %dKB file MISALIGNED in flash and RAM sequentially in 256b chunks\n", TESTSIZEKB);
start = millis();
Expand All @@ -72,8 +91,7 @@ void DoTest(FS *fs) {
}
f.close();
stop = millis();
Serial.printf("==> Time to read %dKB sequentially MISALIGNED in flash and RAM in 256b chunks = %ld milliseconds = %ld bytes/s\n", TESTSIZEKB, stop - start, TESTSIZEKB * 1024 / (stop - start) * 1000);

Serial.printf("==> Time to read %dKB sequentially MISALIGNED in flash and RAM in 256b chunks = %lu milliseconds = %s\n", TESTSIZEKB, stop - start, rate(start, stop, TESTSIZEKB * 1024));

Serial.printf("Reading %dKB file in reverse by 256b chunks\n", TESTSIZEKB);
start = millis();
Expand All @@ -92,8 +110,7 @@ void DoTest(FS *fs) {
}
f.close();
stop = millis();
Serial.printf("==> Time to read %dKB in reverse in 256b chunks = %ld milliseconds = %ld bytes/s\n", TESTSIZEKB, stop - start, TESTSIZEKB * 1024 / (stop - start) * 1000);

Serial.printf("==> Time to read %dKB in reverse in 256b chunks = %lu milliseconds = %s\n", TESTSIZEKB, stop - start, rate(start, stop, TESTSIZEKB * 1024));

Serial.printf("Writing 64K file in 1-byte chunks\n");
start = millis();
Expand All @@ -103,7 +120,7 @@ void DoTest(FS *fs) {
}
f.close();
stop = millis();
Serial.printf("==> Time to write 64KB in 1b chunks = %ld milliseconds = %ld bytes/s\n", stop - start, 65536 / (stop - start) * 1000);
Serial.printf("==> Time to write 64KB in 1b chunks = %lu milliseconds = %s\n", stop - start, rate(start, stop, 65536));

Serial.printf("Reading 64K file in 1-byte chunks\n");
start = millis();
Expand All @@ -114,9 +131,7 @@ void DoTest(FS *fs) {
}
f.close();
stop = millis();
Serial.printf("==> Time to read 64KB in 1b chunks = %ld milliseconds = %ld bytes/s\n", stop - start, 65536 / (stop - start) * 1000);


Serial.printf("==> Time to read 64KB in 1b chunks = %lu milliseconds = %s\n", stop - start, rate(start, stop, 65536));
}

void setup() {
Expand Down

0 comments on commit aabfd73

Please sign in to comment.