⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bench.cpp

📁 一个硬件测试工具的源代码
💻 CPP
字号:
#include <afxwin.h>
#include "bench.h"
#include <float.h>
#include <sys/timeb.h>


double intMess;
HANDLE hThread;
bool nonstop=false;


	

long WINAPI DoInt(long wiederhole)
{	
	long dauertm;
	long i,j,x,y,z;
	struct _timeb start,ende;
	intMess=0;
	i=1;x=5;y=10;z=-5;
	while (nonstop)
	{	
		j=0;
		_ftime(&start);
		while (j<1000000)
		{
			x=y*z;y=(z/y)+1;z=z-2*z;x=x-z+y;
		++j;
		}
		_ftime(&ende); dauertm=(ende.time-start.time)*1000+(ende.millitm-start.millitm);
		intMess=(intMess*(i-1)+dauertm)/i;
	++i;
	}
	return (NO_ERROR);
}

long CBenchApp::DoMem(long wiederhole, int numthreads)
{	
	const long kilo=1024;
	//numthreads=numthreads/4;
	//if (numthreads==0) {numthreads=1;}
	const long bis=32768+1;//(numthreads)+1;
	unsigned long j=0;
	int i=0;
	while (i<wiederhole)
	{
		long k=4096;
		while (k<bis)
		{	
			const unsigned long sizey=k*kilo;
			char *x=new char[sizey];
			j=0;
			x[j]='a';
			while (j<sizey)
			{
				//x[sizey-j]=x[j];
				x[j]=char(j);
				//x[j+2]=x[j+1];
			++j;
			}
			delete [] x;
		k=k*2;
		}
	++i;
	}
	return 1;
}



long CBenchApp::DoDisk(char drive,long wiederhole,long &lmax,long &lmittel,long &lmin,long &smax, long &smittel,long &smin,int &prozent)
{
	long dauer2tm;
	const int kilo=1024;
	const int block=256;
	const unsigned long groesse=block*kilo;
	const unsigned long mal=5*kilo*kilo/groesse;
	//char *test=new char[groesse];
	char test[groesse];
	struct _timeb start,ende;
	int ok=0;
	long k=0;
	long i=0;
	long lesedauertm=0;
	long schreibdauertm=0;
	long lesemaxtm=-LONG_MAX;
	long schreibmaxtm=-LONG_MAX;
	long lesemintm=LONG_MAX;
	long schreibmintm=LONG_MAX;
	double justThread;
	DWORD dwRead;


	//void *test=VirtualAlloc(NULL,groesse,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
	Sleep(3000);

	// starte referenzthread ////
	nonstop=true;
		
	unsigned long nThreadID; long threadNo=100;
	hThread=CreateThread(NULL,
						0,
						(LPTHREAD_START_ROUTINE)DoInt,
						(void*)threadNo,
						0,
						&nThreadID);
	SetThreadPriority(hThread,THREAD_PRIORITY_LOWEST);
	
	Sleep(6000);
	nonstop=false;
	Sleep(2000);

	justThread=intMess;

	// generiere test string /////////////////////////////////////
	
	k=0;
	while (k<groesse)
	{
		test[k]=char(k);
	++k;
	}
	
	TCHAR szFile[20];
	sprintf(szFile,"%c:DISKTEST.DAT",drive);

					
	// wiederhole wiederhole mal die Tests ///////////////////////
	prozent=0;
	DWORD dwResult=NO_ERROR;
	k=0;

	// starte Thread f黵 die CPU Belastung ////////////////////////////
	nonstop=true;
	threadNo=100;
	hThread=CreateThread(NULL,
							0,
							(LPTHREAD_START_ROUTINE)DoInt,
							(void*)threadNo,
							0,
							&nThreadID);
	SetThreadPriority(hThread,THREAD_PRIORITY_LOWEST);
	

	while (k<wiederhole)
	{	
		
		// Das TestFile wird ge鰂fnet ///////////////////////////////
	
		HANDLE hTestFile=CreateFile(szFile,
									GENERIC_READ|GENERIC_WRITE,
									NULL,
									NULL,
									CREATE_ALWAYS,
									FILE_FLAG_NO_BUFFERING,
									NULL);
	
		if(hTestFile==INVALID_HANDLE_VALUE)
		{
			MessageBox(	NULL,
						TEXT("Can't open test file"),
						TEXT("Error opening test file. Make sure you've got admin rights!"),
						MB_ICONHAND);
			delete [] test;
			nonstop=false;
			return -1;
		}
		// schreibe //
		_ftime(&start);

		i=0;
		_ftime(&start);
		while (i<mal)
		{	
			ok=WriteFile(hTestFile, 
						test,
						groesse,
						&dwRead,
						NULL);
			++i;
		}
		if(dwRead==0)
		{
			MessageBox(	NULL,
						TEXT("Can't write test file"),
						TEXT("Error writing to test file"),
						MB_ICONHAND);
			// delete [] test; VirtualAlloc Version
			nonstop=false;
			return -1;
		}
		while (dwRead<(groesse));
		_ftime(&ende); dauer2tm=(ende.time-start.time)*1000+(ende.millitm-start.millitm);
		schreibdauertm=schreibdauertm+dauer2tm;
		if (dauer2tm<schreibmintm) schreibmintm=dauer2tm;
		if (dauer2tm>schreibmaxtm) schreibmaxtm=dauer2tm;
		
		// zeiger auf Dateianfang //
		DWORD dwPos=SetFilePointer(	hTestFile,
									NULL,
									NULL,
									FILE_BEGIN);
		// lese //
		i=0;
		
		_ftime(&start);
		while (i<mal)
		{	
			ok=ReadFile(hTestFile, 
						test,
						groesse,
						&dwRead,
						NULL);
			++i;
		}
		if(dwRead==0)
		{
			MessageBox(	NULL,
						TEXT("Can't read test file"),
						TEXT("Error reading to test file"),
						MB_ICONHAND);
			// delete [] test; VirtualAlloc Version
			nonstop=false;
			return -1;
		}
		while (dwRead<(groesse));
		_ftime(&ende); dauer2tm=(ende.time-start.time)*1000+(ende.millitm-start.millitm);
		lesedauertm=lesedauertm+dauer2tm;
		if (dauer2tm<lesemintm) lesemintm=dauer2tm;
		if (dauer2tm>lesemaxtm) lesemaxtm=dauer2tm;
	++k;
	CloseHandle(hTestFile);
	DeleteFile(szFile);
	delete [] test;
	}
	nonstop=false;
	Sleep(2000);
	// VirtualFree(test,0,MEM_RELEASE); VirtualAlloc Version
	//delete [] test;
	prozent=int(100-(100*(justThread/intMess)));
	lmax=(1000*block*mal)/lesemintm; 
	lmittel=(1000*block*k*mal)/lesedauertm;
	lmin=(1000*block*mal)/lesemaxtm;
	smax=(1000*block*mal)/schreibmintm;
	smittel=(1000*block*k*mal)/schreibdauertm;
	smin=(1000*block*mal)/schreibmaxtm;
	return 0;
}




⌨️ 快捷键说明

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