summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/performance/t_perfcrudoperations.cpp
blob: 393ae9c5918dafdb6c5a5c4b3099d1dcd0256c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/


/**
@SYMPREQ                  PREQ1187
@SYMComponent             app-engines_cntmodel
@SYMAuthor                Simon Mellor, JamesCl
@SYMTestStatus            Implemented
@SYMTestType              CT

@SYMTestCaseDesc          Tests the performance of Reading and Updating a single contact and of
						  Matching Contacts by Phone Number on a corporate profile database of 
						  1000 contacts. 

@SYMTestActions           Measures the time taken to: 
						  -- Attempt to match a phone number using CContactDatabase::MatchPhoneNumberL()
						  -- Create a contact using CContactDatabase::AddNewContactL()
						  -- Read   a contact using CContactDatabase::OpenContactLX()
						  -- Update a contact using CContactDatabase::CommitContactL()
						  -- Delete a contact using CContactDatabase::DeleteContactL()

@SYMTestExpectedResults   Test cases will run without leaving and will output timing information.

*/

#include "t_perfcrudoperations.h"
#include "../t_utils.h"
#include <cntitem.h>

// Constants   
_LIT(KDbPathName,"C:Contacts.cdb");
const TPtrC KDbPath( (KDbPathName) );


CCrudOperations::CCrudOperations()
	{
	} 

CCrudOperations::~CCrudOperations()
	{
	iDb->CloseTables();
	delete iDb;
	} 
	
CCrudOperations* CCrudOperations::NewLC(RTest& aTest)
	{
	CCrudOperations* self = new(ELeave) CCrudOperations();
	CleanupStack::PushL(self);
	self->ConstructL(aTest);
	return(self);
	}  

void CCrudOperations::ConstructL(RTest& aTest)
	{
	iTest = &aTest;
	iDb = CContactDatabase::OpenL(KDbPath);
	CCntTest::ProfileReset(0, 59);
	} 

/**
	Main test routine.
*/
void CCrudOperations::DoTestsL(TInt aNumMsToGet20ItemsFromView)
	{
	iNumMsToGet20ItemsFromView= aNumMsToGet20ItemsFromView;

	// As, at this point, we haven't used the database before, this is the 
	// equivalent of startup. We fetch a contact as a bare minimum operation 
	// on the database to ensure that we fetch and cache the system template.
	// Then, we just get rid of the contact item as we don't actually want it.
	const TInt KContactIdOne(1);
	CContactItem* tmpItem = iDb->ReadContactLC(KContactIdOne);
	CleanupStack::PopAndDestroy(tmpItem);
	tmpItem = NULL;

	MatchContactsByPhoneNumL();
	MatchContactsByEmailAndSipL();
	DoCrudTestsL();
	}

/**
	Try to match the phone number given with existing numbers in the database.
*/
void CCrudOperations::MatchContactsByPhoneNumL()
	{
	_LIT(KPass, "Pass");
	_LIT(KFail, "Fail");
	_LIT(KMatchOutputFormat1, "Number of phone numbers matched: %d -- %s\n");
	_LIT(KMatchOutputFormat2, "Phone number lookup took: %d s %03d\n");
	_LIT(KMatchOutputFormat3, "The contact's first name: %S\n");

	_LIT(KPhoneNumberToFind, "241867306233253164487125"); // this number belongs to the 1000th
														  // contact item in the database, which 
														  // has a photo. 
	
	const TInt KMatchLengthFromRight(7); // match on the number without the dialling code prefix
										 // to avoid differences like +447876... and 07876...
	_LIT(KPhoneNumberTestTitle, "\nBeginning Phone Number Match testing...\n");


	iTest->Printf(KPhoneNumberTestTitle);
	TCntPerfTimer timer;
	timer.StartTimer();
	
	// Search the database...
	CContactIdArray* idArray = iDb->MatchPhoneNumberL(KPhoneNumberToFind, KMatchLengthFromRight);
	CleanupStack::PushL(idArray);
	TInt numMatches = idArray->Count();
	iTest->Printf(KMatchOutputFormat1, numMatches, numMatches ? KPass().Ptr() : KFail().Ptr());

	if (numMatches)
		{
		//fetch first contact item in array
		CContactItem* item = iDb->ReadContactLC((*idArray)[0]);
		timer.StopTimer();
		CContactItemFieldSet& fieldSet = item->CardFields();
		TInt firstNameFieldId = fieldSet.Find(KUidContactFieldGivenName);
		if (firstNameFieldId == KErrNotFound)
			{
			User::Leave(KErrNotFound);
			}
		CContactItemField& firstNameField = fieldSet[firstNameFieldId];
		CContactTextField* firstNameText = firstNameField.TextStorage();
		TPtrC firstName(firstNameText->Text());
		iTest->Printf(KMatchOutputFormat3, &firstName);

		CleanupStack::PopAndDestroy(item);
		}
	else
		{
		timer.StopTimer();
		}

	CleanupStack::PopAndDestroy(idArray);

	TInt result = timer.Result();
	TBuf<64> formattable;
	formattable.Format(KMatchOutputFormat2, result / 1000000, (result / 1000) % 1000);
	iTest->Printf(formattable);
	}

