os/persistentdata/persistentstorage/dbms/pcdbms/tdbms/src/t_bench.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 1998-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 //
    15 
    16 #include <d32dbms.h>
    17 #include <s32file.h>
    18 #include <e32test.h>
    19 #include <e32math.h>
    20 #ifndef __TOOLS2__	// we aren't interested in timings for tools2
    21 #include <e32svr.h>
    22 #include <hal.h>
    23 #endif
    24 
    25 #include "crccheck.h"
    26 
    27 #undef __UHEAP_MARK
    28 #define __UHEAP_MARK
    29 #undef __UHEAP_MARKEND
    30 #define __UHEAP_MARKEND
    31 
    32 LOCAL_D TDBMS_CRCChecks TheCrcChecker;
    33 
    34 #ifndef __linux__ //No CRC test on LINUX
    35 #ifdef __TOOLS2__
    36 const TPtrC	KCrcRecord=_L("\\epoc32\\winscw\\c\\dbms-tst\\T_BENCH.CRC");
    37 #else
    38 const TPtrC	KCrcRecord=_L("C:\\dbms-tst\\T_BENCH.CRC");
    39 #endif
    40 #endif
    41 
    42 // MSVC++ up to 5.0 has problems with expanding inline functions
    43 // This disables the mad warnings for the whole project
    44 #if defined(NDEBUG) && defined(__VC32__) && _MSC_VER<=1100
    45 #pragma warning(disable : 4710)			// function not expanded. MSVC 5.0 is stupid
    46 #endif
    47 
    48 #ifndef __TOOLS2__
    49 class TTimer
    50 	{
    51 public:
    52 	void Start();
    53 	TReal Stop() const;
    54 private:
    55 	TUint iTicks;
    56 	};
    57 #endif
    58 
    59 LOCAL_D RTest test(_L("T_BENCH"));
    60 LOCAL_D CTrapCleanup* TheTrapCleanup;
    61 LOCAL_D RDbNamedDatabase TheDatabase;
    62 LOCAL_D RDbView TheView;
    63 LOCAL_D RFs TheFs;
    64 
    65 const TInt KTestCleanupStack=0x20;
    66 //T_BENCH file shall not be deleted at the end of the test! It will be used by T_COMP test.
    67 
    68 #ifdef __TOOLS2__
    69 const TPtrC KTestDatabase=_L(".\\dbms-tst\\T_BENCH.DB");
    70 #else
    71 const TPtrC KTestDatabase=_L("C:\\dbms-tst\\T_BENCH.DB");
    72 #endif
    73 const TPtrC KTableName=_S("Test");
    74 const TPtrC KColCluster=_S("Cluster");
    75 const TPtrC KColXcluster=_S("xCluster");
    76 const TPtrC KColRandom=_S("Random");
    77 const TPtrC KColXrandom=_S("xRandom");
    78 const TInt KRecords=2000;
    79 
    80 #ifndef __TOOLS2__
    81 static TTimer TheTimer;
    82 
    83 void TTimer::Start()
    84 	{
    85 	iTicks=User::FastCounter();
    86 	}
    87 
    88 TReal TTimer::Stop() const
    89 	{
    90 	TUint ticks = User::FastCounter() - iTicks;
    91 	TInt freq = 0;
    92 	test(HAL::Get(HAL::EFastCounterFrequency, freq) == KErrNone);
    93 	const TInt KMicroSecIn1Sec = 1000000;
    94 	const TInt KMsIn1Sec = 1000;
    95 	double v = ((double)ticks * KMicroSecIn1Sec) / (double)freq; TInt v2 = (TInt)v;
    96 	return v2 / KMsIn1Sec;
    97 	}
    98 #endif
    99 
   100 LOCAL_C void CloseDatabase()
   101 	{
   102 	TheDatabase.Close();
   103 	TheCrcChecker.GenerateCrcL(KTestDatabase);
   104 	}
   105 
   106 /**
   107 Create the database: keep the code 050 compatible
   108 
   109 @SYMTestCaseID          SYSLIB-DBMS-CT-0577
   110 @SYMTestCaseDesc        Benchmark Tests. Creation of a local Database test
   111 @SYMTestPriority        Medium
   112 @SYMTestActions        	Attempt to test RDbNamedDatabase::CreateTable(),RDbNamedDatabase::CreateIndex(),
   113 						RDbNamedDatabase::Compact(),RDbView::Prepare() functions 
   114 @SYMTestExpectedResults Test must not fail
   115 @SYMREQ                 REQ0000
   116 */
   117 LOCAL_C void CreateDatabase()
   118 	{
   119 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0577 "));
   120 	User::LeaveIfError(TheDatabase.Replace(TheFs,KTestDatabase));
   121 	CDbColSet& set=*CDbColSet::NewLC();
   122 	TDbCol col(KColCluster,EDbColInt32);
   123 	col.iAttributes=col.ENotNull;
   124 	set.AddL(col);
   125 	col.iName=KColXcluster;
   126 	set.AddL(col);
   127 	col.iName=KColRandom;
   128 	set.AddL(col);
   129 	col.iName=KColXrandom;
   130 	set.AddL(col);
   131 	TInt r=TheDatabase.CreateTable(KTableName,set);
   132 	test (r==KErrNone);
   133 	CleanupStack::PopAndDestroy();
   134 #ifndef __TOOLS2__
   135 	TheTimer.Start();
   136 #endif
   137 	r=TheView.Prepare(TheDatabase,_L("select * from test"),TheView.EInsertOnly);
   138 	TheDatabase.Begin();
   139 	test (r==KErrNone);
   140 	TInt jj=0;
   141 	for (TInt ii=0;ii<KRecords;++ii)
   142 		{
   143 		TheView.InsertL();
   144 		jj=(jj+23);
   145 		if (jj>=KRecords)
   146 			jj-=KRecords;
   147 		TheView.SetColL(1,ii);
   148 		TheView.SetColL(2,ii);
   149 		TheView.SetColL(3,jj);
   150 		TheView.SetColL(4,jj);
   151 		TheView.PutL();
   152 		}
   153 	r=TheDatabase.Commit();
   154 	test (r==KErrNone);
   155 	TheView.Close();
   156 #ifndef __TOOLS2__
   157 	test.Printf(_L("Build table: %7.1f ms\n"),TheTimer.Stop());
   158 	TheTimer.Start();
   159 #endif
   160 	CDbKey& key=*CDbKey::NewLC();
   161 	key.AddL(KColXcluster);
   162 	key.MakeUnique();
   163 	r=TheDatabase.CreateIndex(KColXcluster,KTableName,key);
   164 	test (r==KErrNone);
   165 #ifndef __TOOLS2__
   166 	test.Printf(_L("Cluster index: %7.1f ms\n"),TheTimer.Stop());
   167 	TheTimer.Start();
   168 #endif
   169 	key.Clear();
   170 	key.AddL(KColXrandom);
   171 	r=TheDatabase.CreateIndex(KColXrandom,KTableName,key);
   172 	test (r==KErrNone);
   173 	CleanupStack::PopAndDestroy();
   174 #ifndef __TOOLS2__
   175 	test.Printf(_L("Random index: %7.1f ms\n"),TheTimer.Stop());
   176 	TheTimer.Start();
   177 #endif
   178 	r = TheDatabase.Compact();
   179 #ifndef __TOOLS2__
   180 	test.Printf(_L("Compact: %7.1f ms\n"),TheTimer.Stop());
   181 #endif
   182 	test (r == KErrNone);
   183 	}
   184 
   185 LOCAL_C TReal Evaluate(const TDesC& aSql)
   186 	{
   187 	TInt m=1;
   188 	for (;;)
   189 		{
   190 #ifndef __TOOLS2__
   191 		TheTimer.Start();
   192 #endif
   193 		for (TInt ii=0;ii<m;++ii)
   194 			{
   195 			TInt r=TheView.Prepare(TheDatabase,aSql,KDbUnlimitedWindow,TheView.EReadOnly);
   196 			if (r<0)
   197 				return r;
   198 			r=TheView.EvaluateAll();
   199 			test (r==KErrNone);
   200 			TheView.Close();
   201 			}
   202 #ifndef __TOOLS2__
   203 		TReal t=TheTimer.Stop();
   204 		if (t>=100.0)
   205 			return t/m;
   206 		m*=4;
   207 #else
   208 		// We aren't interested in timings for tools2.
   209 		return(1.0/m); // is this right?
   210 #endif
   211 		}
   212 	}
   213 
   214 struct TTest
   215 	{
   216 	const TText* iName;
   217 	const TText* iQuery;
   218 	};
   219 const TTest KQuery[]=
   220 	{
   221 	{_S("project"),_S("select cluster,xcluster,random,xrandom from test")},
   222 	{_S("restrict 1"),_S("select * from test where cluster=0")},
   223 	{_S("restrict 2"),_S("select * from test where xrandom=0")},
   224 	{_S("restrict 3"),_S("select * from test where xcluster<500 and xrandom <500")},
   225 	{_S("order 1"),_S("select * from test order by xrandom")},
   226 	{_S("order 2"),_S("select * from test order by cluster")},
   227 	{_S("all 1"),_S("select * from test where random<500 order by xrandom")},
   228 	{_S("all 2"),_S("select * from test where xcluster<500 order by xrandom")},
   229 	{_S("all 3"),_S("select * from test where xcluster<500 order by xcluster")},
   230 	{_S("all 4"),_S("select * from test where xcluster<500 and xrandom<200 order by xcluster")}
   231 	};
   232 
   233 /**
   234 @SYMTestCaseID          SYSLIB-DBMS-CT-0578
   235 @SYMTestCaseDesc        Benchmark Test.Querying a local Database Test
   236 @SYMTestPriority        Medium
   237 @SYMTestActions        	Evaluate SELECT queries on the created database
   238 @SYMTestExpectedResults Test must not fail
   239 @SYMREQ                 REQ0000
   240 */
   241 LOCAL_C void Queries()
   242 	{
   243 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0578 "));
   244 	for (TUint ii=0;ii<sizeof(KQuery)/sizeof(KQuery[0]);++ii)
   245 		{
   246 		test.Printf(_L("%15s: "),KQuery[ii].iName);
   247 		TReal t=Evaluate(TPtrC(KQuery[ii].iQuery));
   248 		if (t<0.0)
   249 			test.Printf(_L("-\n"));
   250 		else
   251 			test.Printf(_L("%7.1f ms\n"),t);
   252 		}
   253 	}
   254 
   255 //
   256 // Benchmark tests
   257 //
   258 LOCAL_C void BenchTest()
   259 	{
   260 	CreateDatabase();
   261 	Queries();
   262 	CloseDatabase();
   263 	}
   264 
   265 //
   266 // Prepare the test directory.
   267 //
   268 LOCAL_C void setupTestDirectory()
   269     {
   270 	TInt r=TheFs.Connect();
   271 	test(r==KErrNone);
   272 //
   273 #if 0
   274 	TDriveList drives;
   275 	TheFs.DriveList(drives);
   276 	if (drives[EDriveK] == KDriveAbsent)
   277 		{
   278 		TInt r = TheFs.AddFileSystem(_L("ELFFS"));
   279 		test (r == KErrNone);
   280 		r = TheFs.MountFileSystem(_L("Lffs"),EDriveK);
   281 		if (r == KErrCorrupt || r == KErrNotReady) 
   282 			{
   283 			RFormat format;
   284 			TInt    count;
   285 			r = format.Open(TheFs, _L("K:\\"), EHighDensity, count);
   286 			test (r == KErrNone);
   287 			while (count)
   288 				format.Next(count);
   289 			format.Close();
   290     		}
   291 		else
   292 			test (r == KErrNone);
   293 		}
   294 #endif
   295 //
   296 	r=TheFs.MkDir(KTestDatabase);
   297 	test(r==KErrNone || r==KErrAlreadyExists);
   298 	}
   299 
   300 //
   301 // Initialise the cleanup stack.
   302 //
   303 LOCAL_C void setupCleanup()
   304     {
   305 	TheTrapCleanup=CTrapCleanup::New();
   306 	test(TheTrapCleanup!=NULL);
   307 	TRAPD(r,\
   308 		{\
   309 		for (TInt i=KTestCleanupStack;i>0;i--)\
   310 			CleanupStack::PushL((TAny*)0);\
   311 		CleanupStack::Pop(KTestCleanupStack);\
   312 		});
   313 	test(r==KErrNone);
   314 	}
   315 
   316 //
   317 // entry point
   318 //
   319 GLDEF_C TInt E32Main()
   320     {
   321 	test.Title();
   322 	setupTestDirectory();
   323 	setupCleanup();
   324 	__UHEAP_MARK;
   325 
   326 	test.Start(_L("Benchmarking..."));
   327 	TRAPD(r,BenchTest();)
   328 	test(r==KErrNone);
   329 
   330 #ifndef __linux__
   331 	TInt err;
   332 #ifndef __TOOLS2__
   333 	TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
   334 	test(err==KErrNone);
   335 	test(lc==KErrNone);
   336 #else
   337 	TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
   338 	TPtrC errmsg;
   339 	TheCrcChecker.ErrorReportL(err, errmsg);
   340 	RDebug::Print(errmsg);
   341 	test(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
   342 #endif // TOOLS2
   343 #endif // linux
   344 
   345 	test.End();
   346 
   347 	__UHEAP_MARKEND;
   348 	delete TheTrapCleanup;
   349 
   350 	//T_BENCH.DB cannot be deleted here, because it is used by T_COMP test!
   351 
   352 	TheFs.Close();
   353 	test.Close();
   354 	return 0;
   355     }