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

📄 tests.cpp

📁 mx27 f14v2 源代码。包括ADS板上诸多驱动的源码。
💻 CPP
字号:
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//------------------------------------------------------------------------------
//
//  Copyright (C) 2004-2006, Freescale Semiconductor, Inc. All Rights Reserved.
//  THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
//  AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//------------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//
//  File:  tests.cpp
//
//  This file contains test code for the File Read/Write Performance
//
//------------------------------------------------------------------------------

#include <windows.h>
#include <Devload.h>
#include <ceddk.h>
#include "csp.h"
#include "main.h"
#include "globals.h"


//-----------------------------------------------------------------------------
// External Functions
extern void Debug(LPCTSTR szFormat, ...);


//-----------------------------------------------------------------------------
// External Variables
static int g_nSize;
static TCHAR g_szDirectory[MAX_PATH];

//-----------------------------------------------------------------------------
// Defines
#define FILEPERF_ZONE_INFO        0
#define FILEPERF_ZONE_ERROR       0
#define FILEPERF_ZONE_FUNCTION    0

#define FILEPERF_TEST_FUNCTION_ENTRY() \
    g_pKato->Log(FILEPERF_ZONE_FUNCTION, (TEXT("++%s\r\n"), __WFUNCTION__))
#define FILEPERF_TEST_FUNCTION_EXIT() \
    g_pKato->Log(FILEPERF_ZONE_FUNCTION, (TEXT("--%s\r\n"), __WFUNCTION__))

#define TESTING_FILENAME _T("FPerfTest.tmp")


//-----------------------------------------------------------------------------
// Types


//-----------------------------------------------------------------------------
// Global Variables
#ifdef DEBUG
DBGPARAM dpCurSettings =
{
    _T("FilePerfTEST"),
    {
        _T(""), _T(""), _T(""), _T(""),
        _T(""), _T(""), _T(""), _T(""),
        _T(""),_T(""),_T(""),_T(""),
        _T("Info"),_T("Function"),_T("Warnings"),_T("Errors")
    },
    0x0
};
#endif


//-----------------------------------------------------------------------------
// Local Variables
static TCHAR g_szFilespec[MAX_PATH];
static TCHAR g_szFilespec2[MAX_PATH];

static BYTE * g_pBufferAll = NULL; // buffers for R/w and pattern
static BYTE * g_pBuffer; // read/write buffer
static BYTE *g_pPattern; // pattern to compare
static int g_nBufferSize; // read, write or pattern buffer size


//-----------------------------------------------------------------------------
// Local Functions

typedef __int64 timer_type;
timer_type finish_count, start_count;
timer_type resolution = 0;

#define INIT_TIMER \
	{ if (resolution == 0) QueryPerformanceFrequency( reinterpret_cast<LARGE_INTEGER*>(&resolution) ); }
#define START_TIMER \
	{ QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER*>( &start_count ) ); }
#define STOP_TIMER \
	{ QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER*>( &finish_count ) ); }
//	RETAILMSG(1,(_T("start=%ld, finish=%ld, resolution=%ld\r\n"),start_count,finish_count,resolution));
#define LOG_TIMER \
	{ DWORD msec = (DWORD)((finish_count-start_count)*1000/resolution); \
        g_pKato->Log(FILEPERF_ZONE_FUNCTION, \
            (TEXT("\ttotal %dms (%d bytes/sec)\r\n")), \
                msec, \
            msec == 0 ? 0 : ((g_nSize*1000) / msec) \
            ); }


static UINT fnDeleteIfExisting(void)
{
	g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("1.Delete existing test file\r\n")));

	if (0 != DeleteFile(g_szFilespec)) //Nonzero indicates success
		g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tdeleted, success\r\n")));
	else
		g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\ttest file not exist, it's ok\r\n")));

	return TPR_PASS;
}


static UINT fnOpenCloseNewEmpty(void)
{
	g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("2.Open and close new (empty) test file \"%s\"\r\n")),g_szFilespec);

	START_TIMER

    HANDLE hFile = INVALID_HANDLE_VALUE;
	DWORD lastErr;
	UINT retVal = TPR_FAIL;

    hFile = CreateFile(g_szFilespec, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL, /*CREATE_NEW*/CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    lastErr = GetLastError();
    if(INVALID_HANDLE_VALUE == hFile)
	{
        if(ERROR_FILE_NOT_FOUND == lastErr)
        {
            // file deleted before we could open it
            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tfailed: unable to CreateFile \"%s\" because file no longer exists")), g_szFilespec);
        }
        else if(ERROR_SHARING_VIOLATION == lastErr)
        {
            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tfailed: unable to CreateFile \"%s\" due to sharing violation")), g_szFilespec);
        }
        else
        {
            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tfailed: CreateFile(\"%s\") failed; system error %u")), g_szFilespec, lastErr);
        }
    }
	else
	{
		CloseHandle(hFile);
        retVal = TPR_PASS;
	}

	STOP_TIMER
	LOG_TIMER

	if (TPR_PASS == retVal)
		g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tsuccess")) );

	return retVal;
}


