chddspeed.c

来自「Console HDD performance test for windows」· C语言 代码 · 共 158 行

C
158
字号
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "util.h"

int test_hdd(const char*name,unsigned flags);


int min_test_time=500000;
int max_test_time=2000000;


int test_multisect(int handle,struct HDD_INFO*info,int nreps,int maxsects,int nresults,double*result_speeds)
{
	void*buf;
	int rep_no;
	int cur_res;
	int start_sect=0;
	buf=alloc_hdd_buffer(info,maxsects);
	if (!buf) return -1;
	printf("%15s%15s%15s%15s%15s\n","N Blocks","N Reps","Time,micros","Speed,MB/s","CPU Load,%");
	for (cur_res=0;cur_res<nresults;cur_res++) {
		int n_sects=cur_res*(maxsects-1)/(nresults-1)+1;
		int nb, nrb;
		long_long t1, t2, dt;
		long_long perft[2][2];
		double sp, sload;
	rept:
		nrb=0;
		seek_hdd(handle,info,start_sect);
		t1=get_system_time();
		get_execution_times(perft[0],perft[0]+1);
		rep_no=nreps;
		if (rep_no*n_sects>info->n_blocks) rep_no=info->n_blocks/n_sects;
		for (;rep_no;rep_no--)
 {			nrb+=read_hdd_cur(handle,info,n_sects,buf);
			memset(buf,0,n_sects*info->block_size);		}		get_execution_times(perft[1],perft[1]+1);
		t2=get_system_time();
		dt=t2-t1;
		if (dt<min_test_time) {
			nreps<<=1;
			goto rept;
		}
		if (!dt) dt=1;
		nb=info->block_size*nrb;
		sp=(double)nb/(dt/1e6)/1024.0/1024.0;
		sload=(double)((perft[1][0]-perft[0][0])+(perft[1][1]-perft[0][1]))*100.0/dt;
		printf("%15i%15i%15i%15f%15.2f%%\n",n_sects,nreps,(int)dt,sp,sload);
		if (dt>max_test_time) nreps>>=1;
	}
	free_hdd_buffer(buf,info,maxsects);
	return 0;
}


int test_randomread_range(int handle,struct HDD_INFO*info,int nreps,long_long range,char*str)
{
	void*buf;
	int rep_no;
	long_long t1,t2,dt;
	long_long perft[2][2];
	int nb, nrb=0;
	long_long lblk=-1;
	double sp, sload;
	buf=alloc_hdd_buffer(info,1);
	if (!buf) return -1;
	t1=get_system_time();
	get_execution_times(perft[0],perft[0]+1);
	for (rep_no=nreps;rep_no;rep_no--) {
		long_long blk;
		do blk=rand()*range/RAND_MAX; while (blk==lblk);
		lblk=blk;
		nrb+=read_hdd(handle,info,blk,1,buf);
		memset(buf,0,info->block_size);	}
	t2=get_system_time();
	get_execution_times(perft[1],perft[1]+1);
	dt=t2-t1;
	nb=nrb*info->block_size;
	sp=nb/(dt/1e6)/1024.0/1024.0;
	sload=(double)((perft[1][0]-perft[0][0])+(perft[1][1]-perft[0][1]))*100.0/dt;
	free_hdd_buffer(buf,info,1);
	sprintf(str,"%15i%15i%15i%15f%15.2f",(int)range,nreps,(int)dt,sp,sload);
	return dt;
}


int test_randomread(int handle,struct HDD_INFO*info,int nreps)
{
	long_long range=info->n_blocks;
	char str[256];
	printf("%15s%15s%15s%15s%15s\n","Random range","N reps","Time,micros","Speed,MB/s","CPU Load,%");
	while (range>1) {
		int t;
	rept:
		t=test_randomread_range(handle,info,nreps,range,str);
		if (t<min_test_time) {
			nreps<<=1;
			goto rept;
		}
		puts(str);
		range>>=1;
		if (t>max_test_time) nreps>>=1;
	}
	return 0;
}


int test_hdd(const char*name,unsigned flags)
{
	struct HDD_INFO info;
	int handle;
	double result_speeds[20];
	handle=open_hdd(name);
	if (handle<0) {
		fprintf(stderr,"can't open drive %s: %s\n",name,hdd_error_msg());
		return -1;
	}
	if (get_hdd_info(handle,&info)<0) {
		fprintf(stderr,"can't get drive geometry for drive %s: %s\n",name,hdd_error_msg());
		close_hdd(handle);
		return -1;
	}
	printf("drive %s information: block_size=%i, n_blocks=%i\n",name,info.block_size,(int)info.n_blocks);
	puts("Multisector read test\n---------------------------");
	test_multisect(handle,&info,1000,200,20,result_speeds);
	puts("Random read test\n---------------------------");
	test_randomread(handle,&info,100);
	close_hdd(handle);
	return 0;
}


int main(int argc,const char*argv[])
{
	setbuf(stdout,NULL);
	puts("Block device benchmark test suite version 0.2. Copyright (c) Nop, 2002-2003\nMail questions to nnop@newmail.ru\n");
	if (hdd_prepare()<0) {
		fprintf(stderr,"initialization error\n");
		return 3;
	}
	if (argc<2) {
		return test_hdd(def_device,0);
	} else {
		int i;
		for (i=1;i<argc;i++) {
			test_hdd(argv[i],0);
		}
	}	
	return 0;
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?