测试硬盘读写速度的小程序

有时候,我们需要测试硬盘的写速度,来更好的设计软件。

Unix/Linux系统有需要有意思的命令可以来完成这个时间,比如dd命令等;

而windows平台,则可能需要copy一个大文件来手动观察具体的时间。

为了统一和更精确的测试时间,试试下面的小demo。

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <time.h>
#include <sys/timeb.h>
#include <sys/times.h>
#include <fcntl.h>

#if defined(WIN32) || defined(WIN64)
	#include <process.h>
	#include  <io.h>
	#include <windows.h>

#else
	#include <unistd.h>
	#include <pthread.h>
#endif

#define	BUFSIZE	8192

char	buf[8192+1]="";

time_t t_start, t_end;
struct tm *st_start;
struct tm *st_end;
struct timeb mtp_start;
struct timeb mtp_end;

int main(int argc,char **argv)
{
	int	fp,num,i=0;
	int	res;
	int	bufsize,filesize;
	char	tmpbuf[32+1]="";
	char	filename[256]="";

	if(argc != 3){
		printf("usage:	./a.out	filename filesize(Mb)\n");
		exit(0);
	}
	printf("you input args:	filename=[%s],filesize=[%d]\n",argv[1],atoi(argv[2]));

	strcpy(filename,argv[1]);
	filesize = atoi(argv[2]);
	if(filesize*1024*1024%BUFSIZE == 0)
		num = filesize*1024*1024/BUFSIZE;
	else
		num = filesize*1024*1024/BUFSIZE+1;

	strncpy(tmpbuf,"testdsfsdadadasdkjaksldjaljdlaks",32);
	for(i=0;i<256;i++){
		strcat(buf,tmpbuf);
	}
	i = 0;

	fp = open(argv[1],O_WRONLY|O_CREAT|O_EXCL|O_APPEND,0666);
	if(fp < 0){
		printf("open file failed!\n");
		exit(-1);
	}

	/* Write Start */
	ftime(&mtp_start);
	t_start = mtp_start.time;
	st_start = localtime(&t_start);
	printf("start time: %04d-%02d-%02d %02d:%02d:%02d:%03d\n", (st_start->tm_year+1900), (st_start->tm_mon+1), st_start->tm_mday, st_start->tm_hour, st_start->tm_min, st_start->tm_sec, mtp_start.millitm);

	while(i++<num){
		if(write(fp,buf,BUFSIZE) != BUFSIZE)
			break;
	}

	/* Write End */
	ftime(&mtp_end);
	t_end = mtp_end.time;
	st_end = localtime(&t_end);

	printf("end   time: %04d-%02d-%02d %02d:%02d:%02d:%03d\n", (st_end->tm_year+1900), (st_end->tm_mon+1), st_end->tm_mday, st_end->tm_hour, st_end->tm_min, st_end->tm_sec, mtp_end.millitm );

	if((mtp_end.millitm - mtp_start.millitm) >=0 )
		printf("second used: %d.%03d\n", (int)(t_end - t_start), (int)(mtp_end.millitm - mtp_start.millitm) );
	else
		printf("second used: %d.%03d\n", (int)(t_end - t_start - 1), (int)(1000 + mtp_end.millitm - mtp_start.millitm) );

	close(fp);

	exit(0);
}

为了更精确的统计时间,我们还可以使用times函数。(Unix/Linux平台,windows平台应该也有对应的api)

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <time.h>
#include <sys/timeb.h>
#include <sys/times.h>
#include <fcntl.h>

#if defined(WIN32) || defined(WIN64)
	#include <process.h>
	#include  <io.h>
	#include <windows.h>

#else
	#include <unistd.h>
	#include <pthread.h>
#endif

#define	BUFSIZE	8192

char	buf[8192+1]="";

static void pr_times(clock_t real,struct tms *tmsstart,struct tms *tmsend);

int main(int argc,char **argv)
{
	int	fp,num,i=0;
	int	res;
	int	bufsize,filesize;
	char	tmpbuf[32+1]="";
	char	filename[256]="";
	struct  tms	tmsstart,tmsend;
	clock_t	start,end;

	if(argc != 3){
		printf("usage:	./a.out	filename filesize(Mb)\n");
		exit(0);
	}
	printf("you input args:	filename=[%s],filesize=[%d]\n",argv[1],atoi(argv[2]));

	strcpy(filename,argv[1]);
	filesize = atoi(argv[2]);
	if(filesize*1024*1024%BUFSIZE == 0)
		num = filesize*1024*1024/BUFSIZE;
	else
		num = filesize*1024*1024/BUFSIZE+1;

	strncpy(tmpbuf,"testdsfsdadadasdkjaksldjaljdlaks",32);
	for(i=0;i<256;i++){
		strcat(buf,tmpbuf);
	}
	i = 0;

	fp = open(argv[1],O_WRONLY|O_CREAT|O_EXCL|O_APPEND,0666);
	if(fp < 0){
		printf("open file failed!\n");
		exit(-1);
	}

	/* Write Start */
	if((start = times(&tmsstart)) == -1){
		printf("starting time failed!\n");
		exit(-1);
	}

	while(i++<num){
		if(write(fp,buf,BUFSIZE) != BUFSIZE)
			break;
	}

	/* Write End */
	if((end = times(&tmsend)) == -1){
		printf("ending time failed!\n");
		exit(-1);
	}
	pr_times(end-start,&tmsstart,&tmsend);

	close(fp);

	exit(0);
}

static void pr_times(clock_t real,struct tms *tmsstart,struct tms *tmsend)
{
	static	long	clktck = 0;

	if(clktck == 0){
		if((clktck = sysconf(_SC_CLK_TCK)) < 0){
			printf("sysconf error!\n");
			exit(-1);
		}
	}
	printf("	real:	%7.2f\n",real/(double)clktck);
	printf("	user:	%7.2f\n",(tmsend->tms_utime - tmsstart->tms_utime)/(double)clktck);
	printf("	sys:	%7.2f\n",(tmsend->tms_stime - tmsstart->tms_stime)/(double)clktck);
}

ps:此程序也可以去扩展,比如测试内存的读写速度等。

小技巧:使用time命令来实现时间统计。如下:

time ./a.out

可以统计a.out的执行时间。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值