static void FillPattern(DWORD* pBuffer,int nSize)
{
	DWORD* p = pBuffer;
	for (int i=0; i<nSize/4; i++) //pattern
		*p++ = i;
}


static UINT fnWriteData(void)
{
	g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("3.Write data pattern generated by utilility in RAM \"%s\"\r\n")),g_szFilespec);

	FillPattern((DWORD*)g_pBuffer,g_nBufferSize);

    BYTE *pBuffer = g_pBuffer;
   	DWORD cbBuffer = g_nBufferSize, cbWritten, cbWrite;

	START_TIMER

    HANDLE hFile = INVALID_HANDLE_VALUE;
	DWORD lastErr;
	UINT retVal = TPR_FAIL;

    hFile = CreateFile(g_szFilespec, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
    lastErr = GetLastError();
    if(INVALID_HANDLE_VALUE == hFile)
	{
        if(ERROR_FILE_NOT_FOUND == lastErr)
        {
            // file deleted before we could open it
            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tunable to CreateFile \"%s\" because file no longer exists")), g_szFilespec);
        }
        else if(ERROR_SHARING_VIOLATION == lastErr)
        {
            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tunable to CreateFile \"%s\" due to sharing violation")), g_szFilespec);
        }
        else
        {
            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tCreateFile(\"%s\") failed; system error %u")), g_szFilespec, lastErr);
        }
    }
	else
	{
		int cbLeft = g_nSize;
        retVal = TPR_PASS;
		while (cbLeft>=0)
		{
			cbWrite = cbLeft>=g_nBufferSize ? g_nBufferSize : cbLeft; // MIN
    		if(!WriteFile(hFile, pBuffer, cbWrite, &cbWritten, NULL) && cbWritten==cbWrite)
    		{
		        retVal = TPR_FAIL;
		        break;
    		}
    		cbLeft -= g_nBufferSize;
    	}

    	CloseHandle(hFile);
	}

	STOP_TIMER
	LOG_TIMER

	if (TPR_PASS == retVal)
		g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tsuccess")) );

	return retVal;
}


static UINT fnRenameFile(void) // ?
{
	g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("4.Rename file \"%s\" to \"%s\"\r\n")),g_szFilespec, g_szFilespec2);

	if (!CopyFile(g_szFilespec, g_szFilespec2, FALSE))
	{
		g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tFailed: CopyFile\r\n")));
		return TPR_FAIL;
	}
	if (0 == DeleteFile(g_szFilespec))
	{
		g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tFailed: DeleteFile \"%s\"\r\n")), g_szFilespec);
		return TPR_FAIL;
	}
	g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\trenamed\r\n")));

	return TPR_PASS;
}


static UINT fnReadData(void)
{
	g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("5.Read data pattern and verify against original \"%s\"\r\n")),g_szFilespec2);

    BYTE *pBuffer = g_pBuffer;
   	DWORD cbBuffer = g_nBufferSize;

	FillPattern((DWORD*)g_pPattern,g_nBufferSize);
	BYTE* pPattern = g_pPattern;

	START_TIMER

    HANDLE hFile = INVALID_HANDLE_VALUE;
	DWORD lastErr;
	UINT retVal = TPR_FAIL;

    hFile = CreateFile(g_szFilespec2, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
    lastErr = GetLastError();
    if(INVALID_HANDLE_VALUE == hFile)
	{
        if(ERROR_FILE_NOT_FOUND == lastErr)
        {
            // file deleted before we could open it
            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tunable to CreateFile \"%s\" because file no longer exists")), g_szFilespec2);
        }
        else if(ERROR_SHARING_VIOLATION == lastErr)
        {
            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tunable to CreateFile \"%s\" due to sharing violation")), g_szFilespec2);
        }
        else
        {
            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tCreateFile(\"%s\") failed; system error %u")), g_szFilespec2, lastErr);
        }
    }
	else
	{
		// get the file size
        DWORD fileSizeHigh;
        int fileSizeLow = GetFileSize(hFile, &fileSizeHigh);
        lastErr = GetLastError();
        if(0xFFFFFFFF == fileSizeLow && ERROR_SUCCESS != lastErr) {
            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("GetFileSize(\"%s\") failed; system error %u")), g_szFilespec2, lastErr);
        }
        else if (fileSizeLow != g_nSize)
		{
	     	g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tFailed: wrong file size: %d")), fileSizeLow);
   		}
		else
		{
			retVal = TPR_PASS;

            // g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("fileSizeLow=%d,fileSizeHigh=%d")), fileSizeLow, fileSizeHigh);

			DWORD nNumberOfBytesToRead = g_nBufferSize, nNumberOfBytesRead;
			int cbRead = 0;
			while (ReadFile(hFile, pBuffer, nNumberOfBytesToRead, &nNumberOfBytesRead, NULL) && nNumberOfBytesRead>0)
    		{
	            //g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("%d")), nNumberOfBytesRead);
    			if (0 != memcmp(pBuffer,pPattern,nNumberOfBytesRead))
    			{
		            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tFailed: compare at position %d")), cbRead);
					retVal = TPR_FAIL;
		            break;
	    		}
	    		cbRead += nNumberOfBytesRead;
    		}
    		if (cbRead != g_nSize)
   			{
	            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tFailed: read error at %d")), cbRead);
   			}
		}

    	CloseHandle(hFile);
	}

	STOP_TIMER
	LOG_TIMER

	if (TPR_PASS == retVal)
		g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tsuccess")) );

	return retVal;
}


