From 8b500211841fb6190b14b85a46d9b90c42031e73 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 15 Oct 2021 10:24:57 -0700 Subject: [PATCH 1/2] Clean up SpeedTest output, avoid div-by-0 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". --- .../LittleFS/examples/SpeedTest/SpeedTest.ino | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino b/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino index 5987e10807..28ed1d4907 100644 --- a/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino +++ b/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino @@ -14,6 +14,24 @@ // 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(long start, long stop, long bytes) { + static char buff[64]; + if (stop == start) { + strcpy_P(buff, PSTR("Inf b/s")); + } else { + float r = 1000.0 * (float)bytes / (float)(stop - start); + 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"); @@ -59,7 +77,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 = %ld 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(); @@ -72,8 +90,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 = %ld 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(); @@ -92,8 +109,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 = %ld milliseconds = %s\n", TESTSIZEKB, stop - start, rate(start, stop, TESTSIZEKB * 1024)); Serial.printf("Writing 64K file in 1-byte chunks\n"); start = millis(); @@ -103,7 +119,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 = %ld milliseconds = %s\n", stop - start, rate(start, stop, 65536)); Serial.printf("Reading 64K file in 1-byte chunks\n"); start = millis(); @@ -114,9 +130,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 = %ld milliseconds = %s\n", stop - start, rate(start, stop, 65536)); } void setup() { From 2091b12ae62e4bd2ae9fff01dd0237692600e5a4 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sat, 16 Oct 2021 14:13:28 -0700 Subject: [PATCH 2/2] Use only unsigned long arith for rate calculation --- .../LittleFS/examples/SpeedTest/SpeedTest.ino | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino b/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino index 28ed1d4907..6fe300f7d9 100644 --- a/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino +++ b/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino @@ -15,16 +15,17 @@ #define TESTSIZEKB 512 // Format speed in bytes/second. Static buffer so not re-entrant safe -const char *rate(long start, long stop, long bytes) { +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 { - float r = 1000.0 * (float)bytes / (float)(stop - start); + 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); + sprintf_P(buff, PSTR("%0.2f KB/s"), r / 1000.0); } else { sprintf_P(buff, PSTR("%d bytes/s"), (int)r); } @@ -48,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"); @@ -60,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()); @@ -77,7 +78,7 @@ void DoTest(FS *fs) { } f.close(); stop = millis(); - Serial.printf("==> Time to read %dKB sequentially in 256b chunks = %ld milliseconds = %s\n", TESTSIZEKB, stop - start, rate(start, stop, TESTSIZEKB * 1024)); + 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(); @@ -90,7 +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 = %s\n", TESTSIZEKB, stop - start, rate(start, stop, TESTSIZEKB * 1024)); + 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(); @@ -109,7 +110,7 @@ void DoTest(FS *fs) { } f.close(); stop = millis(); - Serial.printf("==> Time to read %dKB in reverse in 256b chunks = %ld milliseconds = %s\n", TESTSIZEKB, stop - start, rate(start, stop, TESTSIZEKB * 1024)); + 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(); @@ -119,7 +120,7 @@ void DoTest(FS *fs) { } f.close(); stop = millis(); - Serial.printf("==> Time to write 64KB in 1b chunks = %ld milliseconds = %s\n", stop - start, rate(start, stop, 65536)); + 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(); @@ -130,7 +131,7 @@ void DoTest(FS *fs) { } f.close(); stop = millis(); - Serial.printf("==> Time to read 64KB in 1b chunks = %ld milliseconds = %s\n", stop - start, rate(start, stop, 65536)); + Serial.printf("==> Time to read 64KB in 1b chunks = %lu milliseconds = %s\n", stop - start, rate(start, stop, 65536)); } void setup() {