/**
	Try to match the phone number given with existing numbers in the database.
*/
void CCrudOperations::MatchContactsByEmailAndSipL()
	{
	_LIT(KPass, "Pass");
	_LIT(KFail, "Fail");
	_LIT(KEmailMatchOutputFormat1,"Number of email addresses matched: %d -- %s\n");
	_LIT(KEmailMatchOutputFormat2,"Email address lookup took: %d s %03d\n");
	_LIT(KMatchOutputFormat3, "The contact's first name: %S\n");

	// Test Email address matching

	// this address belongs to the 1000th contact item in the database, which has a photo. 
	_LIT(KEmailAddressToFind, "fred.bloggs@symbian.com"); 
	_LIT(KEmailTestTitle, "\nBeginning Email Address Match testing...\n");

	iTest->Printf(KEmailTestTitle);
	TCntPerfTimer timer;
	timer.StartTimer();
	
	// Search the database...
	CContactItemFieldDef* fieldsToSearch = new (ELeave) CContactItemFieldDef();
	CleanupStack::PushL(fieldsToSearch);
	fieldsToSearch->AppendL(KUidContactFieldEMail);
	CContactIdArray* idArray = iDb->FindLC(KEmailAddressToFind, fieldsToSearch);
	TInt numMatches = idArray->Count();
	iTest->Printf(KEmailMatchOutputFormat1, numMatches, numMatches ? KPass().Ptr() : KFail().Ptr());

	if (numMatches)
		{
		//fetch first contact item in array
		CContactItem* item = iDb->ReadContactLC((*idArray)[0]);
		CContactItemFieldSet& fieldSet = item->CardFields();
		TInt firstNameFieldId = fieldSet.Find(KUidContactFieldGivenName);
		if (firstNameFieldId == KErrNotFound)
			{
			User::Leave(KErrNotFound);
			}
		CContactItemField& firstNameField = fieldSet[firstNameFieldId];
		CContactTextField* firstNameText = firstNameField.TextStorage();
		TPtrC firstName(firstNameText->Text());
		iTest->Printf(KMatchOutputFormat3, &firstName);
		CleanupStack::PopAndDestroy(item);
		}
	
	timer.StopTimer();
	CleanupStack::PopAndDestroy(2, fieldsToSearch); // and idArray

	
	TInt result = timer.Result();
	TBuf<64> formattable;
	formattable.Format(KEmailMatchOutputFormat2, result / 1000000, (result / 1000) % 1000);
	iTest->Printf(formattable);

	// reset variables
	fieldsToSearch = NULL;
	idArray = NULL;
	numMatches = 0;
	timer.ResetTimer();
	result = 0;
	formattable.Zero();

	
	// Test SIP address matching

	// this address belongs to the 1000th contact item in the database, which has a photo. 
	_LIT(KSipAddressToFind, "sip:fred.bloggs@symbian.com"); 
	_LIT(KSipTestTitle, "\nBeginning SIP Address Match testing...\n");
	_LIT(KSipMatchOutputFormat1,"Number of SIP addresses matched: %d -- %s\n");
	_LIT(KSipMatchOutputFormat2,"SIP address lookup took: %d s %03d\n");

	iTest->Printf(KSipTestTitle);
	timer.StartTimer();
	
	// Search the database...
	fieldsToSearch = new (ELeave) CContactItemFieldDef();
	CleanupStack::PushL(fieldsToSearch);
	fieldsToSearch->AppendL(KUidContactFieldSIPID);
	idArray = iDb->FindLC(KSipAddressToFind, fieldsToSearch);
	numMatches = idArray->Count();
	iTest->Printf(KSipMatchOutputFormat1, numMatches, numMatches ? KPass().Ptr() : KFail().Ptr());

	if (numMatches)
		{
		//fetch first contact item in array
		CContactItem* item = iDb->ReadContactLC((*idArray)[0]);
		CContactItemFieldSet& fieldSet = item->CardFields();
		TInt firstNameFieldId = fieldSet.Find(KUidContactFieldGivenName);
		if (firstNameFieldId == KErrNotFound)
			{
			User::Leave(KErrNotFound);
			}
		CContactItemField& firstNameField = fieldSet[firstNameFieldId];
		CContactTextField* firstNameText = firstNameField.TextStorage();
		TPtrC firstName(firstNameText->Text());
		iTest->Printf(KMatchOutputFormat3, &firstName);
		CleanupStack::PopAndDestroy(item);
		}
	
	timer.StopTimer();
	CleanupStack::PopAndDestroy(2, fieldsToSearch); // and idArray

	result = timer.Result();
	formattable.Format(KSipMatchOutputFormat2, result / 1000000, (result / 1000) % 1000);
	iTest->Printf(formattable);
	}


