Select one of the symbols to view example projects that use it.
 
Outline
#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"
#define UNIT_STRING
#define UNIT_MULTIPLIER
#define UNIT_STRING
#define UNIT_MULTIPLIER
print_results(const char *, double, size_t, int)
spiflash_speed_test_raw_run(size_t)
run_fs_test(const char *, const char *, void *, size_t, bool, bool, bool, size_t)
run_fs_tests(const char *, const char *, bool, size_t)
spiflash_speed_test_spiffs_run(size_t)
spiflash_speed_test_fatfs_run(size_t)
spiflash_speed_test_littlefs_run(size_t)
sdcard_speed_test_fatfs_run(size_t)
sdcard_speed_test_littlefs_run(size_t)
sdcard_speed_test_raw_run(sdmmc_card_t *, size_t)
Files
loading (2/6)...
SourceVuESP-IDF Framework and Examplesperf_benchmark samplemain/perf_benchmark_example_tests.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ /* This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *//* ... */ #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; // Best performance is achieved when the buffer is internal (not PSRAM) and aligned to RAM line size 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); }{ ... }
Details