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