os/persistentdata/persistentstorage/dbms/tdbms/t_dbperf1.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_dbperf1.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,751 @@
     1.4 +// Copyright (c) 2006-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 <e32test.h>
    1.20 +#include <e32math.h>
    1.21 +#include <bautils.h>
    1.22 +#include <hal.h>
    1.23 +#include <d32dbms.h>
    1.24 +
    1.25 +///////////////////////////////////////////////////////////////////////////////////////
    1.26 +
    1.27 +RTest TheTest(_L("t_dbperf1"));
    1.28 +RFs   TheFs;
    1.29 +RDbs  TheDbs;
    1.30 +
    1.31 +TFileName TheNonSecureDbName;
    1.32 +TFileName TheNonSecureDbName2;
    1.33 +_LIT(KFillDbScript, "z:\\test\\t_dbperf1.sql");
    1.34 +_LIT(KUpdateSql, "UPDATE IDENTITYTABLE SET CM_FIRSTNAME='%S%d',CM_LASTNAME='%S%d',CM_COMPANYNAME='%S%d' WHERE PARENT_CMID=%d");
    1.35 +_LIT(KSelectSql, "SELECT CM_FIRSTNAME, CM_LASTNAME, CM_COMPANYNAME FROM IDENTITYTABLE WHERE PARENT_CMID > 50");
    1.36 +_LIT(KDeleteSql, "DELETE FROM IDENTITYTABLE WHERE PARENT_CMID > 50");
    1.37 +_LIT(KFirstName, "FirstName-");
    1.38 +_LIT(KLastName, "LastName-");
    1.39 +_LIT(KCompanyName, "CompanyName-");
    1.40 +
    1.41 +const TInt KTestTecordCount = 1000;
    1.42 +
    1.43 +enum TTestType 
    1.44 +	{
    1.45 +	EClientDbTest,		
    1.46 +	EServerDbTest
    1.47 +	};
    1.48 +	
    1.49 +enum TCompactDbOption
    1.50 +	{
    1.51 +	ENoCompaction,
    1.52 +	ECompactAfterCommit,
    1.53 +	ECompactAfterEvery10Rec,
    1.54 +	ECompactAfterEveryUpd,
    1.55 +	ECompactAtEnd
    1.56 +	};
    1.57 +	
    1.58 +///////////////////////////////////////////////////////////////////////////////////////
    1.59 +
    1.60 +void TestEnvDestroy()
    1.61 +	{
    1.62 +	TheDbs.Close();
    1.63 +	TheFs.Delete(TheNonSecureDbName2);
    1.64 +	TheFs.Delete(TheNonSecureDbName);
    1.65 +	TheFs.Close();
    1.66 +	}
    1.67 +
    1.68 +///////////////////////////////////////////////////////////////////////////////////////
    1.69 +///////////////////////////////////////////////////////////////////////////////////////
    1.70 +//Test macros and functions
    1.71 +void Check1(TInt aValue, TInt aLine)
    1.72 +	{
    1.73 +	if(!aValue)
    1.74 +		{
    1.75 +		TestEnvDestroy();
    1.76 +		TheTest.Printf(_L("*** Line %d\r\n"), aLine);
    1.77 +		TheTest(EFalse, aLine);
    1.78 +		}
    1.79 +	}
    1.80 +void Check2(TInt aValue, TInt aExpected, TInt aLine)
    1.81 +	{
    1.82 +	if(aValue != aExpected)
    1.83 +		{
    1.84 +		TestEnvDestroy();
    1.85 +		TheTest.Printf(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue);
    1.86 +		TheTest(EFalse, aLine);
    1.87 +		}
    1.88 +	}
    1.89 +#define TEST(arg) ::Check1((arg), __LINE__)
    1.90 +#define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__)
    1.91 +
    1.92 +///////////////////////////////////////////////////////////////////////////////////////
    1.93 +
    1.94 +void TestEnvInit()
    1.95 +    {
    1.96 +	TInt err = TheFs.Connect();
    1.97 +	TEST2(err, KErrNone);
    1.98 +
    1.99 +	err = TheFs.MkDir(TheNonSecureDbName);
   1.100 +	TEST(err == KErrNone || err == KErrAlreadyExists);
   1.101 +	
   1.102 +	err = TheDbs.Connect();
   1.103 +	TEST2(err, KErrNone);
   1.104 +	}
   1.105 +
   1.106 +//Reads a SQL file and returns the file content as HBUFC string.
   1.107 +//The caller is responsible for destroying the returned HBufC object.
   1.108 +HBufC* ReadSqlScript(const TDesC& aSqlFileName)
   1.109 +	{
   1.110 +	RFile file;
   1.111 +	TEST2(file.Open(TheFs, aSqlFileName, EFileRead), KErrNone);
   1.112 +	
   1.113 +	TInt size = 0;
   1.114 +	TEST2(file.Size(size), KErrNone);
   1.115 +	
   1.116 +	HBufC8* sql = HBufC8::New(size);
   1.117 +	TEST(sql != NULL);
   1.118 +	
   1.119 +	TPtr8 ptr = sql->Des();
   1.120 +	TEST2(file.Read(ptr, size), KErrNone);
   1.121 +
   1.122 +	file.Close();
   1.123 +	
   1.124 +	HBufC* sql2 = HBufC::New(size);
   1.125 +	TEST(sql2 != NULL);
   1.126 +	sql2->Des().Copy(sql->Des());
   1.127 +	delete sql;
   1.128 +	
   1.129 +	return sql2;
   1.130 +	}
   1.131 +	
   1.132 +//Searches for the next ';' appearance in aSqlScript string and returns a TPtrC object holding
   1.133 +//the SQL statement, terminated with that ';'.
   1.134 +TPtrC GetNextStmt(TPtrC& aSqlScript)
   1.135 +	{
   1.136 +	_LIT(KTerminator, ";\xD");
   1.137 +	TPtrC res(NULL, 0);
   1.138 +	TInt pos = aSqlScript.FindF(KTerminator());
   1.139 +	if(pos >= 0)
   1.140 +		{
   1.141 +		res.Set(aSqlScript.Left(pos));
   1.142 +		pos += 1;
   1.143 +		aSqlScript.Set(aSqlScript.Mid(pos));
   1.144 +		}
   1.145 +	return res;
   1.146 +	}
   1.147 +
   1.148 +//Prints aTime parameter (converted to ms)
   1.149 +void PrintStats(TUint32 aStartTicks, TUint32 aEndTicks)
   1.150 +	{
   1.151 +	static TInt freq = 0;
   1.152 +	if(freq == 0)
   1.153 +		{
   1.154 +		TEST2(HAL::Get(HAL::EFastCounterFrequency, freq), KErrNone);
   1.155 +		}
   1.156 +	TInt64 diffTicks = (TInt64)aEndTicks - (TInt64)aStartTicks;
   1.157 +	if(diffTicks < 0)
   1.158 +		{
   1.159 +		diffTicks = KMaxTUint32 + diffTicks + 1;
   1.160 +		}
   1.161 +	const TInt KMicroSecIn1Sec = 1000000;
   1.162 +	TInt32 us = (diffTicks * KMicroSecIn1Sec) / freq;
   1.163 +	TheTest.Printf(_L("####Execution time: %d ms\r\n"), us / 1000);
   1.164 +	}
   1.165 +	
   1.166 +void PrintFileSize(const TDesC& aFileName)
   1.167 +	{
   1.168 +	RFile file;
   1.169 +	TInt err = file.Open(TheFs, aFileName, EFileRead);
   1.170 +	TEST2(err, KErrNone);
   1.171 +	TInt size = 0;
   1.172 +	err = file.Size(size);
   1.173 +	TEST2(err, KErrNone);
   1.174 +	TheTest.Printf(_L("####FileSize: %d\r\n"), size);
   1.175 +	file.Close();
   1.176 +	}
   1.177 +
   1.178 +void PrintString(const TDesC& aStr)
   1.179 +	{
   1.180 +	TheTest.Printf(_L("Str begin~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n"));
   1.181 +	TPtrC ptr(aStr);
   1.182 +	while(ptr.Length() > 0)
   1.183 +		{
   1.184 +		TPtrC ptr2;
   1.185 +		TInt len = Min(ptr.Length(), 60);
   1.186 +		ptr2.Set(ptr.Left(len));
   1.187 +		TheTest.Printf(_L("%S\r\n"), &ptr2);
   1.188 +		ptr.Set(ptr.Mid(len));
   1.189 +		}
   1.190 +	TheTest.Printf(_L("Str end~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n"));
   1.191 +	}
   1.192 +
   1.193 +//////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.194 +///////     DBMS SERVER performance tests
   1.195 +//////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.196 +
   1.197 +//Creates aDb database schema.
   1.198 +void CreateDbSchemaL(RDbDatabase& aDb)
   1.199 +	{
   1.200 +	//"Contacts" table
   1.201 +	CDbColSet* colSet = CDbColSet::NewLC();
   1.202 +	colSet->AddL(TDbCol(_L("cm_id"), EDbColInt32, TDbCol::ENotNull));
   1.203 +	colSet->AddL(TDbCol(_L("cm_type"), EDbColInt32, TDbCol::ENotNull));
   1.204 +	colSet->AddL(TDbCol(_L("cm_preftemplaterefid"), EDbColInt32));
   1.205 +	colSet->AddL(TDbCol(_L("cm_uidstring"), EDbColText, 244));
   1.206 +	colSet->AddL(TDbCol(_L("cm_last_modified"), EDbColDateTime));
   1.207 +	colSet->AddL(TDbCol(_L("cm_contactcreationdate"), EDbColDateTime));
   1.208 +	colSet->AddL(TDbCol(_L("cm_attributes"), EDbColInt32, TDbCol::ENotNull));
   1.209 +	colSet->AddL(TDbCol(_L("cm_replicationcount"), EDbColInt32, TDbCol::ENotNull));
   1.210 +	colSet->AddL(TDbCol(_L("cm_header"), EDbColLongText));
   1.211 +	colSet->AddL(TDbCol(_L("cm_textblob"), EDbColLongText));
   1.212 +	colSet->AddL(TDbCol(_L("cm_searchabletext"), EDbColLongText));
   1.213 +	TInt err = aDb.CreateTable(_L("contacts"), *colSet);
   1.214 +	TEST2(err, KErrNone);
   1.215 +	CleanupStack::PopAndDestroy(); colSet = NULL;
   1.216 +	//"identitytable" table
   1.217 +	colSet = CDbColSet::NewLC();
   1.218 +	colSet->AddL(TDbCol(_L("parent_cmid"), EDbColInt32, TDbCol::ENotNull));
   1.219 +	colSet->AddL(TDbCol(_L("cm_firstname"), EDbColText, 255));
   1.220 +	colSet->AddL(TDbCol(_L("cm_lastname"), EDbColText, 255));
   1.221 +	colSet->AddL(TDbCol(_L("cm_companyname"), EDbColText, 255));
   1.222 +	colSet->AddL(TDbCol(_L("cm_type"), EDbColInt32));
   1.223 +	colSet->AddL(TDbCol(_L("cm_attributes"), EDbColInt32));
   1.224 +	colSet->AddL(TDbCol(_L("cm_hintfield"), EDbColUint8));
   1.225 +	colSet->AddL(TDbCol(_L("cm_exthintfield"), EDbColUint16));
   1.226 +	colSet->AddL(TDbCol(_L("cm_firstnmprn"), EDbColText, 255));
   1.227 +	colSet->AddL(TDbCol(_L("cm_lastnmprn"), EDbColText, 255));
   1.228 +	colSet->AddL(TDbCol(_L("cm_companynmprn"), EDbColText, 255));
   1.229 +	err = aDb.CreateTable(_L("identitytable"), *colSet);
   1.230 +	TEST2(err, KErrNone);
   1.231 +	CleanupStack::PopAndDestroy(); colSet = NULL;
   1.232 +	//"emailtable" table
   1.233 +	colSet = CDbColSet::NewLC();
   1.234 +	colSet->AddL(TDbCol(_L("emailparent_cmid"), EDbColInt32, TDbCol::ENotNull));
   1.235 +	colSet->AddL(TDbCol(_L("email_fieldid"), EDbColInt32, TDbCol::ENotNull));
   1.236 +	colSet->AddL(TDbCol(_L("emailaddress"), EDbColText, 255));
   1.237 +	err = aDb.CreateTable(_L("emailtable"), *colSet);
   1.238 +	TEST2(err, KErrNone);
   1.239 +	CleanupStack::PopAndDestroy(); colSet = NULL;
   1.240 +	//"phone" table
   1.241 +	colSet = CDbColSet::NewLC();
   1.242 +	colSet->AddL(TDbCol(_L("cm_id"), EDbColInt32, TDbCol::ENotNull));
   1.243 +	colSet->AddL(TDbCol(_L("cm_phonematching"), EDbColInt32, TDbCol::ENotNull));
   1.244 +	colSet->AddL(TDbCol(_L("cm_extendedphonematching"), EDbColInt32, TDbCol::ENotNull));
   1.245 +	err = aDb.CreateTable(_L("phone"), *colSet);
   1.246 +	TEST2(err, KErrNone);
   1.247 +	CleanupStack::PopAndDestroy(); colSet = NULL;
   1.248 +	//"groups" table
   1.249 +	colSet = CDbColSet::NewLC();
   1.250 +	colSet->AddL(TDbCol(_L("cm_id"), EDbColInt32, TDbCol::ENotNull));
   1.251 +	colSet->AddL(TDbCol(_L("cm_members"), EDbColInt32, TDbCol::ENotNull));
   1.252 +	err = aDb.CreateTable(_L("groups"), *colSet);
   1.253 +	TEST2(err, KErrNone);
   1.254 +	CleanupStack::PopAndDestroy(); colSet = NULL;
   1.255 +	//"groups2" table
   1.256 +	colSet = CDbColSet::NewLC();
   1.257 +	colSet->AddL(TDbCol(_L("cm_id"), EDbColInt32, TDbCol::ENotNull));
   1.258 +	colSet->AddL(TDbCol(_L("cm_groupmembers"), EDbColLongText));
   1.259 +	err = aDb.CreateTable(_L("groups2"), *colSet);
   1.260 +	TEST2(err, KErrNone);
   1.261 +	CleanupStack::PopAndDestroy(); colSet = NULL;
   1.262 +	//"sync" table
   1.263 +	colSet = CDbColSet::NewLC();
   1.264 +	colSet->AddL(TDbCol(_L("cm_id"), EDbColInt32, TDbCol::ENotNull));
   1.265 +	colSet->AddL(TDbCol(_L("cm_lastsyncdate"), EDbColDateTime));
   1.266 +	err = aDb.CreateTable(_L("sync"), *colSet);
   1.267 +	TEST2(err, KErrNone);
   1.268 +	CleanupStack::PopAndDestroy(); colSet = NULL;
   1.269 +	//"preferences" table
   1.270 +	colSet = CDbColSet::NewLC();
   1.271 +	colSet->AddL(TDbCol(_L("cm_preffileid"), EDbColInt16));
   1.272 +	colSet->AddL(TDbCol(_L("cm_preftemplateid"), EDbColInt32, TDbCol::ENotNull));
   1.273 +	colSet->AddL(TDbCol(_L("cm_prefowncardid"), EDbColInt32));
   1.274 +	colSet->AddL(TDbCol(_L("cm_prefcardtemplateprefid"), EDbColInt32));
   1.275 +	colSet->AddL(TDbCol(_L("cm_preffilever"), EDbColInt32, TDbCol::ENotNull));
   1.276 +	colSet->AddL(TDbCol(_L("cm_prefcardtemplateid"), EDbColLongText));
   1.277 +	colSet->AddL(TDbCol(_L("cm_prefgroupidlist"), EDbColLongText));
   1.278 +	colSet->AddL(TDbCol(_L("cm_creationdate"), EDbColDateTime));
   1.279 +	colSet->AddL(TDbCol(_L("cm_machineuid"), EDbColInt64));
   1.280 +	colSet->AddL(TDbCol(_L("cm_prefsortorder"), EDbColLongText));
   1.281 +	err = aDb.CreateTable(_L("preferences"), *colSet);
   1.282 +	TEST2(err, KErrNone);
   1.283 +	CleanupStack::PopAndDestroy(); colSet = NULL;
   1.284 +	//////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.285 +	//////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.286 +	//"Contacts" table keys
   1.287 +	CDbKey* key = CDbKey::NewLC();
   1.288 +	key->AddL(_L("cm_id"));
   1.289 +	key->MakeUnique();
   1.290 +	err = aDb.CreateIndex(_L("contacts"), _L("contacts"), *key);
   1.291 +	TEST2(err, KErrNone);
   1.292 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.293 +	key = CDbKey::NewLC();
   1.294 +	key->AddL(_L("cm_preftemplaterefid"));
   1.295 +	err = aDb.CreateIndex(_L("cm_preftemplaterefid"), _L("contacts"), *key);
   1.296 +	TEST2(err, KErrNone);
   1.297 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.298 +	//"identitytable" table keys
   1.299 +	key = CDbKey::NewLC();
   1.300 +	key->AddL(_L("parent_cmid"));
   1.301 +	key->MakeUnique();
   1.302 +	err = aDb.CreateIndex(_L("identitytable"), _L("identitytable"), *key);
   1.303 +	TEST2(err, KErrNone);
   1.304 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.305 +	//"emailtable" table keys
   1.306 +	key = CDbKey::NewLC();
   1.307 +	key->AddL(_L("email_fieldid"));
   1.308 +	key->AddL(_L("emailparent_cmid"));
   1.309 +	key->MakeUnique();
   1.310 +	err = aDb.CreateIndex(_L("emailtable"), _L("emailtable"), *key);
   1.311 +	TEST2(err, KErrNone);
   1.312 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.313 +	key = CDbKey::NewLC();
   1.314 +	key->AddL(_L("emailparent_cmid"));
   1.315 +	err = aDb.CreateIndex(_L("emailparent_cmid"), _L("emailtable"), *key);
   1.316 +	TEST2(err, KErrNone);
   1.317 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.318 +	//"phone" table keys
   1.319 +	key = CDbKey::NewLC();
   1.320 +	key->AddL(_L("cm_phonematching"));
   1.321 +	key->AddL(_L("cm_id"));
   1.322 +	key->MakeUnique();
   1.323 +	err = aDb.CreateIndex(_L("phone"), _L("phone"), *key);
   1.324 +	TEST2(err, KErrNone);
   1.325 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.326 +	key = CDbKey::NewLC();
   1.327 +	key->AddL(_L("cm_id"));
   1.328 +	err = aDb.CreateIndex(_L("cm_id"), _L("phone"), *key);
   1.329 +	TEST2(err, KErrNone);
   1.330 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.331 +	//"groups" table keys
   1.332 +	key = CDbKey::NewLC();
   1.333 +	key->AddL(_L("cm_members"));
   1.334 +	key->AddL(_L("cm_id"));
   1.335 +	key->MakeUnique();
   1.336 +	err = aDb.CreateIndex(_L("groups"), _L("groups"), *key);
   1.337 +	TEST2(err, KErrNone);
   1.338 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.339 +	key = CDbKey::NewLC();
   1.340 +	key->AddL(_L("cm_id"));
   1.341 +	err = aDb.CreateIndex(_L("cm_id"), _L("groups"), *key);
   1.342 +	TEST2(err, KErrNone);
   1.343 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.344 +	key = CDbKey::NewLC();
   1.345 +	key->AddL(_L("cm_members"));
   1.346 +	err = aDb.CreateIndex(_L("cm_members"), _L("groups"), *key);
   1.347 +	TEST2(err, KErrNone);
   1.348 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.349 +	//"groups2" table keys
   1.350 +	key = CDbKey::NewLC();
   1.351 +	key->AddL(_L("cm_id"));
   1.352 +	key->MakeUnique();
   1.353 +	err = aDb.CreateIndex(_L("groups2"), _L("groups2"), *key);
   1.354 +	TEST2(err, KErrNone);
   1.355 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.356 +	//"sync" table keys
   1.357 +	key = CDbKey::NewLC();
   1.358 +	key->AddL(_L("cm_id"));
   1.359 +	key->MakeUnique();
   1.360 +	err = aDb.CreateIndex(_L("sync"), _L("sync"), *key);
   1.361 +	TEST2(err, KErrNone);
   1.362 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.363 +	//"preferences" table keys
   1.364 +	key = CDbKey::NewLC();
   1.365 +	key->AddL(_L("cm_preftemplateid"));
   1.366 +	err = aDb.CreateIndex(_L("cm_preftemplateid"), _L("preferences"), *key);
   1.367 +	TEST2(err, KErrNone);
   1.368 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.369 +	key = CDbKey::NewLC();
   1.370 +	key->AddL(_L("cm_prefowncardid"));
   1.371 +	err = aDb.CreateIndex(_L("cm_prefowncardid"), _L("preferences"), *key);
   1.372 +	TEST2(err, KErrNone);
   1.373 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.374 +	key = CDbKey::NewLC();
   1.375 +	key->AddL(_L("cm_prefcardtemplateprefid"));
   1.376 +	err = aDb.CreateIndex(_L("cm_prefcardtemplateprefid"), _L("preferences"), *key);
   1.377 +	TEST2(err, KErrNone);
   1.378 +	CleanupStack::PopAndDestroy(); key = NULL;
   1.379 +	}
   1.380 +
   1.381 +class TDbHelper
   1.382 +	{
   1.383 +public:	
   1.384 +	static void CreateL(const TDesC& aDbName)
   1.385 +		{
   1.386 +		RDbNamedDatabase db;
   1.387 +		TInt err = db.Replace(TheFs, aDbName);
   1.388 +		TEST2(err, KErrNone);
   1.389 +		CreateDbSchemaL(db);
   1.390 +		db.Close();
   1.391 +		}
   1.392 +	static RDbDatabase Open(TTestType aTestType, const TDesC& aDbName)
   1.393 +		{
   1.394 +		RDbNamedDatabase db;
   1.395 +		TInt err = aTestType == EClientDbTest ? db.Open(TheFs, aDbName) : db.Open(TheDbs, aDbName);
   1.396 +		TEST2(err, KErrNone);
   1.397 +		return db;
   1.398 +		}
   1.399 +	};
   1.400 +	
   1.401 +//Executes SQL script
   1.402 +void ExecuteSqlScript(RDbDatabase& aDb, const TDesC& aScriptFileName, TCompactDbOption aCompactDbOpt)
   1.403 +	{
   1.404 +	if(aCompactDbOpt == ECompactAfterCommit)
   1.405 +		{
   1.406 +		TheTest.Printf(_L("===Execute DB script: Compact after COMMIT\r\n"));	
   1.407 +		}
   1.408 +	else if(aCompactDbOpt == ECompactAtEnd)
   1.409 +		{
   1.410 +		TheTest.Printf(_L("===Execute DB script: Compact at end\r\n"));	
   1.411 +		}
   1.412 +	else
   1.413 +		{
   1.414 +		TheTest.Printf(_L("===Execute DB script: No compaction\r\n"));	
   1.415 +		}
   1.416 +	HBufC* fullDbScript = ReadSqlScript(aScriptFileName);
   1.417 +	TUint32 start = User::FastCounter();
   1.418 +	TPtrC ptr(fullDbScript->Des());
   1.419 +	TInt stmtCnt = 0;
   1.420 +	TInt err = aDb.Begin();
   1.421 +	TEST2(err, KErrNone);
   1.422 +	TPtrC sql(GetNextStmt(ptr));
   1.423 +	while(sql.Length() > 0)
   1.424 +		{
   1.425 +		++stmtCnt;
   1.426 +		TInt rc = aDb.Execute(sql);
   1.427 +		if(rc == KErrNoMemory)
   1.428 +			{
   1.429 +			TheTest.Printf(_L("###ERROR 'Out of memory'! The test cannot be completed!\r\n"));
   1.430 +			return;	
   1.431 +			}
   1.432 +		if(rc != 1)	
   1.433 +			{
   1.434 +			PrintString(sql);
   1.435 +			}
   1.436 +		TEST2(rc, 1);
   1.437 +		if((stmtCnt % 200) == 0) //~16 transactions (~3270 statements total)
   1.438 +			{
   1.439 +			err = aDb.Commit();	
   1.440 +			TEST2(err, KErrNone);
   1.441 +			if(aCompactDbOpt == ECompactAfterCommit)
   1.442 +				{
   1.443 +				err = aDb.Compact();
   1.444 +				TEST2(err, KErrNone);
   1.445 +				}
   1.446 +			err = aDb.Begin();
   1.447 +			TEST2(err, KErrNone);
   1.448 +			}
   1.449 +		sql.Set(GetNextStmt(ptr));
   1.450 +		}
   1.451 +	err = aDb.Commit();	
   1.452 +	TEST2(err, KErrNone);
   1.453 +	if(aCompactDbOpt == ECompactAfterCommit || aCompactDbOpt == ECompactAtEnd)
   1.454 +		{
   1.455 +		err = aDb.Compact();
   1.456 +		TEST2(err, KErrNone);
   1.457 +		}
   1.458 +	TUint32 end = User::FastCounter();
   1.459 +	PrintStats(start, end);
   1.460 +	delete fullDbScript;
   1.461 +	TheTest.Printf(_L("###Processed records: %d\r\n"), stmtCnt);
   1.462 +	}
   1.463 +
   1.464 +///////////////////////////////////////////////////////////////////////////////////////
   1.465 +
   1.466 +//"INSERT" test function
   1.467 +void InsertTest(TTestType aTestType, const TDesC& aDbFileName, TCompactDbOption aCompactDbOpt)
   1.468 +	{
   1.469 +	TheTest.Printf(_L("\"Insert\" test\r\n"));
   1.470 +	RDbDatabase db = TDbHelper::Open(aTestType, aDbFileName);
   1.471 +	ExecuteSqlScript(db, KFillDbScript, aCompactDbOpt);
   1.472 +	RDbTable tbl;
   1.473 +	TInt err = tbl.Open(db, _L("CONTACTS"));
   1.474 +	TEST2(err, KErrNone);
   1.475 +	TInt recCnt = 0;
   1.476 +	TRAP(err, recCnt = tbl.CountL());
   1.477 +	TEST2(err, KErrNone);
   1.478 +	TEST2(recCnt, 1001);
   1.479 +	tbl.Close();
   1.480 +	db.Close();	
   1.481 +	}
   1.482 +
   1.483 +//"UPDATE" test function
   1.484 +void UpdateTest(TTestType aTestType, const TDesC& aDbFileName, const TDesC& aUpdateSql, TCompactDbOption aCompactDbOpt)
   1.485 +	{
   1.486 +	if(aCompactDbOpt == ECompactAfterEvery10Rec)
   1.487 +		{
   1.488 +		TheTest.Printf(_L("===Update: Compact after every 10 records\r\n"));	
   1.489 +		}
   1.490 +	else if(aCompactDbOpt == ECompactAfterEveryUpd)
   1.491 +		{
   1.492 +		TheTest.Printf(_L("===Update: Compact after every update\r\n"));	
   1.493 +		}
   1.494 +	else if(aCompactDbOpt == ECompactAtEnd)
   1.495 +		{
   1.496 +		TheTest.Printf(_L("===Update: Compact at end\r\n"));	
   1.497 +		}
   1.498 +	else
   1.499 +		{
   1.500 +		TheTest.Printf(_L("===Update: No compaction\r\n"));	
   1.501 +		}
   1.502 +	TheTest.Printf(_L("\"Update\" test\r\n"));
   1.503 +	RDbDatabase db = TDbHelper::Open(aTestType, aDbFileName);
   1.504 +
   1.505 +	TUint32 start = User::FastCounter();
   1.506 +	for(TInt id=1,updateOpCnt=0;id<=KTestTecordCount;++id,++updateOpCnt)
   1.507 +		{
   1.508 +		TBuf<200> sql;
   1.509 +		sql.Format(aUpdateSql, &KFirstName, id, &KLastName, id, &KCompanyName, id, id);
   1.510 +		TInt rc = db.Execute(sql);
   1.511 +		if(rc != 1)	
   1.512 +			{
   1.513 +			PrintString(sql);
   1.514 +			}
   1.515 +		TEST2(rc, 1);
   1.516 +		if(aCompactDbOpt == ECompactAfterEveryUpd || (aCompactDbOpt == ECompactAfterEvery10Rec && (updateOpCnt % 10) == 0))
   1.517 +			{
   1.518 +			TInt err = db.Compact();
   1.519 +			TEST2(err, KErrNone);
   1.520 +			}
   1.521 +		}
   1.522 +	if(aCompactDbOpt == ECompactAfterEvery10Rec || aCompactDbOpt == ECompactAtEnd)
   1.523 +		{
   1.524 +		TInt err = db.Compact();
   1.525 +		TEST2(err, KErrNone);
   1.526 +		}
   1.527 +	TUint32 end = User::FastCounter();
   1.528 +	PrintStats(start, end);
   1.529 +	db.Close();
   1.530 +	}
   1.531 +
   1.532 +//"SELECT" test function
   1.533 +void SelectTestL(TTestType aTestType, const TDesC& aDbFileName, const TDesC& aSelectSql)
   1.534 +	{
   1.535 +	TheTest.Printf(_L("\"Select\" test\r\n"));
   1.536 +	RDbDatabase db = TDbHelper::Open(aTestType, aDbFileName);
   1.537 +	TDbQuery query(aSelectSql);
   1.538 +	RDbView view;
   1.539 +	TInt err = view.Prepare(db, query, RDbRowSet::EReadOnly);
   1.540 +	TEST2(err, KErrNone);
   1.541 +
   1.542 +	TUint32 start = User::FastCounter();
   1.543 +	err = view.EvaluateAll();
   1.544 +	TEST2(err, KErrNone);
   1.545 +	while(view.NextL())
   1.546 +		{
   1.547 +		view.GetL();
   1.548 +		TPtrC colVal;
   1.549 +		TBuf<20> res;
   1.550 +		colVal.Set(view.ColDes(1));
   1.551 +		res.Copy(colVal);
   1.552 +		TEST(colVal.Length() > 0);
   1.553 +		colVal.Set(view.ColDes(2));
   1.554 +		TEST(colVal.Length() > 0);
   1.555 +		res.Copy(colVal);
   1.556 +		colVal.Set(view.ColDes(3));
   1.557 +		TEST(colVal.Length() > 0);
   1.558 +		res.Copy(colVal);
   1.559 +		}
   1.560 +	TEST(view.AtEnd());
   1.561 +	view.Close();
   1.562 +	TUint32 end = User::FastCounter();
   1.563 +	PrintStats(start, end);
   1.564 +	db.Close();	
   1.565 +	}
   1.566 +
   1.567 +//"DELETE" test function
   1.568 +void DeleteTest(TTestType aTestType, const TDesC& aDbFileName, const TDesC& aDeleteSql, TCompactDbOption aCompactDbOpt)
   1.569 +	{
   1.570 +	if(aCompactDbOpt == ECompactAtEnd)
   1.571 +		{
   1.572 +		TheTest.Printf(_L("===Delete: Compact at end\r\n"));	
   1.573 +		}
   1.574 +	else
   1.575 +		{
   1.576 +		TheTest.Printf(_L("===Delete: No compaction\r\n"));	
   1.577 +		}
   1.578 +	TheTest.Printf(_L("\"Delete\" test\r\n"));
   1.579 +	RDbDatabase db = TDbHelper::Open(aTestType, aDbFileName);
   1.580 +	TUint32 start = User::FastCounter();
   1.581 +	TInt rc = db.Execute(aDeleteSql);
   1.582 +	TEST(rc > 900);
   1.583 +	if(aCompactDbOpt == ECompactAtEnd)
   1.584 +		{
   1.585 +		TInt err = db.Compact();
   1.586 +		TEST2(err, KErrNone);
   1.587 +		}
   1.588 +	TUint32 end = User::FastCounter();
   1.589 +	PrintStats(start, end);
   1.590 +	db.Close();	
   1.591 +	}
   1.592 +
   1.593 +/**
   1.594 +@SYMTestCaseID			PDS-DBMS-CT-4008
   1.595 +@SYMTestCaseDesc		DBMS server performance tests.
   1.596 +						Three test types used: "insert records", "update records" and "select records".
   1.597 +						The tests are executed on non-secure databases.
   1.598 +						Each test executed using UTF16 encoded SQl statements.
   1.599 +						The test results are used for a comparison against the SQL server test results 
   1.600 +						(T_SqlPerformance test).
   1.601 +@SYMTestPriority		High
   1.602 +@SYMTestActions			DBMS server performance tests.
   1.603 +@SYMTestExpectedResults Test must not fail
   1.604 +@SYMREQ					REQ5792
   1.605 +                        REQ5793
   1.606 +*/	
   1.607 +void DoTestsL()
   1.608 +	{
   1.609 +	TheTest.Start(_L(" @SYMTestCaseID:PDS-DBMS-CT-4008 DBMS performance tests"));
   1.610 +	
   1.611 +	CFileMan* fm = CFileMan::NewL(TheFs);
   1.612 +	CleanupStack::PushL(fm);
   1.613 +
   1.614 +	//==================================== CLIENT ==========================================================
   1.615 +	TheTest.Printf(_L("DBMS, insert, UTF16 SQL strings, non-secure db, client, no db compaction\r\n"));
   1.616 +	TDbHelper::CreateL(TheNonSecureDbName);
   1.617 +	InsertTest(EClientDbTest, TheNonSecureDbName, ENoCompaction);
   1.618 +	PrintFileSize(TheNonSecureDbName);
   1.619 +	
   1.620 +	TheTest.Printf(_L("DBMS, insert, UTF16 SQL strings, non-secure db, client, db compaction at end\r\n"));
   1.621 +	TDbHelper::CreateL(TheNonSecureDbName);
   1.622 +	InsertTest(EClientDbTest, TheNonSecureDbName, ECompactAtEnd);
   1.623 +	PrintFileSize(TheNonSecureDbName);
   1.624 +	
   1.625 +	TheTest.Printf(_L("DBMS, insert, UTF16 SQL strings, non-secure db, client, db compaction after commit\r\n"));
   1.626 +	TDbHelper::CreateL(TheNonSecureDbName);
   1.627 +	InsertTest(EClientDbTest, TheNonSecureDbName, ECompactAfterCommit);
   1.628 +	PrintFileSize(TheNonSecureDbName);
   1.629 +
   1.630 +	TInt err = fm->Copy(TheNonSecureDbName, TheNonSecureDbName2);
   1.631 +	TEST2(err, KErrNone);
   1.632 +	
   1.633 +	TheTest.Printf(_L("DBMS, update, UTF16 SQL strings, non-secure db, client, no db compaction\r\n"));
   1.634 +	UpdateTest(EClientDbTest, TheNonSecureDbName, KUpdateSql(), ENoCompaction);
   1.635 +	PrintFileSize(TheNonSecureDbName);
   1.636 +	err = fm->Copy(TheNonSecureDbName2, TheNonSecureDbName);
   1.637 +	TEST2(err, KErrNone);
   1.638 +	
   1.639 +	TheTest.Printf(_L("DBMS, update, UTF16 SQL strings, non-secure db, client, db compaction at end\r\n"));
   1.640 +	UpdateTest(EClientDbTest, TheNonSecureDbName, KUpdateSql(), ECompactAtEnd);
   1.641 +	PrintFileSize(TheNonSecureDbName);
   1.642 +	err = fm->Copy(TheNonSecureDbName2, TheNonSecureDbName);
   1.643 +	TEST2(err, KErrNone);
   1.644 +
   1.645 +	TheTest.Printf(_L("DBMS, update, UTF16 SQL strings, non-secure db, client, db compaction after every 10 records\r\n"));
   1.646 +	UpdateTest(EClientDbTest, TheNonSecureDbName, KUpdateSql(), ECompactAfterEvery10Rec);
   1.647 +	PrintFileSize(TheNonSecureDbName);
   1.648 +//L	err = fm->Copy(TheNonSecureDbName2, TheNonSecureDbName);
   1.649 +//L	TEST2(err, KErrNone);
   1.650 +	
   1.651 +//L	TheTest.Printf(_L("DBMS, update, UTF16 SQL strings, non-secure db, client, db compaction after every update\r\n"));
   1.652 +//L	UpdateTest(EClientDbTest, TheNonSecureDbName, KUpdateSql(), ECompactAfterEveryUpd);
   1.653 +//L	PrintFileSize(TheNonSecureDbName);
   1.654 +	
   1.655 +	TheTest.Printf(_L("DBMS, select, UTF16 SQL strings, non-secure db, client\r\n"));
   1.656 +	SelectTestL(EClientDbTest, TheNonSecureDbName, KSelectSql());
   1.657 +
   1.658 +	TheTest.Printf(_L("DBMS, delete, UTF16 SQL strings, non-secure db, client\r\n"));
   1.659 +	DeleteTest(EClientDbTest, TheNonSecureDbName, KDeleteSql(), ECompactAtEnd);
   1.660 +	PrintFileSize(TheNonSecureDbName);
   1.661 +
   1.662 +	//==================================== SERVER ==========================================================
   1.663 +
   1.664 +	TheTest.Printf(_L("DBMS, insert, UTF16 SQL strings, non-secure db, server, no db compaction\r\n"));
   1.665 +	TDbHelper::CreateL(TheNonSecureDbName);
   1.666 +	InsertTest(EServerDbTest, TheNonSecureDbName, ENoCompaction);
   1.667 +	PrintFileSize(TheNonSecureDbName);
   1.668 +	
   1.669 +	TheTest.Printf(_L("DBMS, insert, UTF16 SQL strings, non-secure db, server, db compaction at end\r\n"));
   1.670 +	TDbHelper::CreateL(TheNonSecureDbName);
   1.671 +	InsertTest(EServerDbTest, TheNonSecureDbName, ECompactAtEnd);
   1.672 +	PrintFileSize(TheNonSecureDbName);
   1.673 +
   1.674 +	TheTest.Printf(_L("DBMS, insert, UTF16 SQL strings, non-secure db, server, db compaction after commit\r\n"));
   1.675 +	TDbHelper::CreateL(TheNonSecureDbName);
   1.676 +	InsertTest(EServerDbTest, TheNonSecureDbName, ECompactAfterCommit);
   1.677 +	PrintFileSize(TheNonSecureDbName);
   1.678 +
   1.679 +	err = fm->Copy(TheNonSecureDbName, TheNonSecureDbName2);
   1.680 +	TEST2(err, KErrNone);
   1.681 +
   1.682 +	TheTest.Printf(_L("DBMS, update, UTF16 SQL strings, non-secure db, server, no db compaction\r\n"));
   1.683 +	UpdateTest(EServerDbTest, TheNonSecureDbName, KUpdateSql(), ENoCompaction);
   1.684 +	PrintFileSize(TheNonSecureDbName);
   1.685 +	err = fm->Copy(TheNonSecureDbName2, TheNonSecureDbName);
   1.686 +	TEST2(err, KErrNone);
   1.687 +	
   1.688 +	TheTest.Printf(_L("DBMS, update, UTF16 SQL strings, non-secure db, server, db compaction at end\r\n"));
   1.689 +	UpdateTest(EServerDbTest, TheNonSecureDbName, KUpdateSql(), ECompactAtEnd);
   1.690 +	PrintFileSize(TheNonSecureDbName);
   1.691 +	err = fm->Copy(TheNonSecureDbName2, TheNonSecureDbName);
   1.692 +	TEST2(err, KErrNone);
   1.693 +
   1.694 +	TheTest.Printf(_L("DBMS, update, UTF16 SQL strings, non-secure db, server, db compaction after every 10 records\r\n"));
   1.695 +	UpdateTest(EServerDbTest, TheNonSecureDbName, KUpdateSql(), ECompactAfterEvery10Rec);
   1.696 +	PrintFileSize(TheNonSecureDbName);
   1.697 +//L	err = fm->Copy(TheNonSecureDbName2, TheNonSecureDbName);
   1.698 +//L	TEST2(err, KErrNone);
   1.699 +
   1.700 +//L	TheTest.Printf(_L("DBMS, update, UTF16 SQL strings, non-secure db, server, db compaction after every update\r\n"));
   1.701 +//L	UpdateTest(EServerDbTest, TheNonSecureDbName, KUpdateSql(), ECompactAfterEveryUpd);
   1.702 +//L	PrintFileSize(TheNonSecureDbName);
   1.703 +	
   1.704 +	TheTest.Printf(_L("DBMS, select, UTF16 SQL strings, non-secure db, server\r\n"));
   1.705 +	SelectTestL(EServerDbTest, TheNonSecureDbName, KSelectSql());
   1.706 +
   1.707 +	TheTest.Printf(_L("DBMS, delete, UTF16 SQL strings, non-secure db, server\r\n"));
   1.708 +	DeleteTest(EServerDbTest, TheNonSecureDbName, KDeleteSql(), ECompactAtEnd);
   1.709 +	PrintFileSize(TheNonSecureDbName);
   1.710 +	
   1.711 +	//======================================================================================================
   1.712 +	CleanupStack::PopAndDestroy(fm);
   1.713 +	}
   1.714 +
   1.715 +//Usage: "t_dbperf1 [<drive letter>:]"
   1.716 +
   1.717 +TInt E32Main()
   1.718 +	{
   1.719 +	TheTest.Title();
   1.720 +	
   1.721 +	CTrapCleanup* tc = CTrapCleanup::New();
   1.722 +
   1.723 +	//Construct test database file names, using the passed as an optional argument drive letter 
   1.724 +	TFileName fname;
   1.725 +	TParse parse;
   1.726 +	User::CommandLine(fname);
   1.727 +	
   1.728 +	_LIT(KNonSecureDbName, "c:\\test\\t_dbperf1_1.db");
   1.729 +	parse.Set(fname, &KNonSecureDbName, 0);
   1.730 +	const TDesC& dbFilePath1 = parse.FullName();
   1.731 +	TheNonSecureDbName.Copy(dbFilePath1);
   1.732 +
   1.733 +	_LIT(KNonSecureDbName2, "c:\\test\\t_dbperf1_2.db");
   1.734 +	parse.Set(fname, &KNonSecureDbName2, 0);
   1.735 +	const TDesC& dbFilePath2 = parse.FullName();
   1.736 +	TheNonSecureDbName2.Copy(dbFilePath2);
   1.737 +	
   1.738 +	__UHEAP_MARK;
   1.739 +	
   1.740 +	TestEnvInit();
   1.741 +	TRAPD(err, DoTestsL());
   1.742 +	TestEnvDestroy();
   1.743 +	TEST2(err, KErrNone);
   1.744 +	
   1.745 +	__UHEAP_MARKEND;
   1.746 +	
   1.747 +	TheTest.End();
   1.748 +	TheTest.Close();
   1.749 +	
   1.750 +	delete tc;
   1.751 +
   1.752 +	User::Heap().Check();
   1.753 +	return KErrNone;
   1.754 +	}