kerneltest/e32test/bench/t_kernbm.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1995-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 the License "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 // e32test\bench\t_kernbm.cpp
       
    15 // Overview:
       
    16 // Benchmark the performance of some kernel functions including RTimer
       
    17 // and RThread. Calculation time required for creation & closing of
       
    18 // timer objects, suspension of threads, handling kernel messages, and 
       
    19 // the number of threads created and stopped within a fixed time period.
       
    20 // API Information:
       
    21 // RTimer, RThread.
       
    22 // Details:
       
    23 // - Calculate and display the time required to create and close 1000 
       
    24 // thread-relative timer objects.
       
    25 // - Count and print the time taken to suspend the thread for 100000 times.
       
    26 // - Count and display the time consumed for 100000 kernel messages.
       
    27 // - Get and print the number of threads created and stopped within a 
       
    28 // specified timeframe, verify the ExitType, and ExitReason of the threads.
       
    29 // Platforms/Drives/Compatibility:
       
    30 // All.
       
    31 // Assumptions/Requirement/Pre-requisites:
       
    32 // Failures and causes:
       
    33 // Base Port information:
       
    34 // 
       
    35 //
       
    36 
       
    37 #include <e32test.h>
       
    38 
       
    39 #define	TIMECOUNT()	User::NTickCount()
       
    40 #define TIMEDIFFMS(i,f)	(f-i)
       
    41 
       
    42 LOCAL_D RTest test(_L("T_KERNBM"));
       
    43 volatile TInt Count;
       
    44 volatile TBool Stop;
       
    45 
       
    46 #ifdef __EPOC32__
       
    47 extern void DummyKernMsg();
       
    48 #else
       
    49 void DummyKernMsg()
       
    50 	{}
       
    51 #endif
       
    52 
       
    53 LOCAL_C TInt ThreadFunction(TAny*)
       
    54 	{
       
    55 	User::WaitForAnyRequest();
       
    56 	return 0;
       
    57 	}
       
    58 
       
    59 TInt TestThread(TAny*)
       
    60 	{
       
    61 	return 0;
       
    62 	}
       
    63 
       
    64 TInt TestThreadCreate(TAny*)
       
    65 	{
       
    66 	RThread t;
       
    67 	t.SetPriority(EPriorityAbsoluteVeryLow);
       
    68 	while (!Stop)
       
    69 		{
       
    70 		t.Create(KNullDesC,TestThread,4096,NULL,NULL);
       
    71 		t.Resume();
       
    72 		t.Close();
       
    73 		++Count;
       
    74 		}
       
    75 	return 0;
       
    76 	}
       
    77 
       
    78 GLDEF_C TInt E32Main()
       
    79 	{
       
    80 	test.Title();
       
    81 	test.Start(_L("Start...\n"));
       
    82 
       
    83 	RTimer t;
       
    84 	TInt i;
       
    85 	TInt r;
       
    86 	TUint32 c1=TIMECOUNT();
       
    87 	for (i=0; i<1000; i++)
       
    88 		{
       
    89 		r=t.CreateLocal();
       
    90 		if (r!=KErrNone)
       
    91 			test(0);
       
    92 		t.Close();
       
    93 		}
       
    94 	TUint32 c2=TIMECOUNT();
       
    95 	test.Printf(_L("Create and close 1000 timers takes %d ms\n"),TIMEDIFFMS(c1,c2));
       
    96 
       
    97 	RThread thrd;
       
    98 	r=thrd.Create(_L("DodgyThread"),ThreadFunction,4096,1024,8192,NULL);
       
    99 	test(r==KErrNone);
       
   100 	test.Printf(_L("DodgyThread created\n"));
       
   101 	thrd.SetPriority(EPriorityMore);
       
   102 	thrd.Resume();
       
   103 	test.Printf(_L("DodgyThread resumed\n"));
       
   104 	c1=TIMECOUNT();
       
   105 	for (i=0; i<100000; i++)
       
   106 		{
       
   107 		thrd.Suspend();
       
   108 		}
       
   109 	c2=TIMECOUNT();
       
   110 	test.Printf(_L("Suspend 100000 times takes %d ms\n"),TIMEDIFFMS(c1,c2));
       
   111 	thrd.Kill(0);
       
   112 	thrd.Close();
       
   113 	test.Printf(_L("DodgyThread killed\n"));
       
   114 
       
   115 	c1=TIMECOUNT();
       
   116 	for (i=0; i<100000; i++)
       
   117 		{
       
   118 		DummyKernMsg();
       
   119 		}
       
   120 	c2=TIMECOUNT();
       
   121 	test.Printf(_L("100000 kernel messages take %d ms\n"),TIMEDIFFMS(c1,c2));
       
   122 
       
   123 	test.Next(_L("Create thread test"));
       
   124 	r=thrd.Create(_L("Creator"),TestThreadCreate,4096,NULL,NULL);
       
   125 	test(r==KErrNone);
       
   126 	thrd.SetPriority(EPriorityLess);
       
   127 	RThread().SetPriority(EPriorityMuchMore);
       
   128 	TRequestStatus s;
       
   129 	thrd.Logon(s);
       
   130 	thrd.Resume();
       
   131 	Count=0;
       
   132 	Stop=EFalse;
       
   133 	c1=TIMECOUNT();
       
   134 	User::After(1000000);
       
   135 	Stop=ETrue;
       
   136 	User::WaitForRequest(s);
       
   137 	c2=TIMECOUNT();
       
   138 	test(thrd.ExitType()==EExitKill);
       
   139 	test(thrd.ExitReason()==0);
       
   140 	thrd.Close();
       
   141 	test.Printf(_L("%d threads created and stopped in %dms\n"),Count,TIMEDIFFMS(c1,c2));
       
   142 
       
   143 	test.End();
       
   144 	return 0;
       
   145 	}
       
   146