readspeed.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * File : readspeed.c
  3. * This file is part of RT-TestCase in RT-Thread RTOS
  4. * COPYRIGHT (C) 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-02-10 Bernard first version
  13. */
  14. #include <rtthread.h>
  15. #include <dfs_posix.h>
  16. void readspeed(const char* filename, int block_size)
  17. {
  18. int fd;
  19. char *buff_ptr;
  20. rt_size_t total_length;
  21. rt_tick_t tick;
  22. fd = open(filename, 0, O_RDONLY);
  23. if (fd < 0)
  24. {
  25. rt_kprintf("open file:%s failed\n", filename);
  26. return;
  27. }
  28. buff_ptr = rt_malloc(block_size);
  29. if (buff_ptr == RT_NULL)
  30. {
  31. rt_kprintf("no memory\n");
  32. close(fd);
  33. return;
  34. }
  35. tick = rt_tick_get();
  36. total_length = 0;
  37. while (1)
  38. {
  39. int length;
  40. length = read(fd, buff_ptr, block_size);
  41. if (length <= 0) break;
  42. total_length += length;
  43. }
  44. tick = rt_tick_get() - tick;
  45. /* close file and release memory */
  46. close(fd);
  47. rt_free(buff_ptr);
  48. /* calculate read speed */
  49. rt_kprintf("File read speed: %d byte/s\n", total_length /tick * RT_TICK_PER_SECOND);
  50. }
  51. #ifdef RT_USING_FINSH
  52. #include <finsh.h>
  53. FINSH_FUNCTION_EXPORT(readspeed, perform file read test);
  54. #endif