2010-02-10 13:03:36 +08:00
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2010-02-11 23:59:25 +08:00
|
|
|
int fd, index, length;
|
2010-02-10 13:03:36 +08:00
|
|
|
char *buff_ptr;
|
|
|
|
rt_tick_t tick;
|
|
|
|
|
2011-01-03 00:39:23 +08:00
|
|
|
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
|
2010-02-10 13:03:36 +08:00
|
|
|
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 */
|
2010-02-11 23:59:25 +08:00
|
|
|
for (index = 0; index < block_size; index++)
|
|
|
|
{
|
|
|
|
buff_ptr[index] = index;
|
|
|
|
}
|
|
|
|
index = 0;
|
2010-02-10 13:03:36 +08:00
|
|
|
|
2010-02-11 23:59:25 +08:00
|
|
|
/* get the beginning tick */
|
2010-02-10 13:03:36 +08:00
|
|
|
tick = rt_tick_get();
|
2010-02-11 23:59:25 +08:00
|
|
|
while (index < total_length / block_size)
|
|
|
|
{
|
|
|
|
length = write(fd, buff_ptr, block_size);
|
|
|
|
if (length != block_size)
|
|
|
|
{
|
|
|
|
rt_kprintf("write failed\n");
|
|
|
|
break;
|
|
|
|
}
|
2011-01-03 00:39:23 +08:00
|
|
|
|
2010-02-11 23:59:25 +08:00
|
|
|
index ++;
|
|
|
|
}
|
2010-02-10 13:03:36 +08:00
|
|
|
tick = rt_tick_get() - tick;
|
|
|
|
|
|
|
|
/* close file and release memory */
|
|
|
|
close(fd);
|
|
|
|
rt_free(buff_ptr);
|
|
|
|
|
|
|
|
/* calculate write speed */
|
2012-01-11 19:19:41 +08:00
|
|
|
rt_kprintf("File write speed: %d byte/s\n", total_length / tick * RT_TICK_PER_SECOND);
|
2010-02-10 13:03:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RT_USING_FINSH
|
|
|
|
#include <finsh.h>
|
|
|
|
FINSH_FUNCTION_EXPORT(writespeed, perform file write test);
|
|
|
|
#endif
|