1[cases.bench_file_read]
2# 0 = in-order
3# 1 = reversed-order
4# 2 = random-order
5defines.ORDER = [0, 1, 2]
6defines.SIZE = '128*1024'
7defines.CHUNK_SIZE = 64
8code = '''
9    lfs_t lfs;
10    lfs_format(&lfs, cfg) => 0;
11    lfs_mount(&lfs, cfg) => 0;
12    lfs_size_t chunks = (SIZE+CHUNK_SIZE-1)/CHUNK_SIZE;
13
14    // first write the file
15    lfs_file_t file;
16    uint8_t buffer[CHUNK_SIZE];
17    lfs_file_open(&lfs, &file, "file",
18            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
19    for (lfs_size_t i = 0; i < chunks; i++) {
20        uint32_t chunk_prng = i;
21        for (lfs_size_t j = 0; j < CHUNK_SIZE; j++) {
22            buffer[j] = BENCH_PRNG(&chunk_prng);
23        }
24
25        lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
26    }
27    lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
28    lfs_file_close(&lfs, &file) => 0;
29
30    // then read the file
31    BENCH_START();
32    lfs_file_open(&lfs, &file, "file", LFS_O_RDONLY) => 0;
33
34    uint32_t prng = 42;
35    for (lfs_size_t i = 0; i < chunks; i++) {
36        lfs_off_t i_
37            = (ORDER == 0) ? i
38            : (ORDER == 1) ? (chunks-1-i)
39            : BENCH_PRNG(&prng) % chunks;
40        lfs_file_seek(&lfs, &file, i_*CHUNK_SIZE, LFS_SEEK_SET)
41                => i_*CHUNK_SIZE;
42        lfs_file_read(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
43
44        uint32_t chunk_prng = i_;
45        for (lfs_size_t j = 0; j < CHUNK_SIZE; j++) {
46            assert(buffer[j] == BENCH_PRNG(&chunk_prng));
47        }
48    }
49
50    lfs_file_close(&lfs, &file) => 0;
51    BENCH_STOP();
52
53    lfs_unmount(&lfs) => 0;
54'''
55
56[cases.bench_file_write]
57# 0 = in-order
58# 1 = reversed-order
59# 2 = random-order
60defines.ORDER = [0, 1, 2]
61defines.SIZE = '128*1024'
62defines.CHUNK_SIZE = 64
63code = '''
64    lfs_t lfs;
65    lfs_format(&lfs, cfg) => 0;
66    lfs_mount(&lfs, cfg) => 0;
67    lfs_size_t chunks = (SIZE+CHUNK_SIZE-1)/CHUNK_SIZE;
68
69    BENCH_START();
70    lfs_file_t file;
71    lfs_file_open(&lfs, &file, "file",
72            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
73
74    uint8_t buffer[CHUNK_SIZE];
75    uint32_t prng = 42;
76    for (lfs_size_t i = 0; i < chunks; i++) {
77        lfs_off_t i_
78            = (ORDER == 0) ? i
79            : (ORDER == 1) ? (chunks-1-i)
80            : BENCH_PRNG(&prng) % chunks;
81        uint32_t chunk_prng = i_;
82        for (lfs_size_t j = 0; j < CHUNK_SIZE; j++) {
83            buffer[j] = BENCH_PRNG(&chunk_prng);
84        }
85
86        lfs_file_seek(&lfs, &file, i_*CHUNK_SIZE, LFS_SEEK_SET)
87                => i_*CHUNK_SIZE;
88        lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
89    }
90
91    lfs_file_close(&lfs, &file) => 0;
92    BENCH_STOP();
93
94    lfs_unmount(&lfs) => 0;
95'''
96