bench_file.toml 2.6 KB

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