xml/xmlfw/test/rtest/tsrc/tp_perftestbase.cpp
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Generic performance test framework implementation. 
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file 
       
    20  @internalComponent
       
    21 */
       
    22 
       
    23 #include <e32base.h>
       
    24 #include <f32file.h>
       
    25 #include <hal.h>
       
    26 #include <e32test.h>
       
    27 #include <bacline.h>
       
    28 
       
    29 #include "tp_perftestbase.h"
       
    30 
       
    31 /**
       
    32 The log directory.
       
    33 */
       
    34 _LIT(KPTestOutput,       "c:\\logs\\");
       
    35 
       
    36 /**
       
    37 Global test object
       
    38 */
       
    39 RTest test(_L("Perfomance Testing"));
       
    40 
       
    41 /**
       
    42 Class constructor.
       
    43 
       
    44 @param aAPIName Name of the API method to test
       
    45 @param aAPIPtr Pointer to the API method
       
    46 @param aMaxDuration The maximum duration of the performance test for that API method
       
    47 */
       
    48 TApiRecord::TApiRecord(const TDesC8& aAPIName, TFuncPtr aAPIPtr, TUint64 aMaxDuration)
       
    49 	{
       
    50 	iAPIName = aAPIName;
       
    51 	iAPIPtr = aAPIPtr;
       
    52 	iMaxDuration = aMaxDuration;
       
    53 	}
       
    54 
       
    55 /**
       
    56 Set method for API pointer attribute
       
    57 
       
    58 @param aAPIPtr Pointer to API method
       
    59 */
       
    60 void TApiRecord::SetAPIPtr(TFuncPtr aAPIPtr)
       
    61 	{
       
    62 	iAPIPtr = aAPIPtr;
       
    63 	}
       
    64 	
       
    65 /**
       
    66 Set method for API name attribute
       
    67 
       
    68 @param aAPIName Function name
       
    69 */
       
    70 void TApiRecord::SetAPIName(TDesC8& aAPIName)
       
    71 	{
       
    72 	iAPIName = aAPIName;
       
    73 	}
       
    74 
       
    75 /**
       
    76 Get method for API name attribute
       
    77 
       
    78 @return API name string
       
    79 */
       
    80 const TDesC8& TApiRecord::APIName()
       
    81 	{
       
    82 	return iAPIName;
       
    83 	}
       
    84 	
       
    85 /**
       
    86 Get method for API method pointer
       
    87 
       
    88 @return API method pointer
       
    89 */
       
    90 TFuncPtr TApiRecord::APIPtr()
       
    91 	{
       
    92 	return iAPIPtr;
       
    93 	}
       
    94 	
       
    95 /**
       
    96 Set method for max duration attribute
       
    97 
       
    98 @param aMaxDuration The maximum duration of the performance test for that API method
       
    99 */
       
   100 void TApiRecord::SetMaxDuration(TUint64 aMaxDuration)
       
   101 	{
       
   102 	iMaxDuration = aMaxDuration;
       
   103 	}
       
   104 	
       
   105 /**
       
   106 Get method for max duration attribute
       
   107 
       
   108 @return The maximum duration of the performance test for that API method
       
   109 */
       
   110 TUint64 TApiRecord::MaxDuration()
       
   111 	{
       
   112 	return iMaxDuration;
       
   113 	}
       
   114 		
       
   115 /**
       
   116 Class consturctor. 
       
   117 */
       
   118 CPerformanceTests::CPerformanceTests(): iOutputFile(NULL)
       
   119 	{
       
   120 	
       
   121 	}
       
   122 	
       
   123 /**
       
   124 Second phase constructor. 
       
   125 Creates adequate directory and output file for logging purposes.
       
   126 
       
   127 @param aTestName Test name string
       
   128 */	
       
   129 void CPerformanceTests::ConstructL(const TDesC& aTestName)
       
   130 	{
       
   131 	ParserComandLineL();
       
   132 	if (iOutputFile != NULL)
       
   133 		{
       
   134 		HBufC* fileName = HBufC::NewLC(KPTestOutput().Length() + iOutputFile->Length());
       
   135 		TPtr ptr(fileName->Des());
       
   136 		
       
   137 		User::LeaveIfError(session.Connect());
       
   138 		
       
   139 		// create the directory if not existing
       
   140 		if (!session.IsValidName(KPTestOutput()))
       
   141 			{
       
   142 			session.MkDirAll(KPTestOutput());
       
   143 			}
       
   144 		
       
   145 		ptr = KPTestOutput;
       
   146 		ptr.Append(*iOutputFile);
       
   147 		
       
   148 		//create the file
       
   149 		User::LeaveIfError(output.Replace(session, *fileName, EFileWrite ));
       
   150 		CleanupStack::PopAndDestroy(fileName);
       
   151 		}
       
   152 	else
       
   153 		{
       
   154 		test.Start(aTestName);
       
   155 		test.Title();
       
   156 		}
       
   157 	}
       
   158 
       
   159 /**
       
   160 Destructor. 
       
   161 */
       
   162 CPerformanceTests::~CPerformanceTests()
       
   163 	{
       
   164 	while (list.Count() > 0)
       
   165 		{
       
   166 		delete list[list.Count()-1];
       
   167 		list.Remove(list.Count()-1);
       
   168 		}
       
   169 	if (iOutputFile != NULL)
       
   170 		{
       
   171 		delete iOutputFile;
       
   172 		}
       
   173 	list.Reset();
       
   174 	if (iOutputFile)
       
   175 		{
       
   176 		output.Flush();
       
   177 		output.Close();
       
   178 		session.Close();
       
   179 		}
       
   180 	else
       
   181 		{
       
   182 		test.End();
       
   183 		test.Close();
       
   184 		}
       
   185 	
       
   186 	}
       
   187 
       
   188 /**
       
   189 Runs all the required tests. 
       
   190 */
       
   191 void CPerformanceTests::RunTestsL()
       
   192 	{
       
   193 	GetMethodListL(list);
       
   194 	iRepNum = GetNumberOfRepetition();
       
   195 	PrepareTestsL();
       
   196 	
       
   197 	for (TInt i = 0 ; i < list.Count();i++)
       
   198 		{
       
   199 		TApiRecord* apiRecord = list[i];
       
   200 		TestApi(*apiRecord);
       
   201 		}
       
   202 	}
       
   203 
       
   204 /**
       
   205 Tests particular API method. 
       
   206 Logs the perfomance benchmarks to the output file. 
       
   207 
       
   208 @param aApi The API record 
       
   209 */
       
   210 void CPerformanceTests::TestApi(TApiRecord& aApi)
       
   211 	{
       
   212 	TUint64 total = 0;
       
   213 	TUint64 average = 0;
       
   214 	TUint64 firstCall = 0;
       
   215 	TBuf8<255> string;
       
   216 	TInt freq;
       
   217 	
       
   218 	if (iOutputFile)
       
   219 		{
       
   220 		output.Write(aApi.APIName());
       
   221 		}
       
   222 	
       
   223 	HAL::Get(HAL::EFastCounterFrequency, freq);
       
   224 		
       
   225 	//first call time verification	
       
   226 	iEndTime = 0;
       
   227 	iStartTime = 0;
       
   228 	aApi.APIPtr()();
       
   229 	
       
   230 	//error checking
       
   231 	if (iEndTime == 0 || iStartTime == 0)
       
   232 		{
       
   233 		// user test method did not call either ApiTestStart or ApiTestEnd method
       
   234 		User::Panic(_L("Xml Framework Error"), KErrGeneral);
       
   235 		}
       
   236 	
       
   237 	// calculate and convert to miliseconds
       
   238 	firstCall = (1000000*(iEndTime - iStartTime))/freq; 
       
   239 
       
   240 	for (TInt i = 0; i < iRepNum; i++)
       
   241 		{
       
   242 		// initialize members
       
   243 		iEndTime = 0;
       
   244 		iStartTime = 0;
       
   245 		
       
   246 		//method call
       
   247 		aApi.APIPtr()();
       
   248 		
       
   249 		//error checking
       
   250 		if (iEndTime == 0 || iStartTime == 0)
       
   251 			{
       
   252 			// user test method did not call either ApiTestStart or ApiTestEnd method
       
   253 			User::Panic(_L("Xml Framework Error"), KErrGeneral);
       
   254 			}
       
   255 		// time counting
       
   256 		total += iEndTime - iStartTime;
       
   257 		}
       
   258 	// calculate and convert to miliseconds
       
   259 	// Fast counter frequency already in MHz
       
   260 	total =  (1000000*total)/freq;
       
   261 	average = total/iRepNum;
       
   262 	if (iOutputFile)
       
   263 		{
       
   264 		string.Format(_L8("\n  First call time: %Ld ms\n  Number of repetitions: %d\n    Time: %Ld ms\n    Average: %Ld ms\n    Max duration: %Ld ms\n\n"),
       
   265 					firstCall,
       
   266 					iRepNum,
       
   267 					total,
       
   268 					average,
       
   269 					aApi.MaxDuration());
       
   270 		TPtrC8 des(string);
       
   271 		output.Write(des);
       
   272 		}
       
   273 	else
       
   274 		{
       
   275 #ifdef EABI
       
   276 		test(total < aApi.MaxDuration());
       
   277 #else
       
   278 		test(ETrue);
       
   279 #endif // EABI
       
   280 		TBuf<100> temp;
       
   281 		temp.Copy(aApi.APIName());
       
   282 		test.Printf(_L("\n %S - Performance testing results\n\t First call time: %Ld ms\n\t Number of repetitions: %d\n\t Time: %Ld ms \n\t Average: %Ld ms\n\t Max duration: %Ld ms \n"),
       
   283 		&temp, firstCall, iRepNum, total, average, aApi.MaxDuration());
       
   284 		}
       
   285 	}
       
   286 /**
       
   287 Stores the start time of Api call.
       
   288 ApiTestStart and ApiTestEnd should enlosed to particular API call. 
       
   289 */
       
   290 void CPerformanceTests::ApiTestStart()
       
   291 	{
       
   292 	iStartTime = User::FastCounter();
       
   293 	}
       
   294 	
       
   295 /**
       
   296 Stores the end time of Api call.
       
   297 ApiTestStart and ApiTestEnd should enlosed to particular API call. 
       
   298 */
       
   299 void CPerformanceTests::ApiTestEnd()
       
   300 	{
       
   301 	iEndTime = User::FastCounter();
       
   302 	}
       
   303 
       
   304 /**
       
   305 Parses the command line 
       
   306 */
       
   307 void CPerformanceTests::ParserComandLineL()
       
   308 	{
       
   309 	CCommandLineArguments* pCmd = CCommandLineArguments::NewLC();
       
   310 	if (pCmd->Count() == 3)
       
   311 		{
       
   312 		if (pCmd->Arg(1).CompareF(_L("-l")) == 0)
       
   313 			{
       
   314 			iOutputFile = pCmd->Arg(2).AllocL();
       
   315 			}	
       
   316 		}
       
   317 	
       
   318 	CleanupStack::PopAndDestroy(pCmd);
       
   319 	}
       
   320 
       
   321