/
groupmodelprofiletest.cpp
205 lines (161 loc) · 5.85 KB
/
groupmodelprofiletest.cpp
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
/******************************************************************************
**
** This file is part of libcommhistory.
**
** Copyright (C) 2015 Jolla Ltd.
** Contact: Matt Vogt <matthew.vogt@jollamobile.com>
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the GNU Lesser General Public License version 2.1 as
** published by the Free Software Foundation.
**
** This library is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
** License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this library; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**
******************************************************************************/
#include "groupmodelprofiletest.h"
#include "groupmodel.h"
#include "common.h"
#include <QtTest/QtTest>
#include <QDateTime>
#include <QDBusConnection>
#include <QModelIndex>
#include <cstdlib>
using namespace CommHistory;
void GroupModelProfileTest::initTestCase()
{
initTestDatabase();
logFile = 0;
qsrand( QDateTime::currentDateTime().toTime_t() );
}
void GroupModelProfileTest::init()
{
}
void GroupModelProfileTest::prepare()
{
const int groups = 1000;
const int messages = 10;
const int contacts = 1000;
deleteAll();
int commitBatchSize = 100;
qDebug() << Q_FUNC_INFO << "- Creating" << contacts << "contacts";
QList<QPair<QString, QPair<QString, QString> > > contactDetails;
int ci = remoteUids.count();
while(ci < contacts) {
QString phoneNumber;
do {
phoneNumber = QString().setNum(qrand() % 10000000);
} while (remoteUids.contains(phoneNumber));
remoteUids << phoneNumber;
contactIndices << ci;
ci++;
contactDetails.append(qMakePair(QString("Test Contact %1").arg(ci), qMakePair(phoneNumber, QString())));
if(ci % commitBatchSize == 0 && ci < contacts) {
qDebug() << Q_FUNC_INFO << "- adding" << commitBatchSize
<< "contacts (" << ci << "/" << contacts << ")";
addTestContacts(contactDetails);
contactDetails.clear();
}
}
if (!contactDetails.isEmpty()) {
qDebug() << Q_FUNC_INFO << "- adding rest of the contacts ("
<< ci << "/" << contacts << ")";
addTestContacts(contactDetails);
contactDetails.clear();
}
// Randomize the contact indices
random_shuffle(contactIndices.begin(), contactIndices.end());
qDebug() << Q_FUNC_INFO << "- Creating" << groups << "new groups";
QList<Group> groupList;
GroupModel addModel;
int gi = 0;
while(gi < groups) {
Group grp;
grp.setLocalUid(RING_ACCOUNT);
grp.setRecipients(RecipientList::fromUids(RING_ACCOUNT, QStringList() << remoteUids.at(contactIndices.at(gi))));
QVERIFY(addModel.addGroup(grp));
groupList << grp;
gi++;
if(gi % commitBatchSize == 0 && gi < groups) {
qDebug() << Q_FUNC_INFO << "- adding" << commitBatchSize
<< "groups (" << gi << "/" << groups << ")";
}
}
qDebug() << Q_FUNC_INFO << "- adding rest of the groups ("
<< gi << "/" << groups << ")";
qDebug() << Q_FUNC_INFO << "- Creating" << messages << "messages to each group";
EventModel eventModel;
QDateTime when = QDateTime::currentDateTime();
gi = 0;
foreach (Group grp, groupList) {
QList<Event> eventList;
for(int i = 0; i < messages; i++) {
Event e;
e.setType(Event::SMSEvent);
e.setDirection(qrand() % 2 ? Event::Inbound : Event::Outbound);
e.setGroupId(grp.id());
e.setStartTime(when.addSecs(i));
e.setEndTime(when.addSecs(i));
e.setLocalUid(RING_ACCOUNT);
e.setRecipients(grp.recipients());
e.setFreeText(randomMessage(qrand() % 49 + 1)); // Max 50 words / message
e.setIsDraft(false);
e.setIsMissedCall(false);
eventList << e;
}
QVERIFY(eventModel.addEvents(eventList, false));
gi++;
qDebug() << Q_FUNC_INFO << "- adding" << messages << "messages ("
<< gi << "/" << groups << ")";
eventList.clear();
}
}
void GroupModelProfileTest::execute()
{
const bool resolve = false;
QList<int> times;
int iterations = 1;
logFile = new QFile("libcommhistory-performance-test.log");
if(!logFile->open(QIODevice::Append)) {
qDebug() << "!!!! Failed to open log file !!!!";
logFile = 0;
}
QDateTime startTime = QDateTime::currentDateTime();
qDebug() << Q_FUNC_INFO << "- Fetching groups." << iterations << "iterations";
for(int i = 0; i < iterations; i++) {
GroupModel fetchModel;
GroupManager manager;
manager.setResolveContacts(resolve ? GroupManager::ResolveImmediately : GroupManager::DoNotResolve);
fetchModel.setManager(&manager);
QTime time;
time.start();
bool result = fetchModel.getGroups();
QVERIFY(result);
if (!fetchModel.isReady())
waitForSignal(&fetchModel, SIGNAL(modelReady(bool)));
int elapsed = time.elapsed();
times << elapsed;
qDebug("Time elapsed: %d ms", elapsed);
QVERIFY(fetchModel.rowCount() > 0);
}
summarizeResults(metaObject()->className(), times, logFile, startTime.secsTo(QDateTime::currentDateTime()));
}
void GroupModelProfileTest::finalise()
{
deleteAll();
}
void GroupModelProfileTest::cleanupTestCase()
{
if(logFile) {
logFile->close();
delete logFile;
logFile = 0;
}
}
QTEST_MAIN(GroupModelProfileTest)