static UINT fnDeleteFile(void)
{
	UINT retVal = TPR_PASS;

	g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("6.Delete file \"%s\"\r\n")),g_szFilespec2);

	START_TIMER

	if (0 == DeleteFile(g_szFilespec2))
	{
		g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tFailed: can't delete '%s'\r\n"),
                    g_szFilespec2));

		retVal = TPR_FAIL;
	}
	
	STOP_TIMER
	LOG_TIMER

	if (TPR_PASS == retVal)
		g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tdeleted, success\r\n")));

	return retVal;
}


// Preapare 2 filespecs and 2 buffers
static BOOL Initialization(void)
{
	// 1.Prepare filespaces
	if (0 == GetTempFileName(g_szDirectory,_T("FPERF"),0,g_szFilespec2))
	{
		g_pKato->Log(FILEPERF_ZONE_FUNCTION,
                (TEXT("\tFailed: can't get a temp backup filename. Directory \"%s\" might not exist\r\n")),g_szDirectory );
		return FALSE;
	}

	_tcscpy(g_szFilespec, g_szDirectory);
	_tcscat(g_szFilespec,_T("\\"));
	_tcscat(g_szFilespec,TESTING_FILENAME);

	// 2.Alloc buffer to read/write and pattern for read

	// try to allocate consecutively smaller buffers until success
	BYTE* pBuffer = NULL;
	int cbBuffer = 65536; // 64 KB
    while(NULL == pBuffer && 0 != cbBuffer)
	{
        pBuffer = (BYTE*)VirtualAlloc(NULL, cbBuffer, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
        cbBuffer >>= 1;
    }

    if(NULL == pBuffer || cbBuffer < 2048)
	{
		g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("VirtualAlloc() failed; system error %u\r\n")), GetLastError() );
		if (NULL != pBuffer)
        	VirtualFree(pBuffer, 0, MEM_RELEASE);
        return FALSE;
    }

	g_nBufferSize = cbBuffer / 2;
	g_pBufferAll = g_pBuffer = pBuffer;
	g_pPattern = g_pBufferAll + g_nBufferSize;

	return TRUE;
}


// Free buffers
static void Finalization(void)
{
	if (NULL != g_pBufferAll)
    {
    	if(!VirtualFree(g_pBufferAll, 0, MEM_RELEASE))
        {
            g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("\tVirtualFree() failed; system eror %u")), GetLastError());
        }
        else
        	g_pBufferAll = NULL;
	}
}


//------------------------------------------------------------------------------
//
// Function: Test1
//
// This function tests file read/write performance.
//
// Parameters:
//      uiMsg
//           [in] Ignored.
//
//      tpParam
//           [in] Ignored.
//
//      lpFTE
//           [in] Ignored.
//
// Returns:
//      Specifies if the test passed (TPR_PASS), failed (TPR_FAIL), or was
//      skipped (TPR_SKIP).
//
//------------------------------------------------------------------------------

TESTPROCAPI Test1(UINT uMsg, TPPARAM tpParam, LPFUNCTION_TABLE_ENTRY lpFTE)
{
    // FILEPERF_TEST_FUNCTION_ENTRY();

    // Validate that the shell wants the test to run
    if (uMsg != TPM_EXECUTE)
    {
        return TPR_NOT_HANDLED;
    }

	g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("Directory=\"%s\",Size=%d\r\n")),g_szDirectory,g_nSize);

	if (! Initialization())
		return TPR_FAIL;

	g_pKato->Log(FILEPERF_ZONE_FUNCTION,(TEXT("Testing and backup files will be \"%s\" and \"%s\", size is %ld\r\n")),g_szFilespec,g_szFilespec2,g_nSize);

	INIT_TIMER

	UINT retVal = TPR_FAIL;

	if ((retVal = fnDeleteIfExisting()) != TPR_PASS)
		goto lDone;
	if ((retVal = fnOpenCloseNewEmpty()) != TPR_PASS)
		goto lDone;
	if ((retVal = fnWriteData()) != TPR_PASS)
		goto lDone;
	if ((retVal = fnRenameFile()) != TPR_PASS)
		goto lDone;
	if ((retVal = fnReadData()) != TPR_PASS)
		goto lDone;
	if ((retVal = fnDeleteFile()) != TPR_PASS)
		goto lDone;

lDone:
	Finalization();

	// FILEPERF_TEST_FUNCTION_EXIT();
    return  retVal;
}


void SetParameters(int nSize,TCHAR* pDirectory)
{
	g_nSize = nSize;
	_tcscpy(g_szDirectory,pDirectory);
}

⌨️ 快捷键说明

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