writespeed.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * File : writespeed.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 writespeed(const char* filename, int total_length, int block_size)
  17. {
  18. int fd, index, length;
  19. char *buff_ptr;
  20. rt_tick_t tick;
  21. fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
  22. if (fd < 0)
  23. {
  24. rt_kprintf("open file:%s failed\n", filename);
  25. return;
  26. }
  27. buff_ptr = rt_malloc(block_size);
  28. if (buff_ptr == RT_NULL)
  29. {
  30. rt_kprintf("no memory\n");
  31. close(fd);
  32. return;
  33. }
  34. /* prepare write data */
  35. for (index = 0; index < block_size; index++)
  36. {
  37. buff_ptr[index] = index;
  38. }
  39. index = 0;
  40. /* get the beginning tick */
  41. tick = rt_tick_get();
  42. while (index < total_length / block_size)
  43. {
  44. length = write(fd, buff_ptr, block_size);
  45. if (length != block_size)
  46. {
  47. rt_kprintf("write failed\n");
  48. break;
  49. }
  50. index ++;
  51. }
  52. tick = rt_tick_get() - tick;
  53. /* close file and release memory */
  54. close(fd);
  55. rt_free(buff_ptr);
  56. /* calculate write speed */
  57. rt_kprintf("File write speed: %d byte/s\n", total_length / tick * RT_TICK_PER_SECOND);
  58. }
  59. #ifdef RT_USING_FINSH
  60. #include <finsh.h>
  61. FINSH_FUNCTION_EXPORT(writespeed, perform file write test);
  62. #endif