xref: /nrf52832-nimble/rt-thread/examples/file/readspeed.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
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 
15 #include <rtthread.h>
16 #include <dfs_posix.h>
17 
18 void readspeed(const char* filename, int block_size)
19 {
20     int fd;
21     char *buff_ptr;
22     rt_size_t total_length;
23     rt_tick_t tick;
24 
25     fd = open(filename, 0, O_RDONLY);
26     if (fd < 0)
27     {
28         rt_kprintf("open file:%s failed\n", filename);
29         return;
30     }
31 
32     buff_ptr = rt_malloc(block_size);
33     if (buff_ptr == RT_NULL)
34     {
35         rt_kprintf("no memory\n");
36         close(fd);
37 
38         return;
39     }
40 
41     tick = rt_tick_get();
42     total_length = 0;
43     while (1)
44     {
45         int length;
46         length = read(fd, buff_ptr, block_size);
47 
48         if (length <= 0) break;
49         total_length += length;
50     }
51     tick = rt_tick_get() - tick;
52 
53 	/* close file and release memory */
54     close(fd);
55 	rt_free(buff_ptr);
56 
57     /* calculate read speed */
58     rt_kprintf("File read speed: %d byte/s\n", total_length /tick * RT_TICK_PER_SECOND);
59 }
60 
61 #ifdef RT_USING_FINSH
62 #include <finsh.h>
63 FINSH_FUNCTION_EXPORT(readspeed, perform file read test);
64 #endif
65