1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/dbms/pcdbms/tdbms/src/t_bench.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,355 @@
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 +#ifndef __TOOLS2__ // we aren't interested in timings for tools2
1.24 +#include <e32svr.h>
1.25 +#include <hal.h>
1.26 +#endif
1.27 +
1.28 +#include "crccheck.h"
1.29 +
1.30 +#undef __UHEAP_MARK
1.31 +#define __UHEAP_MARK
1.32 +#undef __UHEAP_MARKEND
1.33 +#define __UHEAP_MARKEND
1.34 +
1.35 +LOCAL_D TDBMS_CRCChecks TheCrcChecker;
1.36 +
1.37 +#ifndef __linux__ //No CRC test on LINUX
1.38 +#ifdef __TOOLS2__
1.39 +const TPtrC KCrcRecord=_L("\\epoc32\\winscw\\c\\dbms-tst\\T_BENCH.CRC");
1.40 +#else
1.41 +const TPtrC KCrcRecord=_L("C:\\dbms-tst\\T_BENCH.CRC");
1.42 +#endif
1.43 +#endif
1.44 +
1.45 +// MSVC++ up to 5.0 has problems with expanding inline functions
1.46 +// This disables the mad warnings for the whole project
1.47 +#if defined(NDEBUG) && defined(__VC32__) && _MSC_VER<=1100
1.48 +#pragma warning(disable : 4710) // function not expanded. MSVC 5.0 is stupid
1.49 +#endif
1.50 +
1.51 +#ifndef __TOOLS2__
1.52 +class TTimer
1.53 + {
1.54 +public:
1.55 + void Start();
1.56 + TReal Stop() const;
1.57 +private:
1.58 + TUint iTicks;
1.59 + };
1.60 +#endif
1.61 +
1.62 +LOCAL_D RTest test(_L("T_BENCH"));
1.63 +LOCAL_D CTrapCleanup* TheTrapCleanup;
1.64 +LOCAL_D RDbNamedDatabase TheDatabase;
1.65 +LOCAL_D RDbView TheView;
1.66 +LOCAL_D RFs TheFs;
1.67 +
1.68 +const TInt KTestCleanupStack=0x20;
1.69 +//T_BENCH file shall not be deleted at the end of the test! It will be used by T_COMP test.
1.70 +
1.71 +#ifdef __TOOLS2__
1.72 +const TPtrC KTestDatabase=_L(".\\dbms-tst\\T_BENCH.DB");
1.73 +#else
1.74 +const TPtrC KTestDatabase=_L("C:\\dbms-tst\\T_BENCH.DB");
1.75 +#endif
1.76 +const TPtrC KTableName=_S("Test");
1.77 +const TPtrC KColCluster=_S("Cluster");
1.78 +const TPtrC KColXcluster=_S("xCluster");
1.79 +const TPtrC KColRandom=_S("Random");
1.80 +const TPtrC KColXrandom=_S("xRandom");
1.81 +const TInt KRecords=2000;
1.82 +
1.83 +#ifndef __TOOLS2__
1.84 +static TTimer TheTimer;
1.85 +
1.86 +void TTimer::Start()
1.87 + {
1.88 + iTicks=User::FastCounter();
1.89 + }
1.90 +
1.91 +TReal TTimer::Stop() const
1.92 + {
1.93 + TUint ticks = User::FastCounter() - iTicks;
1.94 + TInt freq = 0;
1.95 + test(HAL::Get(HAL::EFastCounterFrequency, freq) == KErrNone);
1.96 + const TInt KMicroSecIn1Sec = 1000000;
1.97 + const TInt KMsIn1Sec = 1000;
1.98 + double v = ((double)ticks * KMicroSecIn1Sec) / (double)freq; TInt v2 = (TInt)v;
1.99 + return v2 / KMsIn1Sec;
1.100 + }
1.101 +#endif
1.102 +
1.103 +LOCAL_C void CloseDatabase()
1.104 + {
1.105 + TheDatabase.Close();
1.106 + TheCrcChecker.GenerateCrcL(KTestDatabase);
1.107 + }
1.108 +
1.109 +/**
1.110 +Create the database: keep the code 050 compatible
1.111 +
1.112 +@SYMTestCaseID SYSLIB-DBMS-CT-0577
1.113 +@SYMTestCaseDesc Benchmark Tests. Creation of a local Database test
1.114 +@SYMTestPriority Medium
1.115 +@SYMTestActions Attempt to test RDbNamedDatabase::CreateTable(),RDbNamedDatabase::CreateIndex(),
1.116 + RDbNamedDatabase::Compact(),RDbView::Prepare() functions
1.117 +@SYMTestExpectedResults Test must not fail
1.118 +@SYMREQ REQ0000
1.119 +*/
1.120 +LOCAL_C void CreateDatabase()
1.121 + {
1.122 + test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0577 "));
1.123 + User::LeaveIfError(TheDatabase.Replace(TheFs,KTestDatabase));
1.124 + CDbColSet& set=*CDbColSet::NewLC();
1.125 + TDbCol col(KColCluster,EDbColInt32);
1.126 + col.iAttributes=col.ENotNull;
1.127 + set.AddL(col);
1.128 + col.iName=KColXcluster;
1.129 + set.AddL(col);
1.130 + col.iName=KColRandom;
1.131 + set.AddL(col);
1.132 + col.iName=KColXrandom;
1.133 + set.AddL(col);
1.134 + TInt r=TheDatabase.CreateTable(KTableName,set);
1.135 + test (r==KErrNone);
1.136 + CleanupStack::PopAndDestroy();
1.137 +#ifndef __TOOLS2__
1.138 + TheTimer.Start();
1.139 +#endif
1.140 + r=TheView.Prepare(TheDatabase,_L("select * from test"),TheView.EInsertOnly);
1.141 + TheDatabase.Begin();
1.142 + test (r==KErrNone);
1.143 + TInt jj=0;
1.144 + for (TInt ii=0;ii<KRecords;++ii)
1.145 + {
1.146 + TheView.InsertL();
1.147 + jj=(jj+23);
1.148 + if (jj>=KRecords)
1.149 + jj-=KRecords;
1.150 + TheView.SetColL(1,ii);
1.151 + TheView.SetColL(2,ii);
1.152 + TheView.SetColL(3,jj);
1.153 + TheView.SetColL(4,jj);
1.154 + TheView.PutL();
1.155 + }
1.156 + r=TheDatabase.Commit();
1.157 + test (r==KErrNone);
1.158 + TheView.Close();
1.159 +#ifndef __TOOLS2__
1.160 + test.Printf(_L("Build table: %7.1f ms\n"),TheTimer.Stop());
1.161 + TheTimer.Start();
1.162 +#endif
1.163 + CDbKey& key=*CDbKey::NewLC();
1.164 + key.AddL(KColXcluster);
1.165 + key.MakeUnique();
1.166 + r=TheDatabase.CreateIndex(KColXcluster,KTableName,key);
1.167 + test (r==KErrNone);
1.168 +#ifndef __TOOLS2__
1.169 + test.Printf(_L("Cluster index: %7.1f ms\n"),TheTimer.Stop());
1.170 + TheTimer.Start();
1.171 +#endif
1.172 + key.Clear();
1.173 + key.AddL(KColXrandom);
1.174 + r=TheDatabase.CreateIndex(KColXrandom,KTableName,key);
1.175 + test (r==KErrNone);
1.176 + CleanupStack::PopAndDestroy();
1.177 +#ifndef __TOOLS2__
1.178 + test.Printf(_L("Random index: %7.1f ms\n"),TheTimer.Stop());
1.179 + TheTimer.Start();
1.180 +#endif
1.181 + r = TheDatabase.Compact();
1.182 +#ifndef __TOOLS2__
1.183 + test.Printf(_L("Compact: %7.1f ms\n"),TheTimer.Stop());
1.184 +#endif
1.185 + test (r == KErrNone);
1.186 + }
1.187 +
1.188 +LOCAL_C TReal Evaluate(const TDesC& aSql)
1.189 + {
1.190 + TInt m=1;
1.191 + for (;;)
1.192 + {
1.193 +#ifndef __TOOLS2__
1.194 + TheTimer.Start();
1.195 +#endif
1.196 + for (TInt ii=0;ii<m;++ii)
1.197 + {
1.198 + TInt r=TheView.Prepare(TheDatabase,aSql,KDbUnlimitedWindow,TheView.EReadOnly);
1.199 + if (r<0)
1.200 + return r;
1.201 + r=TheView.EvaluateAll();
1.202 + test (r==KErrNone);
1.203 + TheView.Close();
1.204 + }
1.205 +#ifndef __TOOLS2__
1.206 + TReal t=TheTimer.Stop();
1.207 + if (t>=100.0)
1.208 + return t/m;
1.209 + m*=4;
1.210 +#else
1.211 + // We aren't interested in timings for tools2.
1.212 + return(1.0/m); // is this right?
1.213 +#endif
1.214 + }
1.215 + }
1.216 +
1.217 +struct TTest
1.218 + {
1.219 + const TText* iName;
1.220 + const TText* iQuery;
1.221 + };
1.222 +const TTest KQuery[]=
1.223 + {
1.224 + {_S("project"),_S("select cluster,xcluster,random,xrandom from test")},
1.225 + {_S("restrict 1"),_S("select * from test where cluster=0")},
1.226 + {_S("restrict 2"),_S("select * from test where xrandom=0")},
1.227 + {_S("restrict 3"),_S("select * from test where xcluster<500 and xrandom <500")},
1.228 + {_S("order 1"),_S("select * from test order by xrandom")},
1.229 + {_S("order 2"),_S("select * from test order by cluster")},
1.230 + {_S("all 1"),_S("select * from test where random<500 order by xrandom")},
1.231 + {_S("all 2"),_S("select * from test where xcluster<500 order by xrandom")},
1.232 + {_S("all 3"),_S("select * from test where xcluster<500 order by xcluster")},
1.233 + {_S("all 4"),_S("select * from test where xcluster<500 and xrandom<200 order by xcluster")}
1.234 + };
1.235 +
1.236 +/**
1.237 +@SYMTestCaseID SYSLIB-DBMS-CT-0578
1.238 +@SYMTestCaseDesc Benchmark Test.Querying a local Database Test
1.239 +@SYMTestPriority Medium
1.240 +@SYMTestActions Evaluate SELECT queries on the created database
1.241 +@SYMTestExpectedResults Test must not fail
1.242 +@SYMREQ REQ0000
1.243 +*/
1.244 +LOCAL_C void Queries()
1.245 + {
1.246 + test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0578 "));
1.247 + for (TUint ii=0;ii<sizeof(KQuery)/sizeof(KQuery[0]);++ii)
1.248 + {
1.249 + test.Printf(_L("%15s: "),KQuery[ii].iName);
1.250 + TReal t=Evaluate(TPtrC(KQuery[ii].iQuery));
1.251 + if (t<0.0)
1.252 + test.Printf(_L("-\n"));
1.253 + else
1.254 + test.Printf(_L("%7.1f ms\n"),t);
1.255 + }
1.256 + }
1.257 +
1.258 +//
1.259 +// Benchmark tests
1.260 +//
1.261 +LOCAL_C void BenchTest()
1.262 + {
1.263 + CreateDatabase();
1.264 + Queries();
1.265 + CloseDatabase();
1.266 + }
1.267 +
1.268 +//
1.269 +// Prepare the test directory.
1.270 +//
1.271 +LOCAL_C void setupTestDirectory()
1.272 + {
1.273 + TInt r=TheFs.Connect();
1.274 + test(r==KErrNone);
1.275 +//
1.276 +#if 0
1.277 + TDriveList drives;
1.278 + TheFs.DriveList(drives);
1.279 + if (drives[EDriveK] == KDriveAbsent)
1.280 + {
1.281 + TInt r = TheFs.AddFileSystem(_L("ELFFS"));
1.282 + test (r == KErrNone);
1.283 + r = TheFs.MountFileSystem(_L("Lffs"),EDriveK);
1.284 + if (r == KErrCorrupt || r == KErrNotReady)
1.285 + {
1.286 + RFormat format;
1.287 + TInt count;
1.288 + r = format.Open(TheFs, _L("K:\\"), EHighDensity, count);
1.289 + test (r == KErrNone);
1.290 + while (count)
1.291 + format.Next(count);
1.292 + format.Close();
1.293 + }
1.294 + else
1.295 + test (r == KErrNone);
1.296 + }
1.297 +#endif
1.298 +//
1.299 + r=TheFs.MkDir(KTestDatabase);
1.300 + test(r==KErrNone || r==KErrAlreadyExists);
1.301 + }
1.302 +
1.303 +//
1.304 +// Initialise the cleanup stack.
1.305 +//
1.306 +LOCAL_C void setupCleanup()
1.307 + {
1.308 + TheTrapCleanup=CTrapCleanup::New();
1.309 + test(TheTrapCleanup!=NULL);
1.310 + TRAPD(r,\
1.311 + {\
1.312 + for (TInt i=KTestCleanupStack;i>0;i--)\
1.313 + CleanupStack::PushL((TAny*)0);\
1.314 + CleanupStack::Pop(KTestCleanupStack);\
1.315 + });
1.316 + test(r==KErrNone);
1.317 + }
1.318 +
1.319 +//
1.320 +// entry point
1.321 +//
1.322 +GLDEF_C TInt E32Main()
1.323 + {
1.324 + test.Title();
1.325 + setupTestDirectory();
1.326 + setupCleanup();
1.327 + __UHEAP_MARK;
1.328 +
1.329 + test.Start(_L("Benchmarking..."));
1.330 + TRAPD(r,BenchTest();)
1.331 + test(r==KErrNone);
1.332 +
1.333 +#ifndef __linux__
1.334 + TInt err;
1.335 +#ifndef __TOOLS2__
1.336 + TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
1.337 + test(err==KErrNone);
1.338 + test(lc==KErrNone);
1.339 +#else
1.340 + TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
1.341 + TPtrC errmsg;
1.342 + TheCrcChecker.ErrorReportL(err, errmsg);
1.343 + RDebug::Print(errmsg);
1.344 + test(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
1.345 +#endif // TOOLS2
1.346 +#endif // linux
1.347 +
1.348 + test.End();
1.349 +
1.350 + __UHEAP_MARKEND;
1.351 + delete TheTrapCleanup;
1.352 +
1.353 + //T_BENCH.DB cannot be deleted here, because it is used by T_COMP test!
1.354 +
1.355 + TheFs.Close();
1.356 + test.Close();
1.357 + return 0;
1.358 + }