1
6
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
37
38
40
41
42
49
50
51
52
53
54
55
56
57
58
59
60
61
62
69
70
71
72
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
97
98
99
103
104
105
106
107
108
113
114
121
122
123
124
125
128
129
130
133
134
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
192
193
197
198
202
203
208
209
210
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
241
242
243
244
251
252
253
254
/* ... */
/* ... */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include "esp_err.h"
#include "esp_random.h"
#include "driver/sdmmc_types.h"
#include "sdmmc_cmd.h"
#include "wear_levelling.h"
#include "esp_log.h"
#include "esp_check.h"
#include "sdkconfig.h"
#include "perf_benchmark_example_tests.h"19 includes
#if CONFIG_EXAMPLE_USE_BYTES
#define UNIT_STRING "B"
#define UNIT_MULTIPLIER (1)/* ... */
#else
#define UNIT_STRING "b"
#define UNIT_MULTIPLIER (8)/* ... */
#endif
static void print_results(const char *name, double time, size_t size, int repeat_count)
{
double average = time / repeat_count;
double speed = (size / average) / (1024 * 1024) * (1000 * 1000);
printf("[%-55s] (%dx) %8.3f ms %8.2f k" UNIT_STRING " %10.3f M" UNIT_STRING "/s\n",
name, repeat_count, (float) average / 1000, (float)size * UNIT_MULTIPLIER / 1024, speed);
}{ ... }
void spiflash_speed_test_raw_run(size_t repeat_count)
{
const size_t buf_size = CONFIG_EXAMPLE_TARGET_RW_SIZE;
uint32_t* buf = (uint32_t*) heap_caps_calloc(1, buf_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_CACHE_ALIGNED | MALLOC_CAP_DMA);
assert(buf != NULL);
esp_fill_random(buf, buf_size);
struct timeval tv_start;
struct timeval tv_end;
struct timeval tv_diff;
double time = 0;
for (int i = 0; i < repeat_count; ++i) {
gettimeofday(&tv_start, NULL);
ESP_ERROR_CHECK(wl_write(s_wl_handle, 0, buf, buf_size));
gettimeofday(&tv_end, NULL);
timersub(&tv_end, &tv_start, &tv_diff);
time += (tv_diff.tv_sec * 1000000 + tv_diff.tv_usec);
}{...}
print_results("SPI Flash raw write", time, buf_size, repeat_count);
time = 0;
for (int i = 0; i < repeat_count; ++i) {
gettimeofday(&tv_start, NULL);
ESP_ERROR_CHECK(wl_read(s_wl_handle, 0, buf, buf_size));
gettimeofday(&tv_end, NULL);
timersub(&tv_end, &tv_start, &tv_diff);
time += (tv_diff.tv_sec * 1000000 + tv_diff.tv_usec);
}{...}
print_results("SPI Flash raw read", time, buf_size, repeat_count);
free(buf);
}{ ... }
static void run_fs_test(const char *name, const char* path, void *buf, size_t buf_size, bool is_write, bool cleanup, bool new_file, size_t repeat_count)
{
struct timeval tv_start;
struct timeval tv_end;
struct timeval tv_diff;
double time = 0;
int fd = -1;
const int flags = is_write ? O_CREAT | O_TRUNC | O_RDWR : O_RDONLY;
if (!new_file) {
fd = open(path, flags, 0666);
assert(fd >= 0);
}{...}
for (int i = 0; i < repeat_count; ++i) {
if (new_file) {
fd = open(path, flags, 0666);
assert(fd >= 0);
}{...}
gettimeofday(&tv_start, NULL);
int ret = is_write ? write(fd, buf, buf_size) : read(fd, buf, buf_size);
gettimeofday(&tv_end, NULL);
if (ret < 0) {
perror(is_write ? "write" : "read");
close(fd);
return;
}{...}
if (ret != buf_size) {
ESP_LOGE("fs_test",
"Could not %s full buffer size. Actually %s bytes: %d.",
is_write ? "write" : "read",
is_write ? "wrote" : "read",
ret);
}{...}
timersub(&tv_end, &tv_start, &tv_diff);
time += (tv_diff.tv_sec * 1000000 + tv_diff.tv_usec);
if (new_file) {
close(fd);
}{...}
}{...}
if (!new_file) {
close(fd);
}{...}
if (cleanup) {
unlink(path);
}{...}
print_results(name, time, buf_size, repeat_count);
}{ ... }
static void run_fs_tests(const char *base_path, const char *type, bool new_file, size_t repeat_count)
{
char path[256];
snprintf(path, sizeof(path), "%s/test.bin", base_path);
char name[256];
strcpy(name, type);
if (new_file)
strcat(name, " (new file)");
strcat(name, ": ");
size_t name_prefix_len = strlen(name);
const size_t less_than_target_size = CONFIG_EXAMPLE_TARGET_RW_SIZE / 1.2;
const size_t exact_target_size = CONFIG_EXAMPLE_TARGET_RW_SIZE;
const size_t more_than_target_size = CONFIG_EXAMPLE_TARGET_RW_SIZE * 1.2;
const size_t tiny_size = 255;
assert(tiny_size < less_than_target_size);
const size_t buf_size = more_than_target_size;
uint32_t* buf = (uint32_t*) heap_caps_calloc(1, buf_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_CACHE_ALIGNED | MALLOC_CAP_DMA);
assert(buf != NULL);
esp_fill_random(buf, buf_size);
run_fs_test(strcat(name, "write target size"), path, buf, exact_target_size, true, false, new_file, repeat_count);
name[name_prefix_len] = '\0';
run_fs_test(strcat(name, "read target size"), path, buf, exact_target_size, false, true, new_file, repeat_count);
name[name_prefix_len] = '\0';
run_fs_test(strcat(name, "write more than target size"), path, buf, more_than_target_size, true, false, new_file, repeat_count);
name[name_prefix_len] = '\0';
run_fs_test(strcat(name, "read more than target size"), path, buf, more_than_target_size, false, true, new_file, repeat_count);
name[name_prefix_len] = '\0';
run_fs_test(strcat(name, "write less than target size"), path, buf, less_than_target_size, true, false, new_file, repeat_count);
name[name_prefix_len] = '\0';
run_fs_test(strcat(name, "read less than target size"), path, buf, less_than_target_size, false, true, new_file, repeat_count);
name[name_prefix_len] = '\0';
run_fs_test(strcat(name, "write tiny size"), path, buf, tiny_size, true, false, new_file, repeat_count);
name[name_prefix_len] = '\0';
run_fs_test(strcat(name, "read tiny size"), path, buf, tiny_size, false, true, new_file, repeat_count);
name[name_prefix_len] = '\0';
free(buf);
}{ ... }
void spiflash_speed_test_spiffs_run(size_t repeat_count)
{
run_fs_tests(FLASH_BASE_PATH, "SPI SPIFFS", true, repeat_count);
}{ ... }
void spiflash_speed_test_fatfs_run(size_t repeat_count)
{
run_fs_tests(FLASH_BASE_PATH, "SPI FATFS", true, repeat_count);
}{ ... }
void spiflash_speed_test_littlefs_run(size_t repeat_count)
{
run_fs_tests(FLASH_BASE_PATH, "SPI LittleFS", true, repeat_count);
}{ ... }
void sdcard_speed_test_fatfs_run(size_t repeat_count)
{
run_fs_tests(SD_BASE_PATH, "SD FATFS", true, repeat_count);
run_fs_tests(SD_BASE_PATH, "SD FATFS", false, repeat_count);
}{ ... }
void sdcard_speed_test_littlefs_run(size_t repeat_count)
{
run_fs_tests(SD_BASE_PATH, "SD LittleFS", true, repeat_count);
run_fs_tests(SD_BASE_PATH, "SD LittleFS", false, repeat_count);
}{ ... }
void sdcard_speed_test_raw_run(sdmmc_card_t *card, size_t repeat_count)
{
uint32_t sector_size = card->csd.sector_size;
size_t sector_count = CONFIG_EXAMPLE_TARGET_RW_SIZE / sector_size;
size_t subsection_sector_count = sector_count / 4;
char *buf = (char *) heap_caps_calloc(1, sector_count * sector_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_CACHE_ALIGNED | MALLOC_CAP_DMA);
assert(buf != NULL);
struct timeval tv_start;
struct timeval tv_end;
struct timeval tv_diff;
for(int i = 0; i < 4; i++) {
const size_t count = subsection_sector_count * (i + 1);
double time = 0;
for (int i = 0; i < repeat_count; ++i) {
gettimeofday(&tv_start, NULL);
ESP_ERROR_CHECK(sdmmc_write_sectors(card, buf, 0, sector_count));
gettimeofday(&tv_end, NULL);
timersub(&tv_end, &tv_start, &tv_diff);
time += (tv_diff.tv_sec * 1000000 + tv_diff.tv_usec);
}{...}
print_results("SD raw write", time, count * sector_size, repeat_count);
time = 0;
for (int i = 0; i < repeat_count; ++i) {
gettimeofday(&tv_start, NULL);
ESP_ERROR_CHECK(sdmmc_read_sectors(card, buf, 0, sector_count));
gettimeofday(&tv_end, NULL);
timersub(&tv_end, &tv_start, &tv_diff);
time += (tv_diff.tv_sec * 1000000 + tv_diff.tv_usec);
}{...}
print_results("SD raw read", time, count * sector_size, repeat_count);
}{...}
free(buf);
}{ ... }