sd卡测试我们分为裸设备速率测试和文件系统下读写测试。
测试思路:将一个100MB的文件写入sd卡或者从sd卡读出100MB的文件。得到写入和读取的速率。
裸设备测试
实现:采用dd命令 ,输入文件为/dev/zero,生成100MB的全0文件写入sd卡 /dev/mmcblk0或者/dev/mmcblk0p1,或者从sd卡读取50MB的文件。使用time命令得到平均时间。
代码(shell):
while true
do
echo "writing......"
time dd if=/dev/zero of=/dev/mmcblk0p1 bs=1024K count=100
echo "reading......"
time dd if=/dev/mmcblk0p1 of=/dev/zero bs=1024K count=100
done
文件读写速率测试
上面的方法只能得到平均速率,但是没法知道每一秒的写速率,因为sd卡的写速率可能事不均匀的,所以我们需要知道每一秒的写速率。
思路:创建一个新文件,起一个线程写文件,另一个线程读取文件的大小信息,比较两秒之间的文件差,得到每秒的写速率。
代码©:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#define TEST_NODE "/mnt/sda/test_node"
#define TEST_BUF_SIZE (1024*64)
char test_buf[TEST_BUF_SIZE] = {0};
void* write_thread(void* arg);
unsigned long get_file_size(const char *path)
{
unsigned long filesize = -1;
struct stat statbuff;
if(stat(path, &statbuff) < 0){
return filesize;
}else{
filesize = statbuff.st_size;
}
return filesize;
}
int main()
{
int ret = 0;
pthread_t wr_tid = 0;
int times = 0;
int file_size_last = 0;
int file_size = 0;
for (int i = 0; i < TEST_BUF_SIZE; i++)
{
test_buf[i] = TEST_BUF_SIZE - i;
}
int fd = open(TEST_NODE, O_WRONLY|O_CREAT);
if (fd < 0)
{
printf("open file failed errno(%d)", errno);
return -1;
}
ret = pthread_create(&wr_tid, NULL, write_thread, &fd);
if (ret != 0)
{
printf("create thread failed!\n");
goto exit;
}
while(1)
{
sleep(1);
file_size = get_file_size(TEST_NODE);
if (file_size < 0)
{
printf("get file size failed!!!\n");
break;
}
printf("file_size: now: %d, last:%d times:%d speed %d KB/s\n", file_size, file_size_last, times, ((file_size - file_size_last) / 1024));
fflush(stdout);
file_size_last = file_size;
times++;
}
pthread_join(wr_tid, NULL);
exit:
close(fd);
return ret;
}
void* write_thread(void* arg)
{
int fd = * (int*)arg;
int ret = 0;
while(1)
{
ret = write(fd, test_buf, sizeof(test_buf));
if (ret != sizeof(test_buf))
{
printf("write failed ret: %d, expect:%d\n", ret, sizeof(test_buf));
break;
}
}
}