Skip to content

Commit aabfd73

Browse files
Clean up SpeedTest output, avoid div-by-0 (#8340)
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 ````
1 parent c312a2e commit aabfd73

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

Diff for: libraries/LittleFS/examples/SpeedTest/SpeedTest.ino

+27-12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@
1414
// How large of a file to test
1515
#define TESTSIZEKB 512
1616

17+
// Format speed in bytes/second. Static buffer so not re-entrant safe
18+
const char *rate(unsigned long start, unsigned long stop, unsigned long bytes) {
19+
static char buff[64];
20+
if (stop == start) {
21+
strcpy_P(buff, PSTR("Inf b/s"));
22+
} else {
23+
unsigned long delta = stop - start;
24+
float r = 1000.0 * (float)bytes / (float)delta;
25+
if (r >= 1000000.0) {
26+
sprintf_P(buff, PSTR("%0.2f MB/s"), r / 1000000.0);
27+
} else if (r >= 1000.0) {
28+
sprintf_P(buff, PSTR("%0.2f KB/s"), r / 1000.0);
29+
} else {
30+
sprintf_P(buff, PSTR("%d bytes/s"), (int)r);
31+
}
32+
}
33+
return buff;
34+
}
35+
1736
void DoTest(FS *fs) {
1837
if (!fs->format()) {
1938
Serial.printf("Unable to format(), aborting\n");
@@ -30,7 +49,7 @@ void DoTest(FS *fs) {
3049
}
3150

3251
Serial.printf("Creating %dKB file, may take a while...\n", TESTSIZEKB);
33-
long start = millis();
52+
unsigned long start = millis();
3453
File f = fs->open("/testwrite.bin", "w");
3554
if (!f) {
3655
Serial.printf("Unable to open file for writing, aborting\n");
@@ -42,8 +61,8 @@ void DoTest(FS *fs) {
4261
}
4362
}
4463
f.close();
45-
long stop = millis();
46-
Serial.printf("==> Time to write %dKB in 256b chunks = %ld milliseconds\n", TESTSIZEKB, stop - start);
64+
unsigned long stop = millis();
65+
Serial.printf("==> Time to write %dKB in 256b chunks = %lu milliseconds\n", TESTSIZEKB, stop - start);
4766

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

6483
Serial.printf("Reading %dKB file MISALIGNED in flash and RAM sequentially in 256b chunks\n", TESTSIZEKB);
6584
start = millis();
@@ -72,8 +91,7 @@ void DoTest(FS *fs) {
7291
}
7392
f.close();
7493
stop = millis();
75-
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);
76-
94+
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));
7795

7896
Serial.printf("Reading %dKB file in reverse by 256b chunks\n", TESTSIZEKB);
7997
start = millis();
@@ -92,8 +110,7 @@ void DoTest(FS *fs) {
92110
}
93111
f.close();
94112
stop = millis();
95-
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);
96-
113+
Serial.printf("==> Time to read %dKB in reverse in 256b chunks = %lu milliseconds = %s\n", TESTSIZEKB, stop - start, rate(start, stop, TESTSIZEKB * 1024));
97114

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

108125
Serial.printf("Reading 64K file in 1-byte chunks\n");
109126
start = millis();
@@ -114,9 +131,7 @@ void DoTest(FS *fs) {
114131
}
115132
f.close();
116133
stop = millis();
117-
Serial.printf("==> Time to read 64KB in 1b chunks = %ld milliseconds = %ld bytes/s\n", stop - start, 65536 / (stop - start) * 1000);
118-
119-
134+
Serial.printf("==> Time to read 64KB in 1b chunks = %lu milliseconds = %s\n", stop - start, rate(start, stop, 65536));
120135
}
121136

122137
void setup() {

0 commit comments

Comments
 (0)