bench_file.toml 2.8 KB

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