os/persistentdata/persistentstorage/dbms/tdbms/t_dbbench.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/dbms/tdbms/t_dbbench.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,294 @@
     1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <d32dbms.h>
    1.20 +#include <s32file.h>
    1.21 +#include <e32test.h>
    1.22 +#include <e32math.h>
    1.23 +#include <e32svr.h>
    1.24 +#include <hal.h>
    1.25 +#include <d32dbmsconstants.h>
    1.26 +
    1.27 +// MSVC++ up to 5.0 has problems with expanding inline functions
    1.28 +// This disables the mad warnings for the whole project
    1.29 +#if defined(NDEBUG) && defined(__VC32__) && _MSC_VER<=1100
    1.30 +#pragma warning(disable : 4710)			// function not expanded. MSVC 5.0 is stupid
    1.31 +#endif
    1.32 +
    1.33 +class TTimer
    1.34 +	{
    1.35 +public:
    1.36 +	void Start();
    1.37 +	TReal Stop() const;
    1.38 +private:
    1.39 +	TUint iTicks;
    1.40 +	};
    1.41 +
    1.42 +LOCAL_D RTest test(_L("t_dbbench"));
    1.43 +LOCAL_D CTrapCleanup* TheTrapCleanup;
    1.44 +LOCAL_D RDbNamedDatabase TheDatabase;
    1.45 +LOCAL_D RDbView TheView;
    1.46 +LOCAL_D RFs TheFs;
    1.47 +
    1.48 +const TInt KTestCleanupStack=0x20;
    1.49 +//T_BENCH file shall not be deleted at the end of the test! It will be used by T_COMP test.
    1.50 +const TPtrC KTestDatabase=_S("\\DBMS-TST\\T_BENCH.DB");
    1.51 +const TPtrC KTableName=_S("Test");
    1.52 +const TPtrC KColCluster=_S("Cluster");
    1.53 +const TPtrC KColXcluster=_S("xCluster");
    1.54 +const TPtrC KColRandom=_S("Random");
    1.55 +const TPtrC KColXrandom=_S("xRandom");
    1.56 +const TInt KRecords=2000;
    1.57 +
    1.58 +static TTimer TheTimer;
    1.59 +
    1.60 +void TTimer::Start()
    1.61 +	{
    1.62 +	iTicks=User::FastCounter();
    1.63 +	}
    1.64 +
    1.65 +TReal TTimer::Stop() const
    1.66 +	{
    1.67 +	TUint ticks = User::FastCounter() - iTicks;
    1.68 +	TInt freq = 0;
    1.69 +	test(HAL::Get(HAL::EFastCounterFrequency, freq) == KErrNone);
    1.70 +	const TInt KMicroSecIn1Sec = 1000000;
    1.71 +	const TInt KMsIn1Sec = 1000;
    1.72 +	double v = ((double)ticks * KMicroSecIn1Sec) / (double)freq; TInt v2 = (TInt)v;
    1.73 +	return v2 / KMsIn1Sec;
    1.74 +	}
    1.75 +
    1.76 +LOCAL_C void CloseDatabase()
    1.77 +	{
    1.78 +	TheDatabase.Close();
    1.79 +	}
    1.80 +
    1.81 +/**
    1.82 +Create the database: keep the code 050 compatible
    1.83 +
    1.84 +@SYMTestCaseID          SYSLIB-DBMS-CT-0577
    1.85 +@SYMTestCaseDesc        Benchmark Tests. Creation of a local Database test
    1.86 +@SYMTestPriority        Medium
    1.87 +@SYMTestActions        	Attempt to test RDbNamedDatabase::CreateTable(),RDbNamedDatabase::CreateIndex(),
    1.88 +						RDbNamedDatabase::Compact(),RDbView::Prepare() functions
    1.89 +@SYMTestExpectedResults Test must not fail
    1.90 +@SYMREQ                 REQ0000
    1.91 +*/
    1.92 +LOCAL_C void CreateDatabaseL()
    1.93 +	{
    1.94 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0577 "));
    1.95 +	User::LeaveIfError(TheDatabase.Replace(TheFs,KTestDatabase));
    1.96 +	CDbColSet& set=*CDbColSet::NewLC();
    1.97 +	TDbCol col(KColCluster,EDbColInt32);
    1.98 +	col.iAttributes=col.ENotNull;
    1.99 +	set.AddL(col);
   1.100 +	col.iName=KColXcluster;
   1.101 +	set.AddL(col);
   1.102 +	col.iName=KColRandom;
   1.103 +	set.AddL(col);
   1.104 +	col.iName=KColXrandom;
   1.105 +	set.AddL(col);
   1.106 +	TInt r=TheDatabase.CreateTable(KTableName,set);
   1.107 +	test (r==KErrNone);
   1.108 +	CleanupStack::PopAndDestroy();
   1.109 +	TheTimer.Start();
   1.110 +	r=TheView.Prepare(TheDatabase,_L("select * from test"),TheView.EInsertOnly);
   1.111 +	TheDatabase.Begin();
   1.112 +	test (r==KErrNone);
   1.113 +	TInt jj=0;
   1.114 +	for (TInt ii=0;ii<KRecords;++ii)
   1.115 +		{
   1.116 +		TheView.InsertL();
   1.117 +		jj=(jj+23);
   1.118 +		if (jj>=KRecords)
   1.119 +			jj-=KRecords;
   1.120 +		TheView.SetColL(1,ii);
   1.121 +		TheView.SetColL(2,ii);
   1.122 +		TheView.SetColL(3,jj);
   1.123 +		TheView.SetColL(4,jj);
   1.124 +		TheView.PutL();
   1.125 +		}
   1.126 +	r=TheDatabase.Commit();
   1.127 +	test (r==KErrNone);
   1.128 +	TheView.Close();
   1.129 +	test.Printf(_L("Build table: %7.1f ms\n"),TheTimer.Stop());
   1.130 +	TheTimer.Start();
   1.131 +	CDbKey& key=*CDbKey::NewLC();
   1.132 +	key.AddL(KColXcluster);
   1.133 +	key.MakeUnique();
   1.134 +	r=TheDatabase.CreateIndex(KColXcluster,KTableName,key);
   1.135 +	test (r==KErrNone);
   1.136 +	test.Printf(_L("Cluster index: %7.1f ms\n"),TheTimer.Stop());
   1.137 +	TheTimer.Start();
   1.138 +	key.Clear();
   1.139 +	key.AddL(KColXrandom);
   1.140 +	r=TheDatabase.CreateIndex(KColXrandom,KTableName,key);
   1.141 +	test (r==KErrNone);
   1.142 +	CleanupStack::PopAndDestroy();
   1.143 +	test.Printf(_L("Random index: %7.1f ms\n"),TheTimer.Stop());
   1.144 +	TheTimer.Start();
   1.145 +	r = TheDatabase.Compact();
   1.146 +	test.Printf(_L("Compact: %7.1f ms\n"),TheTimer.Stop());
   1.147 +	test (r == KErrNone);
   1.148 +	}
   1.149 +
   1.150 +LOCAL_C TReal Evaluate(const TDesC& aSql)
   1.151 +	{
   1.152 +	TInt m=1;
   1.153 +	for (;;)
   1.154 +		{
   1.155 +		TheTimer.Start();
   1.156 +		for (TInt ii=0;ii<m;++ii)
   1.157 +			{
   1.158 +			TInt r=TheView.Prepare(TheDatabase,aSql,KDbUnlimitedWindow,TheView.EReadOnly);
   1.159 +			if (r<0)
   1.160 +				return r;
   1.161 +			r=TheView.EvaluateAll();
   1.162 +			test (r==KErrNone);
   1.163 +			TheView.Close();
   1.164 +			}
   1.165 +		TReal t=TheTimer.Stop();
   1.166 +		if (t>=100.0)
   1.167 +			return t/m;
   1.168 +		m*=4;
   1.169 +		}
   1.170 +	}
   1.171 +
   1.172 +struct TTest
   1.173 +	{
   1.174 +	const TText* iName;
   1.175 +	const TText* iQuery;
   1.176 +	};
   1.177 +const TTest KQuery[]=
   1.178 +	{
   1.179 +	{_S("project"),_S("select cluster,xcluster,random,xrandom from test")},
   1.180 +	{_S("restrict 1"),_S("select * from test where cluster=0")},
   1.181 +	{_S("restrict 2"),_S("select * from test where xrandom=0")},
   1.182 +	{_S("restrict 3"),_S("select * from test where xcluster<500 and xrandom <500")},
   1.183 +	{_S("order 1"),_S("select * from test order by xrandom")},
   1.184 +	{_S("order 2"),_S("select * from test order by cluster")},
   1.185 +	{_S("all 1"),_S("select * from test where random<500 order by xrandom")},
   1.186 +	{_S("all 2"),_S("select * from test where xcluster<500 order by xrandom")},
   1.187 +	{_S("all 3"),_S("select * from test where xcluster<500 order by xcluster")},
   1.188 +	{_S("all 4"),_S("select * from test where xcluster<500 and xrandom<200 order by xcluster")}
   1.189 +	};
   1.190 +
   1.191 +/**
   1.192 +@SYMTestCaseID          SYSLIB-DBMS-CT-0578
   1.193 +@SYMTestCaseDesc        Benchmark Test.Querying a local Database Test
   1.194 +@SYMTestPriority        Medium
   1.195 +@SYMTestActions        	Evaluate SELECT queries on the created database
   1.196 +@SYMTestExpectedResults Test must not fail
   1.197 +@SYMREQ                 REQ0000
   1.198 +*/
   1.199 +LOCAL_C void Queries()
   1.200 +	{
   1.201 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0578 "));
   1.202 +	for (TUint ii=0;ii<sizeof(KQuery)/sizeof(KQuery[0]);++ii)
   1.203 +		{
   1.204 +		test.Printf(_L("%15s: "),KQuery[ii].iName);
   1.205 +		TReal t=Evaluate(TPtrC(KQuery[ii].iQuery));
   1.206 +		if (t<0.0)
   1.207 +			test.Printf(_L("-\n"));
   1.208 +		else
   1.209 +			test.Printf(_L("%7.1f ms\n"),t);
   1.210 +		}
   1.211 +	}
   1.212 +
   1.213 +//
   1.214 +// Benchmark tests
   1.215 +//
   1.216 +LOCAL_C void BenchTestL()
   1.217 +	{
   1.218 +	CreateDatabaseL();
   1.219 +	Queries();
   1.220 +	CloseDatabase();
   1.221 +	}
   1.222 +
   1.223 +//
   1.224 +// Prepare the test directory.
   1.225 +//
   1.226 +LOCAL_C void setupTestDirectory()
   1.227 +    {
   1.228 +	TInt r=TheFs.Connect();
   1.229 +	test(r==KErrNone);
   1.230 +//
   1.231 +#if 0
   1.232 +	TDriveList drives;
   1.233 +	TheFs.DriveList(drives);
   1.234 +	if (drives[EDriveK] == KDriveAbsent)
   1.235 +		{
   1.236 +		TInt r = TheFs.AddFileSystem(_L("ELFFS"));
   1.237 +		test (r == KErrNone);
   1.238 +		r = TheFs.MountFileSystem(_L("Lffs"),EDriveK);
   1.239 +		if (r == KErrCorrupt || r == KErrNotReady)
   1.240 +			{
   1.241 +			RFormat format;
   1.242 +			TInt    count;
   1.243 +			r = format.Open(TheFs, _L("K:\\"), EHighDensity, count);
   1.244 +			test (r == KErrNone);
   1.245 +			while (count)
   1.246 +				format.Next(count);
   1.247 +			format.Close();
   1.248 +    		}
   1.249 +		else
   1.250 +			test (r == KErrNone);
   1.251 +		}
   1.252 +#endif
   1.253 +//
   1.254 +	r=TheFs.MkDir(KTestDatabase);
   1.255 +	test(r==KErrNone || r==KErrAlreadyExists);
   1.256 +	}
   1.257 +
   1.258 +//
   1.259 +// Initialise the cleanup stack.
   1.260 +//
   1.261 +LOCAL_C void setupCleanup()
   1.262 +    {
   1.263 +	TheTrapCleanup=CTrapCleanup::New();
   1.264 +	test(TheTrapCleanup!=NULL);
   1.265 +	TRAPD(r,\
   1.266 +		{\
   1.267 +		for (TInt i=KTestCleanupStack;i>0;i--)\
   1.268 +			CleanupStack::PushL((TAny*)0);\
   1.269 +		CleanupStack::Pop(KTestCleanupStack);\
   1.270 +		});
   1.271 +	test(r==KErrNone);
   1.272 +	}
   1.273 +
   1.274 +//
   1.275 +// entry point
   1.276 +//
   1.277 +GLDEF_C TInt E32Main()
   1.278 +    {
   1.279 +	test.Title();
   1.280 +	setupTestDirectory();
   1.281 +	setupCleanup();
   1.282 +	__UHEAP_MARK;
   1.283 +//
   1.284 +	test.Start(_L("Benchmarking..."));
   1.285 +	TRAPD(r,BenchTestL());
   1.286 +	test(r==KErrNone);
   1.287 +	test.End();
   1.288 +//
   1.289 +	__UHEAP_MARKEND;
   1.290 +	delete TheTrapCleanup;
   1.291 +
   1.292 +	//T_BENCH.DB cannot be deleted here, because it is used by T_COMP test!
   1.293 +
   1.294 +	TheFs.Close();
   1.295 +	test.Close();
   1.296 +	return 0;
   1.297 +    }