/**
	Perform CRUD operations with 1 item in the database.
*/
void CCrudOperations::DoCrudTestsL()
	{
	// chosen because they have a photo field
	const TContactItemId K901stContactItemId(901);
	const TContactItemId K973rdContactItemId(973); 
	const TContactItemId K926thContactItemId(926);

	_LIT(KCRUDTestTitle, "\nBeginning CRUD Operations on 1 Contact (with photo) testing ...\n");
	_LIT(KCRUDTestNote, "[Figures for each write operation include time to fetch first 20 items in view.]\n");
	iTest->Printf(KCRUDTestTitle);
	iTest->Printf(KCRUDTestNote);
	
	TCntPerfTimer timer;

	// fetch a contact item to seed a new one
	CContactItem* temp = iDb->OpenContactL(K901stContactItemId);
	CleanupStack::PushL(temp);
	CContactItem* item = CContactCard::NewL(temp);
	CleanupStack::PopAndDestroy(temp);
	CleanupStack::PushL(item);

	// Create (add) a contact with a photo
	timer.StartTimer();
	iDb->AddNewContactL(*item);
	timer.StopTimer();
	TInt createResult = timer.Result() + iNumMsToGet20ItemsFromView; // add the time to fetch 20 items from view.
	timer.ResetTimer();
	CleanupStack::PopAndDestroy(item);
	item = NULL;

	// Read a contact with a photo
	timer.StartTimer();
	item = iDb->OpenContactLX(K973rdContactItemId); // lock object put on the CleanupStack
	CleanupStack::PushL(item);
	timer.StopTimer();
	TInt readResult = timer.Result();
	timer.ResetTimer();
	
	// Update the contact in the database
	timer.StartTimer();
	iDb->CommitContactL(*item);
	timer.StopTimer();
	TInt updateResult = timer.Result() + iNumMsToGet20ItemsFromView; // add the time to fetch 20 items from view.
	timer.ResetTimer();
	CleanupStack::PopAndDestroy(item);
	item = NULL;
	CleanupStack::PopAndDestroy(); // lock object

	// Delete a contact with a photo
	timer.StartTimer();
	iDb->DeleteContactL(K926thContactItemId);
	timer.StopTimer();
	TInt deleteResult = timer.Result() + iNumMsToGet20ItemsFromView; // add the time to fetch 20 items from view.
	timer.ResetTimer();
	
	// output timing result
	_LIT(KCreateOutputFormat,"Creating (adding) 1 contact took: %d s %03d\n");
	iTest->Printf(KCreateOutputFormat, createResult / 1000000, (createResult / 1000) % 1000);

	_LIT(KReadOutputFormat,"Reading 1 contact took: %d s %03d\n");
	iTest->Printf(KReadOutputFormat, readResult / 1000000, (readResult / 1000) % 1000);

	_LIT(KUpdateOutputFormat,"Updating 1 contact took: %d s %03d\n");
	iTest->Printf(KUpdateOutputFormat, updateResult / 1000000, (updateResult / 1000) % 1000);

	_LIT(KDeleteOutputFormat,"Deleting 1 contact took: %d s %03d\n");
	iTest->Printf(KDeleteOutputFormat, deleteResult / 1000000, (deleteResult / 1000) % 1000);
	}