imagingandcamerafws/imagingfws/MediaClientImage/Source/Test/TMdaStress/Tmdatest.cpp
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 2000-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 // TMDAVID1.CPP
       
    15 // These tests are designed to perform multiple Media Server opertaions by using multiple threads
       
    16 // each containing many active objects.
       
    17 // 
       
    18 //
       
    19 
       
    20 #include "tmdatest.h"
       
    21 
       
    22 
       
    23 /*********************************************************************************************/
       
    24 class CStressTestMonitor : public CBase
       
    25 	{
       
    26 public:
       
    27 	void IncActiveCount();
       
    28 	void DecActiveCount();
       
    29 	void SetError(TInt aError);
       
    30 	TInt Error();
       
    31 private:
       
    32 	TInt iActiveCount;
       
    33 	TInt iError;
       
    34 	};
       
    35 
       
    36 void CStressTestMonitor::IncActiveCount()
       
    37 	{
       
    38 	iActiveCount++;
       
    39 	}
       
    40 
       
    41 void CStressTestMonitor::DecActiveCount()
       
    42 	{
       
    43 	iActiveCount--;
       
    44 	if(iActiveCount==0)
       
    45 		CActiveScheduler::Stop();
       
    46 	}
       
    47 
       
    48 void CStressTestMonitor::SetError(TInt aError)
       
    49 	{
       
    50 	if(iError==KErrNone)
       
    51 		iError = aError;
       
    52 	}
       
    53 
       
    54 TInt CStressTestMonitor::Error()
       
    55 	{
       
    56 	return(iError);
       
    57 	}
       
    58 
       
    59 /*********************************************************************************************/
       
    60 
       
    61 
       
    62 
       
    63 /*********************************************************************************************/
       
    64 class CStressTestActive : public CActive
       
    65 	{
       
    66 public:
       
    67 	inline CStressTestActive(CStressTestMonitor& aMonitor) : CActive(EPriorityStandard), iMonitor(&aMonitor) { aMonitor.IncActiveCount(); }
       
    68 	~CStressTestActive();
       
    69 protected:
       
    70 	// From CActive
       
    71 	TInt RunError(TInt aError);
       
    72 	// New
       
    73 	void SetPending();
       
    74 public:
       
    75 	CStressTestMonitor* iMonitor;
       
    76 	};
       
    77 
       
    78 CStressTestActive::~CStressTestActive()
       
    79 	{
       
    80 	iMonitor->DecActiveCount();
       
    81 	}
       
    82 
       
    83 void CStressTestActive::SetPending()
       
    84 	{
       
    85 	if(!IsAdded())
       
    86 		CActiveScheduler::Add(this);
       
    87 	iStatus = KRequestPending;
       
    88 	SetActive();
       
    89 	}
       
    90 
       
    91 TInt CStressTestActive::RunError(TInt aError)
       
    92 	{
       
    93 	iMonitor->SetError(aError);
       
    94 	delete this;
       
    95 	return KErrNone;
       
    96 	}
       
    97 
       
    98 /*********************************************************************************************/
       
    99 
       
   100 
       
   101 
       
   102 /*********************************************************************************************/
       
   103 class CImageUtil : public CStressTestActive, public MMdaImageUtilObserver
       
   104 	{
       
   105 public:
       
   106 	inline CImageUtil(CStressTestMonitor& aMonitor) : CStressTestActive(aMonitor) {}
       
   107 private:
       
   108 	virtual void MiuoCreateComplete(TInt aError);
       
   109 	virtual void MiuoOpenComplete(TInt aError);
       
   110 	virtual void MiuoConvertComplete(TInt aError);
       
   111 	};
       
   112 
       
   113 void CImageUtil::MiuoCreateComplete(TInt aError)
       
   114 	{
       
   115 	TRequestStatus* status = &iStatus;
       
   116 	User::RequestComplete(status,aError);
       
   117 	}
       
   118 
       
   119 void CImageUtil::MiuoOpenComplete(TInt aError)
       
   120 	{
       
   121 	TRequestStatus* status = &iStatus;
       
   122 	User::RequestComplete(status,aError);
       
   123 	}
       
   124 
       
   125 void CImageUtil::MiuoConvertComplete(TInt aError)
       
   126 	{
       
   127 	TRequestStatus* status = &iStatus;
       
   128 	User::RequestComplete(status,aError);
       
   129 	}
       
   130 /*********************************************************************************************/
       
   131 
       
   132 
       
   133 
       
   134 /*********************************************************************************************/
       
   135 class CImageLoadUtil : public CImageUtil
       
   136 	{
       
   137 public:
       
   138 	static CImageLoadUtil* NewL(CStressTestMonitor& aMonitor,const TDesC& aFileName,const TDisplayMode aDisplayMode);
       
   139 	~CImageLoadUtil();
       
   140 private:
       
   141 	inline CImageLoadUtil(CStressTestMonitor& aMonitor) : CImageUtil(aMonitor) {};
       
   142 	void RunL();
       
   143 	void DoCancel();
       
   144 private:
       
   145 	TBuf<255> iMessage;
       
   146 	TDisplayMode iDisplayMode;
       
   147 	CFbsBitmap* iBitmap;
       
   148 	CMdaImageFileToBitmapUtility* iMdaUtil;
       
   149 	TInt iState;
       
   150 	};
       
   151 
       
   152 CImageLoadUtil* CImageLoadUtil::NewL(CStressTestMonitor& aMonitor,const TDesC& aFileName,const TDisplayMode aDisplayMode)
       
   153 	{
       
   154 	CImageLoadUtil* ptr = new (ELeave) CImageLoadUtil(aMonitor);
       
   155 
       
   156 	CleanupStack::PushL(ptr);
       
   157 
       
   158 	ptr->iBitmap = new(ELeave) CFbsBitmap;
       
   159 	ptr->iDisplayMode = aDisplayMode;
       
   160 
       
   161 	ptr->iMdaUtil = CMdaImageFileToBitmapUtility::NewL(*ptr);
       
   162 
       
   163 	ptr->iMessage.Append(_L("Loading image "));
       
   164 	ptr->iMessage.Append(aFileName);
       
   165 
       
   166 	ptr->iMdaUtil->OpenL(aFileName,NULL,NULL,NULL);
       
   167 
       
   168 	CleanupStack::Pop();
       
   169 
       
   170 	ptr->SetPending();
       
   171 
       
   172 	return ptr;
       
   173 	}
       
   174 
       
   175 CImageLoadUtil::~CImageLoadUtil()
       
   176 	{
       
   177 	Cancel();
       
   178 	delete iBitmap;
       
   179 	delete iMdaUtil;
       
   180 	}
       
   181 
       
   182 void CImageLoadUtil::RunL()
       
   183 	{
       
   184 	TInt error = iStatus.Int();
       
   185 
       
   186 	if(error==KErrNone)
       
   187 		{
       
   188 		if(!iState)
       
   189 			{
       
   190 			TFrameInfo frameInfo;
       
   191 			iMdaUtil->FrameInfo(0,frameInfo);
       
   192 
       
   193 			iBitmap->Reset();
       
   194 			error = iBitmap->Create(frameInfo.iOverallSizeInPixels,iDisplayMode);
       
   195 			if(error==KErrNone)
       
   196 				{
       
   197 				TRAP(error,iMdaUtil->ConvertL(*iBitmap));
       
   198 				if(error==KErrNone)
       
   199 					{
       
   200 					iState++;
       
   201 					SetPending();
       
   202 					return;
       
   203 					}
       
   204 				}
       
   205 			}
       
   206 		}
       
   207 
       
   208 	if(error!=KErrNone)
       
   209 		{
       
   210 		iMessage.AppendFormat(_L(" - Failed with error %d"),error);
       
   211 		RDebug::Print(iMessage);
       
   212 		User::Leave(error);
       
   213 		}
       
   214 	else
       
   215 		{
       
   216 		iMessage.AppendFormat(_L(" - Finished OK"));
       
   217 		RDebug::Print(iMessage);
       
   218 		}
       
   219 	delete this;
       
   220 	}
       
   221 
       
   222 void CImageLoadUtil::DoCancel()
       
   223 	{
       
   224 	iMdaUtil->Cancel();
       
   225 	}
       
   226 
       
   227 /*********************************************************************************************/
       
   228 
       
   229 
       
   230 
       
   231 /*********************************************************************************************/
       
   232 GLDEF_C TInt TestStartupThread(TAny* /*aParam*/)
       
   233 	{
       
   234 	return KErrNone;
       
   235 	}
       
   236 /*********************************************************************************************/
       
   237 
       
   238 
       
   239 
       
   240 
       
   241 /*********************************************************************************************/
       
   242 void DoTestLoadImagesL(CMdaStressTestThread* aThread)
       
   243 	{
       
   244 	TInt err;
       
   245 
       
   246 	 // Install active scheduler
       
   247 	CActiveScheduler* as = new CActiveScheduler;
       
   248 	__ASSERT_ALWAYS(as,User::Invariant());	
       
   249 	CActiveScheduler::Install(as);
       
   250 	CleanupStack::PushL(as);
       
   251 
       
   252 	// Connect to file server
       
   253 	RFs fs;
       
   254 	err = fs.Connect();
       
   255 	__ASSERT_ALWAYS(err == KErrNone, User::Invariant());
       
   256 	CleanupClosePushL(fs);
       
   257 
       
   258 	// Connect to FBS
       
   259 	User::LeaveIfError(FbsStartup());
       
   260 	User::LeaveIfError(RFbsSession::Connect());
       
   261 
       
   262 	// Create monitor object
       
   263 	CStressTestMonitor* monitor = new (ELeave) CStressTestMonitor;
       
   264 	CleanupStack::PushL(monitor);
       
   265 
       
   266 	CDir* dir = NULL;
       
   267 	fs.GetDir(KNullDesC,0,0,dir);
       
   268 	CleanupStack::PushL(dir);
       
   269 	TInt entries = dir->Count();
       
   270 
       
   271 	TDisplayMode displayMode;
       
   272 	if(aThread->iId==CMdaStressTest::ELoadImages1)
       
   273 		displayMode = EColor256;
       
   274 	else
       
   275 		displayMode = EGray4;
       
   276 
       
   277 	entries--;
       
   278 	while(entries>=0)
       
   279 		{
       
   280 		const TEntry& entry = (*dir)[entries];
       
   281 		TFileName fileName;
       
   282 		User::LeaveIfError(fs.DefaultPath(fileName));
       
   283 		fileName.Append(entry.iName);
       
   284 		CImageLoadUtil::NewL(*monitor,fileName,displayMode);
       
   285 		entries -= 2;	// only load every other image to keep memory usage down
       
   286 		}
       
   287 	CleanupStack::PopAndDestroy();	// dir
       
   288 
       
   289 	// Run active objects
       
   290 	CActiveScheduler::Start();
       
   291 
       
   292 	// Check for errors
       
   293 	User::LeaveIfError(monitor->Error());
       
   294 
       
   295 	// Close everything
       
   296 	CleanupStack::PopAndDestroy(); // monitor
       
   297 	RFbsSession::Disconnect();
       
   298 	CleanupStack::PopAndDestroy(2); // fs and as
       
   299 	}
       
   300 /*********************************************************************************************/
       
   301 
       
   302 
       
   303 
       
   304 /*********************************************************************************************/
       
   305 GLDEF_C TInt TestLoadImages(TAny* aParam)
       
   306 	{
       
   307 	CTrapCleanup* tc = CTrapCleanup::New();
       
   308 	__ASSERT_ALWAYS(tc,User::Invariant());	
       
   309 
       
   310 	TRAPD(err,DoTestLoadImagesL(REINTERPRET_CAST(CMdaStressTestThread*,aParam)));
       
   311 
       
   312 	delete tc;
       
   313 	return err;
       
   314 	}
       
   315 /*********************************************************************************************/
       
   316 
       
   317 
       
   318 
       
   319 /*********************************************************************************************/
       
   320 CMdaStressTest::CMdaStressTest()
       
   321 	: CMdaCoreTest(), iThreadList(_FOFF(CMdaStressTestThread,iLink)), iThreadIter(iThreadList), iRandomSeed(1)
       
   322 	{
       
   323 	}
       
   324 /*********************************************************************************************/
       
   325 
       
   326 
       
   327 
       
   328 /*********************************************************************************************/
       
   329 CMdaStressTest::~CMdaStressTest()
       
   330 	{
       
   331 	// Delete all threads
       
   332 	CMdaStressTestThread* thread;
       
   333 	iThreadIter.SetToFirst();
       
   334 	while ((thread=iThreadIter++)!=NULL)
       
   335 		DeleteTestThread(thread);
       
   336 	}
       
   337 /*********************************************************************************************/
       
   338 
       
   339 
       
   340 
       
   341 const TInt KThreadScanDelay = 200000;
       
   342 
       
   343 /*********************************************************************************************/
       
   344 void CMdaStressTest::DoTestL()
       
   345 	{
       
   346 	TInt error = KErrNone;
       
   347 	TInt time = 10000000;
       
   348 	TInt testNumber;
       
   349 	CMdaStressTestThread* thread;
       
   350 
       
   351 	// Launch test threads
       
   352 	for(testNumber=0; testNumber<ENumTestThreads; testNumber++)
       
   353 		StartTestThreadL(STATIC_CAST(TTestThreadId,testNumber));
       
   354 
       
   355 	// Scan thread list
       
   356 	do
       
   357 		{
       
   358 		TInt numThreads = 0;
       
   359 
       
   360 		iThreadIter.SetToFirst();
       
   361 		while ((thread=iThreadIter++)!=NULL)
       
   362 			{
       
   363 			if(thread->iThread.ExitType()==EExitPending)
       
   364 				numThreads++;
       
   365 			else
       
   366 				{
       
   367 				TInt reason = thread->iThread.ExitReason();
       
   368 				if(reason!=KErrNone)
       
   369 					{
       
   370 					iEngine->TestFailed(reason);
       
   371 					error = reason;
       
   372 					break;
       
   373 					}
       
   374 				testNumber = thread->iId;
       
   375 				DeleteTestThread(thread);
       
   376 				StartTestThreadL(STATIC_CAST(TTestThreadId,testNumber));	// Restart test
       
   377 				}
       
   378 			}
       
   379 
       
   380 		if(error)
       
   381 			break;
       
   382 
       
   383 		User::After(KThreadScanDelay);
       
   384 		time -= KThreadScanDelay;
       
   385 		}
       
   386 	while(time>0);
       
   387 
       
   388 //	iEngine->Print(_L("Waiting for test threads to end..."));
       
   389 
       
   390 	// Wait for all threads to finish
       
   391 	while(!iThreadList.IsEmpty())
       
   392 		{
       
   393 		User::After(KThreadScanDelay);
       
   394 		// Delete all completed threads
       
   395 		iThreadIter.SetToFirst();
       
   396 		while ((thread=iThreadIter++)!=NULL)
       
   397 			if(thread->iThread.ExitType()!=EExitPending)
       
   398 				{
       
   399 				TInt reason = thread->iThread.ExitReason();
       
   400 				if(reason!=KErrNone)
       
   401 					iEngine->TestFailed(reason);
       
   402 				DeleteTestThread(thread);
       
   403 				}
       
   404 		}
       
   405 
       
   406 
       
   407 	}
       
   408 /*********************************************************************************************/
       
   409 
       
   410 
       
   411 
       
   412 /*********************************************************************************************/
       
   413 void CMdaStressTest::StartTestThreadL(TTestThreadId aId)
       
   414 	{
       
   415 	TThreadFunction function;
       
   416 
       
   417 	switch(aId)
       
   418 		{
       
   419 		case ELoadImages1:
       
   420 		case ELoadImages2:
       
   421 			function = TestLoadImages;
       
   422 			break;
       
   423 		default:
       
   424 			return;
       
   425 		}
       
   426 
       
   427 	TBuf<40> name;
       
   428 	name.Append(_L("CMdaStressTestThread #"));
       
   429 	name.AppendNum(aId,EDecimal);
       
   430 	name.Append(_L("-"));
       
   431 	name.AppendNum(iThreadNameCount++,EDecimal);
       
   432 
       
   433 	CMdaStressTestThread* thread = new (ELeave) CMdaStressTestThread;
       
   434 	CleanupStack::PushL(thread);
       
   435 
       
   436 	thread->iId = aId;
       
   437 	User::LeaveIfError(thread->iThread.Create(name, function, KDefaultStackSize, 1024, 2048*1024, thread));
       
   438 
       
   439 	CleanupStack::Pop();
       
   440 
       
   441 	iThreadList.AddLast(*thread);
       
   442 	thread->iThread.Resume();
       
   443 	}
       
   444 /*********************************************************************************************/
       
   445 
       
   446 
       
   447 
       
   448 /*********************************************************************************************/
       
   449 void CMdaStressTest::DeleteTestThread(CMdaStressTestThread* thread)
       
   450 	{
       
   451 	if(thread->iThread.ExitType()==EExitPending)
       
   452 		thread->iThread.Terminate(KErrNone);
       
   453 	thread->iThread.Close();
       
   454 	iThreadList.Remove(*thread);
       
   455 	delete thread;
       
   456 	}
       
   457 /*********************************************************************************************/