readspeed.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <rtthread.h>
  2. #include <dfs_posix.h>
  3. void readspeed(const char* filename, int block_size)
  4. {
  5. int fd;
  6. char *buff_ptr;
  7. rt_size_t total_length;
  8. rt_tick_t tick;
  9. fd = open(filename, 0, DFS_O_RDONLY);
  10. if (fd < 0)
  11. {
  12. rt_kprintf("open file:%s failed\n", filename);
  13. return;
  14. }
  15. buff_ptr = rt_malloc(block_size);
  16. if (buff_ptr == RT_NULL)
  17. {
  18. rt_kprintf("no memory\n");
  19. close(fd);
  20. return;
  21. }
  22. tick = rt_tick_get();
  23. total_length = 0;
  24. while (1)
  25. {
  26. int length;
  27. length = read(fd, buff_ptr, block_size);
  28. if (length == 0) break;
  29. total_length += length;
  30. }
  31. tick = rt_tick_get() - tick;
  32. /* close file and release memory */
  33. close(fd);
  34. rt_free(buff_ptr);
  35. /* calculate read speed */
  36. rt_kprintf("File read speed: %d byte/s\n", total_length/ (tick/RT_TICK_PER_SECOND));
  37. }
  38. #ifdef RT_USING_FINSH
  39. #include <finsh.h>
  40. FINSH_FUNCTION_EXPORT(readspeed, perform file read test);
  41. #endif