storage-settings-benchmark.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "../storage-settings.h"
  2. #define BENCH_DATA_SIZE 4096
  3. #define BENCH_COUNT 6
  4. #define BENCH_REPEATS 4
  5. #define BENCH_FILE "/ext/rwfiletest.bin"
  6. static void
  7. storage_settings_scene_benchmark_dialog_callback(DialogExResult result, void* context) {
  8. StorageSettings* app = context;
  9. view_dispatcher_send_custom_event(app->view_dispatcher, result);
  10. }
  11. static bool storage_settings_bench_write(
  12. Storage* api,
  13. uint16_t size,
  14. const uint8_t* data,
  15. uint32_t* speed) {
  16. File* file = storage_file_alloc(api);
  17. bool result = true;
  18. if(storage_file_open(file, BENCH_FILE, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  19. uint32_t ticks;
  20. ticks = osKernelGetTickCount();
  21. for(size_t repeat = 0; repeat < BENCH_REPEATS; repeat++) {
  22. for(size_t i = 0; i < BENCH_DATA_SIZE / size; i++) {
  23. if(storage_file_write(file, (data + i * size), size) != size) {
  24. result = false;
  25. break;
  26. }
  27. }
  28. }
  29. ticks = osKernelGetTickCount() - ticks;
  30. *speed = BENCH_DATA_SIZE * osKernelGetTickFreq() * BENCH_REPEATS;
  31. *speed /= ticks;
  32. *speed /= 1024;
  33. }
  34. storage_file_close(file);
  35. storage_file_free(file);
  36. return result;
  37. }
  38. static bool
  39. storage_settings_bench_read(Storage* api, uint16_t size, uint8_t* data, uint32_t* speed) {
  40. File* file = storage_file_alloc(api);
  41. bool result = true;
  42. *speed = -1;
  43. if(storage_file_open(file, BENCH_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) {
  44. uint32_t ticks;
  45. ticks = osKernelGetTickCount();
  46. for(size_t repeat = 0; repeat < BENCH_REPEATS; repeat++) {
  47. for(size_t i = 0; i < BENCH_DATA_SIZE / size; i++) {
  48. if(storage_file_read(file, (data + i * size), size) != size) {
  49. result = false;
  50. break;
  51. }
  52. }
  53. }
  54. ticks = osKernelGetTickCount() - ticks;
  55. *speed = BENCH_DATA_SIZE * osKernelGetTickFreq() * BENCH_REPEATS;
  56. *speed /= ticks;
  57. *speed /= 1024;
  58. }
  59. storage_file_close(file);
  60. storage_file_free(file);
  61. return result;
  62. }
  63. static void storage_settings_benchmark(StorageSettings* app) {
  64. DialogEx* dialog_ex = app->dialog_ex;
  65. uint8_t* bench_data;
  66. dialog_ex_set_header(dialog_ex, "Preparing data...", 64, 32, AlignCenter, AlignCenter);
  67. bench_data = malloc(BENCH_DATA_SIZE);
  68. for(size_t i = 0; i < BENCH_DATA_SIZE; i++) {
  69. bench_data[i] = (uint8_t)i;
  70. }
  71. uint16_t bench_size[BENCH_COUNT] = {1, 8, 32, 256, 1024, 4096};
  72. uint32_t bench_w_speed[BENCH_COUNT] = {0, 0, 0, 0, 0, 0};
  73. uint32_t bench_r_speed[BENCH_COUNT] = {0, 0, 0, 0, 0, 0};
  74. dialog_ex_set_header(dialog_ex, "Benchmarking...", 64, 32, AlignCenter, AlignCenter);
  75. for(size_t i = 0; i < BENCH_COUNT; i++) {
  76. if(!storage_settings_bench_write(app->fs_api, bench_size[i], bench_data, &bench_w_speed[i]))
  77. break;
  78. if(i > 0) string_cat_printf(app->text_string, "\n");
  79. string_cat_printf(app->text_string, "%ub : W %luK ", bench_size[i], bench_w_speed[i]);
  80. dialog_ex_set_header(dialog_ex, NULL, 0, 0, AlignCenter, AlignCenter);
  81. dialog_ex_set_text(
  82. dialog_ex, string_get_cstr(app->text_string), 0, 32, AlignLeft, AlignCenter);
  83. if(!storage_settings_bench_read(app->fs_api, bench_size[i], bench_data, &bench_r_speed[i]))
  84. break;
  85. string_cat_printf(app->text_string, "R %luK", bench_r_speed[i]);
  86. dialog_ex_set_text(
  87. dialog_ex, string_get_cstr(app->text_string), 0, 32, AlignLeft, AlignCenter);
  88. }
  89. free(bench_data);
  90. }
  91. void storage_settings_scene_benchmark_on_enter(void* context) {
  92. StorageSettings* app = context;
  93. DialogEx* dialog_ex = app->dialog_ex;
  94. dialog_ex_set_context(dialog_ex, app);
  95. dialog_ex_set_result_callback(dialog_ex, storage_settings_scene_benchmark_dialog_callback);
  96. view_dispatcher_switch_to_view(app->view_dispatcher, StorageSettingsViewDialogEx);
  97. if(storage_sd_status(app->fs_api) != FSE_OK) {
  98. dialog_ex_set_header(dialog_ex, "SD card not mounted", 64, 10, AlignCenter, AlignCenter);
  99. dialog_ex_set_text(
  100. dialog_ex,
  101. "If an SD card is inserted,\r\npull it out and reinsert it",
  102. 64,
  103. 32,
  104. AlignCenter,
  105. AlignCenter);
  106. dialog_ex_set_left_button_text(dialog_ex, "Back");
  107. } else {
  108. storage_settings_benchmark(app);
  109. notification_message(app->notification, &sequence_blink_green_100);
  110. }
  111. }
  112. bool storage_settings_scene_benchmark_on_event(void* context, SceneManagerEvent event) {
  113. StorageSettings* app = context;
  114. bool consumed = false;
  115. if(event.type == SceneManagerEventTypeCustom) {
  116. switch(event.event) {
  117. case DialogExResultLeft:
  118. consumed = scene_manager_previous_scene(app->scene_manager);
  119. break;
  120. }
  121. }
  122. return consumed;
  123. }
  124. void storage_settings_scene_benchmark_on_exit(void* context) {
  125. StorageSettings* app = context;
  126. DialogEx* dialog_ex = app->dialog_ex;
  127. dialog_ex_set_header(dialog_ex, NULL, 0, 0, AlignCenter, AlignCenter);
  128. dialog_ex_set_text(dialog_ex, NULL, 0, 0, AlignCenter, AlignTop);
  129. dialog_ex_set_icon(dialog_ex, 0, 0, NULL);
  130. dialog_ex_set_left_button_text(dialog_ex, NULL);
  131. dialog_ex_set_right_button_text(dialog_ex, NULL);
  132. dialog_ex_set_result_callback(dialog_ex, NULL);
  133. dialog_ex_set_context(dialog_ex, NULL);
  134. string_clean(app->text_string);
  135. }