writespeed.c 1.6 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * File      : writespeed.c
 * This file is part of RT-TestCase in RT-Thread RTOS
 * COPYRIGHT (C) 2010, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2010-02-10     Bernard      first version
 */
#include <rtthread.h>
#include <dfs_posix.h>

void writespeed(const char* filename, int total_length, int block_size)
{
B
bernard.xiong 已提交
19
    int fd, index, length;
B
bernard.xiong 已提交
20 21 22
    char *buff_ptr;
    rt_tick_t tick;

wuyangyong's avatar
wuyangyong 已提交
23
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
B
bernard.xiong 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    if (fd < 0)
    {
        rt_kprintf("open file:%s failed\n", filename);
        return;
    }

    buff_ptr = rt_malloc(block_size);
    if (buff_ptr == RT_NULL)
    {
        rt_kprintf("no memory\n");
        close(fd);

        return;
    }

	/* prepare write data */
B
bernard.xiong 已提交
40 41 42 43 44
	for (index = 0; index < block_size; index++)
	{
		buff_ptr[index] = index;
	}
	index = 0;
B
bernard.xiong 已提交
45

B
bernard.xiong 已提交
46
	/* get the beginning tick */
B
bernard.xiong 已提交
47
    tick = rt_tick_get();
B
bernard.xiong 已提交
48 49 50 51 52 53 54 55
	while (index < total_length / block_size)
	{
		length = write(fd, buff_ptr, block_size);
		if (length != block_size)
		{
			rt_kprintf("write failed\n");
			break;
		}
wuyangyong's avatar
wuyangyong 已提交
56

B
bernard.xiong 已提交
57 58
		index ++;
	}
B
bernard.xiong 已提交
59 60 61 62 63 64 65
    tick = rt_tick_get() - tick;

	/* close file and release memory */
    close(fd);
	rt_free(buff_ptr);

    /* calculate write speed */
wuyangyong's avatar
wuyangyong 已提交
66
    rt_kprintf("File write speed: %d byte/s\n", total_length*RT_TICK_PER_SECOND/tick);
B
bernard.xiong 已提交
67 68 69 70 71 72
}

#ifdef RT_USING_FINSH
#include <finsh.h>
FINSH_FUNCTION_EXPORT(writespeed, perform file write test);
#endif