随笔misc:sd卡速率测试用例

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;
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值