searcher/searchserver/src/searchservertesting.cpp
changeset 2 6c1a2771f4b7
parent 1 6f2c1c46032b
child 3 ae3f1779f6da
child 4 e0a44815fded
equal deleted inserted replaced
1:6f2c1c46032b 2:6c1a2771f4b7
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "SearchServerConfiguration.h"
       
    19 
       
    20 #include "SearchServerTesting.h"
       
    21 #include "SearchServerLogger.h"
       
    22 
       
    23 #include "CSearchServer.h"
       
    24 
       
    25 #ifdef PROVIDE_TESTING_UTILITY
       
    26 
       
    27 
       
    28 #include <e32math.h>
       
    29 
       
    30 TPerformanceRecord::TPerformanceRecord() 
       
    31 : iMinimum(0x7fffffff), 
       
    32   iAverage(0),
       
    33   iVarEst(0),
       
    34   iPeak(0),  
       
    35   iSampleCount(0) 
       
    36 	{
       
    37 
       
    38 	}
       
    39 
       
    40 void TPerformanceRecord::Record( TInt aValue )
       
    41 	{
       
    42 	iAverage = ( iAverage ) * ( (double)iSampleCount / (double)( iSampleCount + 1. ) ) 
       
    43 					   + ( ( double ) aValue ) / ( iSampleCount + 1. );
       
    44 	iVarEst = ( iVarEst ) * ( (double)iSampleCount / (double)( iSampleCount + 1. ) ) 
       
    45 					   + ( ( aValue - iAverage ) * ( aValue - iAverage ) ) / ( iSampleCount + 1. );
       
    46 	iPeak = Max( iPeak, aValue );
       
    47 	iMinimum = Min( iMinimum, aValue );
       
    48 	iSampleCount++; 
       
    49 	}
       
    50 
       
    51 void TPerformanceRecord::Record( TPerformanceRecord& aRecord )
       
    52 	{
       
    53 	iAverage = ((iAverage*iSampleCount) + (aRecord.iAverage*aRecord.iSampleCount)) / (iSampleCount+aRecord.iSampleCount); 
       
    54 	iVarEst = ((iVarEst*iSampleCount) + (aRecord.iVarEst*aRecord.iSampleCount)) / (iSampleCount+aRecord.iSampleCount); 
       
    55 	iPeak = Max( iPeak, aRecord.iPeak ); 
       
    56 	iMinimum = Min( iPeak, aRecord.iPeak ); 
       
    57 	iSampleCount += aRecord.iSampleCount; 
       
    58 	}
       
    59 
       
    60 void TPerformanceRecord::ToStringL( TDes& aString, const TDesC& aUnit, TInt aScale )
       
    61 	{
       
    62 	TReal sd;
       
    63 	Math::Sqrt( sd, iVarEst );
       
    64 	aString.AppendNum( iMinimum / aScale );
       
    65 	aString.Append( _L( " " ) ); 
       
    66 	aString.Append( aUnit );
       
    67 	aString.Append( _L( " > ~") );
       
    68 	aString.AppendNum( (TInt)(iAverage / aScale) );
       
    69 	aString.Append( _L( "+-" ) ); 
       
    70 	aString.AppendNum( (TInt)(sd / aScale) ); 
       
    71 	aString.Append( _L( " " ) ); 
       
    72 	aString.Append( aUnit ); 
       
    73 	aString.Append( _L( " < ") );
       
    74 	aString.AppendNum( iPeak / aScale );
       
    75 	aString.Append( _L( " " ) ); 
       
    76 	aString.Append( aUnit ); 
       
    77 	aString.Append( _L( " (n=") );
       
    78 	aString.AppendNum( iSampleCount );
       
    79 	aString.Append( _L( ")") );
       
    80 	}
       
    81 
       
    82 int MemoryRecorder::iInstances = 0; 
       
    83 
       
    84 MemoryRecorder* MemoryRecorder::NewL()
       
    85 	{
       
    86 	MemoryRecorder* self = new ( ELeave ) MemoryRecorder(); 	
       
    87 	CleanupStack::PushL( self ); 
       
    88 	self->ConstructL();
       
    89 	CleanupStack::Pop( self ); 
       
    90 	return self; 
       
    91 	}
       
    92 
       
    93 MemoryRecorder::MemoryRecorder()
       
    94 :  	iSemaphore(), 
       
    95 	iWorkerThread(),
       
    96  	iAbandon( EFalse  ), 
       
    97  	iRecord( NULL )
       
    98  	{
       
    99 	}
       
   100 
       
   101 void MemoryRecorder::ConstructL()
       
   102 	{
       
   103 	TThreadFunction function = MemoryRecorder::ThreadFunction; 
       
   104 	User::LeaveIfError( iSemaphore.CreateLocal( 0 ) ); 
       
   105 	TBuf<64> buf; 
       
   106 	buf.Append( _L( "MemUsgRecord" ) ); 
       
   107 	buf.AppendNum( iInstances++ ); 
       
   108 	User::LeaveIfError( iWorkerThread.Create( buf, function, 2048, NULL, this ) ); 
       
   109 	// Ownerhips is safely transferred to the worker thread
       
   110 	// Go thread go. It will get stuck in the semaphore
       
   111 	iWorkerThread.Resume(); 
       
   112 	}
       
   113 
       
   114 MemoryRecorder::~MemoryRecorder()
       
   115 	{
       
   116 	if ( iRecord ) 
       
   117 		{
       
   118 		// We are still recording!
       
   119 		iSemaphore.Wait(); // need to do this to access the iRecord
       
   120 		delete iRecord; 
       
   121 		// And the thread is stuck 
       
   122 		// Semaphore is in 0
       
   123 		}
       
   124 	
       
   125 	iAbandon = ETrue; // Thread, let's go and die
       
   126 	iSemaphore.Signal(); // Release thread and kill semaphore
       
   127 
       
   128 	TRequestStatus status; 
       
   129 	iWorkerThread.Logon( status );
       
   130 	User::WaitForRequest( status ); 
       
   131 
       
   132 	iWorkerThread.Close(); 
       
   133 	iSemaphore.Close(); 
       
   134 	}
       
   135 
       
   136 void MemoryRecorder::StartL( TInt aRecordingIntervalMs ) 
       
   137 	{
       
   138 	if ( iRecord ) 
       
   139 		{
       
   140 		User::Leave( KErrNotReady ); 
       
   141 		}
       
   142 	
       
   143 	iIntervalMs = aRecordingIntervalMs;
       
   144 	iRecord = new TPerformanceRecord(); 
       
   145 	Record(); 
       
   146 	iSemaphore.Signal(); // Go thread go 
       
   147 	
       
   148 	// Must not touch record from this thread anymore
       
   149 	}
       
   150 
       
   151 TBool MemoryRecorder::IsActive() 
       
   152 	{
       
   153 	return iRecord != 0; 
       
   154 	}
       
   155 
       
   156 TPerformanceRecord* MemoryRecorder::Finish()
       
   157 	{
       
   158 	if ( !iRecord ) 
       
   159 		{
       
   160 		User::Panic( _L( "MemUsageRecord" ), 1 ); 
       
   161 		}
       
   162 	iSemaphore.Wait(); // Thread, stop
       
   163 	// Can touch record from this thread now 
       
   164 	Record(); 
       
   165 	TPerformanceRecord* ret = iRecord;
       
   166 	iRecord = NULL; 
       
   167 	return ret; 
       
   168 	}
       
   169 
       
   170 void MemoryRecorder::Record()
       
   171 	{
       
   172     TInt 
       
   173         largestBlock;
       
   174     TInt 
       
   175         mem = User::Heap().Size() - User::Heap().Available(largestBlock);
       
   176     
       
   177     iRecord->Record( mem ); 
       
   178 	}
       
   179 
       
   180 void MemoryRecorder::Work()
       
   181 	{
       
   182 	while (true) 
       
   183 		{
       
   184 		iSemaphore.Wait();
       
   185 		// Condition: iAbandon == ETrue || iRecord != NULL 
       
   186 		if ( iAbandon ) {
       
   187 			break; // Quit execution
       
   188 		}
       
   189 		Record(); 
       
   190 		iSemaphore.Signal();
       
   191 
       
   192 		User::After( iIntervalMs * 1000 );
       
   193 		}
       
   194 	}
       
   195 
       
   196 	
       
   197 TInt MemoryRecorder::ThreadFunction(void *aThis)
       
   198 	{	
       
   199 	(reinterpret_cast<MemoryRecorder*>(aThis))->Work();
       
   200 	return KErrNone;
       
   201 	}
       
   202 
       
   203 #endif // 
       
   204 
       
   205 
       
   206 #ifdef LOG_PERIODIC_MEMORY_USAGE
       
   207 
       
   208 CMemoryLogger::CMemoryLogger() 
       
   209 	{
       
   210 	}
       
   211 
       
   212 CMemoryLogger::~CMemoryLogger() 
       
   213 	{
       
   214 	//iPeriodic->Cancel(); 
       
   215 	delete iPeriodic;
       
   216 	if ( iRecorder ) 
       
   217 		{
       
   218 		iRecorder->Close();
       
   219 		}
       
   220 	}
       
   221 
       
   222 	
       
   223 void CMemoryLogger::StartL()
       
   224 	{
       
   225 	TCallBack callback(CMemoryLogger::RunThisL, this);
       
   226 	iPeriodic = CPeriodic::NewL( CActive::EPriorityNormal ); 
       
   227 	iPeriodic->Start( TTimeIntervalMicroSeconds32( KMemoryLoggingIntervalMs*1000 ), 
       
   228 					  TTimeIntervalMicroSeconds32( KMemoryLoggingIntervalMs*1000 ), 
       
   229 					  callback ); 
       
   230 
       
   231 	iRecorder = RMemoryRecorder::NewL(); 
       
   232 	iRecorder->StartL( KMemoryRecordingIntervalMs );
       
   233 	}
       
   234 	
       
   235 void CMemoryLogger::Stop()
       
   236 	{
       
   237 	iPeriodic->Cancel(); 
       
   238 	delete iPeriodic; 
       
   239 	iPeriodic = NULL; 
       
   240 	iRecorder->Close(); 
       
   241 	iRecorder = NULL; 
       
   242 	}
       
   243 	
       
   244 void CMemoryLogger::RunL()
       
   245 	{
       
   246 	TPerformanceRecord* record = iRecorder->Finish(); 
       
   247 
       
   248 	CleanupStack::PushL( record ); 
       
   249 	
       
   250 	TBuf<256> buf; 
       
   251 	buf.Append( _L("SearchServer ") );
       
   252 	record->ToStringL( buf ); 
       
   253 	PERFORMANCE_LOG( buf );
       
   254 	
       
   255 	CleanupStack::PopAndDestroy( record ); 				
       
   256 	
       
   257 	iRecorder->StartL( KMemoryRecordingIntervalMs );
       
   258 	}
       
   259 	
       
   260 TInt CMemoryLogger::RunThisL(void *aThis)
       
   261 	{
       
   262 	(reinterpret_cast<CMemoryLogger*>(aThis))->RunL(); 
       
   263 	return KErrNone; 
       
   264 	}
       
   265 
       
   266 #endif
       
   267 
       
   268 #ifdef PROVIDE_TESTING_UTILITY
       
   269 
       
   270 
       
   271 TFunctionPerformanceEntry::TFunctionPerformanceEntry( const TDesC& aId )
       
   272 	{
       
   273 	iId = aId; 
       
   274 	}
       
   275 
       
   276 CSearchServerTesting* CSearchServerTesting::iInstance = NULL;
       
   277 
       
   278 CSearchServerTesting* CSearchServerTesting::Instance()
       
   279 	{
       
   280 	return iInstance; 
       
   281 	}
       
   282 	
       
   283 CSearchServerTesting::CSearchServerTesting()
       
   284 :	iRecorder( 0 ),
       
   285 	iServer( 0 ),
       
   286 	iPeriodic( 0 ),
       
   287 	iEntries()
       
   288 	{
       
   289 	iInstance = this;
       
   290 	}
       
   291 
       
   292 void CSearchServerTesting::ConstructL()
       
   293 	{
       
   294 	User::LeaveIfError( iFs.Connect() ); 
       
   295 	iPeriodic = CPeriodic::NewL( CActive::EPriorityLow ); 
       
   296 	iRecorder = MemoryRecorder::NewL();
       
   297 	}
       
   298 
       
   299 CSearchServerTesting::~CSearchServerTesting()
       
   300 	{
       
   301 	Stop(); 
       
   302 	
       
   303 	delete iRecorder;
       
   304 	
       
   305 	iEntries.Reset(); 
       
   306 	iEntries.Close(); 
       
   307 	
       
   308 	iInstance = NULL;
       
   309 	delete iPeriodic;
       
   310 	
       
   311 	iPeriodic = NULL; 
       
   312 	iFs.Close(); 
       
   313 	}
       
   314 	
       
   315 void CSearchServerTesting::SetServer( CSearchServer* aServer )
       
   316 	{
       
   317 	iServer = aServer; 
       
   318 	}
       
   319 
       
   320 void CSearchServerTesting::Reset()
       
   321 	{
       
   322 	iEntries.Reset(); 
       
   323 	}
       
   324 
       
   325 TFunctionPerformanceEntry& CSearchServerTesting::FunctionEntry( const TDesC& aFunctionId )
       
   326 	{
       
   327 	for ( TInt i = 0; i < iEntries.Count(); i++ ) 
       
   328 		{
       
   329 		if ( iEntries[i].iId == aFunctionId )
       
   330 			{
       
   331 			return iEntries[i]; 
       
   332 			}
       
   333 		}
       
   334 	
       
   335 	TFunctionPerformanceEntry entry( aFunctionId );
       
   336 	TRAP_IGNORE(iEntries.AppendL( entry )); 
       
   337 	
       
   338 	return iEntries[iEntries.Count()-1]; 
       
   339 	}
       
   340 
       
   341 void CSearchServerTesting::StartL()
       
   342 	{
       
   343 	User::LeaveIfError( iFs.Connect() ); 
       
   344 	TCallBack callback( CSearchServerTesting::RunThisL, this );
       
   345 	iPeriodic->Start( TTimeIntervalMicroSeconds32( TSignalCheckIntervalMs*1000 ), 
       
   346 					  TTimeIntervalMicroSeconds32( TSignalCheckIntervalMs*1000 ), 
       
   347 					  callback ); 
       
   348 }
       
   349 
       
   350 void CSearchServerTesting::Stop()
       
   351 	{
       
   352 	iPeriodic->Cancel(); 
       
   353 	}
       
   354 
       
   355 void CSearchServerTesting::RunL()
       
   356 	{
       
   357 	TEntry entry; 
       
   358 
       
   359 	if ( iFs.Entry( KStartRecordingSignalFile, entry ) == KErrNone  ) 
       
   360 		{
       
   361 		iFs.Delete( KStartRecordingSignalFile );
       
   362 		StartRecordingL(); 
       
   363 		}
       
   364 
       
   365 	if ( iFs.Entry( KStopRecordingSignalFile, entry ) == KErrNone  ) 
       
   366 		{
       
   367 		iFs.Delete( KStopRecordingSignalFile );
       
   368 		StopRecording(); 
       
   369 		}
       
   370 
       
   371 	if ( iFs.Entry( KDumpRecordSignalFile, entry ) == KErrNone  ) 
       
   372 		{
       
   373 		iFs.Delete( KDumpRecordSignalFile );
       
   374 		DumpRecordL(); 
       
   375 		}
       
   376 
       
   377 	if ( iFs.Entry( KShutdownSignalFile, entry ) == KErrNone ) 
       
   378 		{
       
   379 		iFs.Delete( KShutdownSignalFile );
       
   380 		ShutdownL(); 
       
   381 		}
       
   382 	}
       
   383 
       
   384 TInt CSearchServerTesting::RunThisL(void *aThis)
       
   385 	{
       
   386 	(reinterpret_cast<CSearchServerTesting*>(aThis))->RunL(); 
       
   387 	return KErrNone; 
       
   388 	}
       
   389 
       
   390 void CSearchServerTesting::ShutdownL()
       
   391 	{
       
   392 	if ( iServer ) 
       
   393 		{
       
   394 		iServer->ShutDown();
       
   395 		}
       
   396 	}
       
   397 	
       
   398 void CSearchServerTesting::StartRecordingL()
       
   399 	{
       
   400 	if ( !iRecorder->IsActive() )
       
   401 		{
       
   402 		iRecorder->StartL( KMemoryRecordingIntervalMs ); 
       
   403 		}
       
   404 	}
       
   405 
       
   406 void CSearchServerTesting::StopRecording()
       
   407 	{
       
   408 	if ( iRecorder->IsActive() )
       
   409 		{
       
   410         TPerformanceRecord* record;
       
   411         record = iRecorder->Finish();
       
   412 		delete record; 
       
   413 		}
       
   414 	}
       
   415 	
       
   416 
       
   417 void CSearchServerTesting::DumpRecordL()
       
   418 	{
       
   419 	if ( !iRecorder->IsActive() ) return; // not active
       
   420 
       
   421 	TPerformanceRecord* record = iRecorder->Finish();
       
   422 	CleanupStack::PushL( record ); 
       
   423 
       
   424 	HBufC8* buf = HBufC8::NewLC( 2048 ); 
       
   425 	
       
   426 	RFile file;
       
   427 	TBool created = EFalse; 
       
   428 	if ( file.Open( iFs, KServerRecordFile, EFileWrite ) == KErrNotFound ) 
       
   429 		{
       
   430 		User::LeaveIfError( file.Create( iFs, KServerRecordFile, EFileWrite ) );
       
   431 		created = ETrue; 
       
   432 		}
       
   433 	CleanupClosePushL( file ); 
       
   434 
       
   435 	TInt end = 0;
       
   436 	file.Seek( ESeekEnd, end );
       
   437 	
       
   438 	if ( created ) 
       
   439 		{
       
   440 		buf->Des().Append( _L8("heap min ; heap aver; heap peak\n" ) );
       
   441 		file.Write( *buf ); 
       
   442 		buf->Des().Zero();
       
   443 		}
       
   444 	
       
   445 	buf->Des().AppendNum( record->iMinimum ); 
       
   446 	
       
   447 	TInt spaces = Max( 0, 9 - buf->Length() );
       
   448 	if ( spaces ) buf->Des().AppendFill( ' ', spaces );
       
   449 	buf->Des().Append( _L8("; " ) );
       
   450 	buf->Des().AppendNum( record->iAverage ); 
       
   451 	
       
   452 	spaces = Max( 0, 20 - buf->Length() );
       
   453 	if ( spaces ) buf->Des().AppendFill( ' ', spaces );
       
   454 	buf->Des().Append( _L8("; " ) );
       
   455 	buf->Des().AppendNum( record->iPeak ); 
       
   456 	
       
   457 	buf->Des().Append( _L8("\n" ) );
       
   458 	file.Write( *buf ); 
       
   459 	file.Flush();
       
   460 	
       
   461 	CleanupStack::PopAndDestroy(); // file
       
   462 	CleanupStack::PopAndDestroy( buf ); 
       
   463 	CleanupStack::PopAndDestroy( record ); 
       
   464 	
       
   465 	Reset(); 
       
   466 	StartRecordingL(); 
       
   467 	}
       
   468 
       
   469 
       
   470 	
       
   471 #endif  // PROVIDE_TESTING_UTILITY
       
   472 
       
   473