1[cases.bench_dir_open]
2# 0 = in-order
3# 1 = reversed-order
4# 2 = random-order
5defines.ORDER = [0, 1, 2]
6defines.N = 1024
7defines.FILE_SIZE = 8
8defines.CHUNK_SIZE = 8
9code = '''
10    lfs_t lfs;
11    lfs_format(&lfs, cfg) => 0;
12    lfs_mount(&lfs, cfg) => 0;
13
14    // first create the files
15    char name[256];
16    uint8_t buffer[CHUNK_SIZE];
17    for (lfs_size_t i = 0; i < N; i++) {
18        sprintf(name, "file%08x", i);
19        lfs_file_t file;
20        lfs_file_open(&lfs, &file, name,
21                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
22
23        uint32_t file_prng = i;
24        for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) {
25            for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) {
26                buffer[k] = BENCH_PRNG(&file_prng);
27            }
28            lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
29        }
30
31        lfs_file_close(&lfs, &file) => 0;
32    }
33
34    // then read the files
35    BENCH_START();
36    uint32_t prng = 42;
37    for (lfs_size_t i = 0; i < N; i++) {
38        lfs_off_t i_
39            = (ORDER == 0) ? i
40            : (ORDER == 1) ? (N-1-i)
41            : BENCH_PRNG(&prng) % N;
42        sprintf(name, "file%08x", i_);
43        lfs_file_t file;
44        lfs_file_open(&lfs, &file, name, LFS_O_RDONLY) => 0;
45
46        uint32_t file_prng = i_;
47        for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) {
48            lfs_file_read(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
49            for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) {
50                assert(buffer[k] == BENCH_PRNG(&file_prng));
51            }
52        }
53
54        lfs_file_close(&lfs, &file) => 0;
55    }
56    BENCH_STOP();
57
58    lfs_unmount(&lfs) => 0;
59'''
60
61[cases.bench_dir_creat]
62# 0 = in-order
63# 1 = reversed-order
64# 2 = random-order
65defines.ORDER = [0, 1, 2]
66defines.N = 1024
67defines.FILE_SIZE = 8
68defines.CHUNK_SIZE = 8
69code = '''
70    lfs_t lfs;
71    lfs_format(&lfs, cfg) => 0;
72    lfs_mount(&lfs, cfg) => 0;
73
74    BENCH_START();
75    uint32_t prng = 42;
76    char name[256];
77    uint8_t buffer[CHUNK_SIZE];
78    for (lfs_size_t i = 0; i < N; i++) {
79        lfs_off_t i_
80            = (ORDER == 0) ? i
81            : (ORDER == 1) ? (N-1-i)
82            : BENCH_PRNG(&prng) % N;
83        sprintf(name, "file%08x", i_);
84        lfs_file_t file;
85        lfs_file_open(&lfs, &file, name,
86                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
87
88        uint32_t file_prng = i_;
89        for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) {
90            for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) {
91                buffer[k] = BENCH_PRNG(&file_prng);
92            }
93            lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
94        }
95
96        lfs_file_close(&lfs, &file) => 0;
97    }
98    BENCH_STOP();
99
100    lfs_unmount(&lfs) => 0;
101'''
102
103[cases.bench_dir_remove]
104# 0 = in-order
105# 1 = reversed-order
106# 2 = random-order
107defines.ORDER = [0, 1, 2]
108defines.N = 1024
109defines.FILE_SIZE = 8
110defines.CHUNK_SIZE = 8
111code = '''
112    lfs_t lfs;
113    lfs_format(&lfs, cfg) => 0;
114    lfs_mount(&lfs, cfg) => 0;
115
116    // first create the files
117    char name[256];
118    uint8_t buffer[CHUNK_SIZE];
119    for (lfs_size_t i = 0; i < N; i++) {
120        sprintf(name, "file%08x", i);
121        lfs_file_t file;
122        lfs_file_open(&lfs, &file, name,
123                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
124
125        uint32_t file_prng = i;
126        for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) {
127            for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) {
128                buffer[k] = BENCH_PRNG(&file_prng);
129            }
130            lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
131        }
132
133        lfs_file_close(&lfs, &file) => 0;
134    }
135
136    // then remove the files
137    BENCH_START();
138    uint32_t prng = 42;
139    for (lfs_size_t i = 0; i < N; i++) {
140        lfs_off_t i_
141            = (ORDER == 0) ? i
142            : (ORDER == 1) ? (N-1-i)
143            : BENCH_PRNG(&prng) % N;
144        sprintf(name, "file%08x", i_);
145        int err = lfs_remove(&lfs, name);
146        assert(!err || err == LFS_ERR_NOENT);
147    }
148    BENCH_STOP();
149
150    lfs_unmount(&lfs) => 0;
151'''
152
153[cases.bench_dir_read]
154defines.N = 1024
155defines.FILE_SIZE = 8
156defines.CHUNK_SIZE = 8
157code = '''
158    lfs_t lfs;
159    lfs_format(&lfs, cfg) => 0;
160    lfs_mount(&lfs, cfg) => 0;
161
162    // first create the files
163    char name[256];
164    uint8_t buffer[CHUNK_SIZE];
165    for (lfs_size_t i = 0; i < N; i++) {
166        sprintf(name, "file%08x", i);
167        lfs_file_t file;
168        lfs_file_open(&lfs, &file, name,
169                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
170
171        uint32_t file_prng = i;
172        for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) {
173            for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) {
174                buffer[k] = BENCH_PRNG(&file_prng);
175            }
176            lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
177        }
178
179        lfs_file_close(&lfs, &file) => 0;
180    }
181
182    // then read the directory
183    BENCH_START();
184    lfs_dir_t dir;
185    lfs_dir_open(&lfs, &dir, "/") => 0;
186    struct lfs_info info;
187    lfs_dir_read(&lfs, &dir, &info) => 1;
188    assert(info.type == LFS_TYPE_DIR);
189    assert(strcmp(info.name, ".") == 0);
190    lfs_dir_read(&lfs, &dir, &info) => 1;
191    assert(info.type == LFS_TYPE_DIR);
192    assert(strcmp(info.name, "..") == 0);
193    for (int i = 0; i < N; i++) {
194        sprintf(name, "file%08x", i);
195        lfs_dir_read(&lfs, &dir, &info) => 1;
196        assert(info.type == LFS_TYPE_REG);
197        assert(strcmp(info.name, name) == 0);
198    }
199    lfs_dir_read(&lfs, &dir, &info) => 0;
200    lfs_dir_close(&lfs, &dir) => 0;
201    BENCH_STOP();
202
203    lfs_unmount(&lfs) => 0;
204'''
205
206[cases.bench_dir_mkdir]
207# 0 = in-order
208# 1 = reversed-order
209# 2 = random-order
210defines.ORDER = [0, 1, 2]
211defines.N = 8
212code = '''
213    lfs_t lfs;
214    lfs_format(&lfs, cfg) => 0;
215    lfs_mount(&lfs, cfg) => 0;
216
217    BENCH_START();
218    uint32_t prng = 42;
219    char name[256];
220    for (lfs_size_t i = 0; i < N; i++) {
221        lfs_off_t i_
222            = (ORDER == 0) ? i
223            : (ORDER == 1) ? (N-1-i)
224            : BENCH_PRNG(&prng) % N;
225        printf("hm %d\n", i);
226        sprintf(name, "dir%08x", i_);
227        int err = lfs_mkdir(&lfs, name);
228        assert(!err || err == LFS_ERR_EXIST);
229    }
230    BENCH_STOP();
231
232    lfs_unmount(&lfs) => 0;
233'''
234
235[cases.bench_dir_rmdir]
236# 0 = in-order
237# 1 = reversed-order
238# 2 = random-order
239defines.ORDER = [0, 1, 2]
240defines.N = 8
241code = '''
242    lfs_t lfs;
243    lfs_format(&lfs, cfg) => 0;
244    lfs_mount(&lfs, cfg) => 0;
245
246    // first create the dirs
247    char name[256];
248    for (lfs_size_t i = 0; i < N; i++) {
249        sprintf(name, "dir%08x", i);
250        lfs_mkdir(&lfs, name) => 0;
251    }
252
253    // then remove the dirs
254    BENCH_START();
255    uint32_t prng = 42;
256    for (lfs_size_t i = 0; i < N; i++) {
257        lfs_off_t i_
258            = (ORDER == 0) ? i
259            : (ORDER == 1) ? (N-1-i)
260            : BENCH_PRNG(&prng) % N;
261        sprintf(name, "dir%08x", i_);
262        int err = lfs_remove(&lfs, name);
263        assert(!err || err == LFS_ERR_NOENT);
264    }
265    BENCH_STOP();
266
267    lfs_unmount(&lfs) => 0;
268'''
269
270
271