summaryrefslogtreecommitdiffstats
path: root/plugins/organizer/symbian/tsrc/tst_symbianperformance/tst_symbianperformance.cpp
blob: 923a04a0b10f6183cedf2146c56edc0c838dc958 (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QObject>
#include <qmobilityglobal.h>
#include <qtorganizer.h>
#include <QtTest/QtTest>
#include <QDebug>
#include <calcommon.h> // for SYMBIAN_CALENDAR_V2

QTM_USE_NAMESPACE

/*!
 * For testing symbian backend performance via QOrganizerManager API.
 */
class tst_symbianperformance : public QObject
{
    Q_OBJECT

private slots:  // Init & cleanup
    void init();
    void cleanup();

private slots:  // Test cases

    void items_data();
    void items();
    void itemsInCollections();

private: // util functions
    QOrganizerItem createItem(
        QString itemType, QString label, QDateTime startTime, QDateTime endTime, int recurrenceCount);

private:
    QOrganizerManager *m_om;
    bool m_customCollectionsSupported;
};


void tst_symbianperformance::init()
{
    // TODO: How could this be done dynamically?
    // Some kind of manager feature flag would be nice.
#ifdef SYMBIAN_CALENDAR_V2
    m_customCollectionsSupported = true;
#else
    m_customCollectionsSupported = false;
#endif

    m_om = new QOrganizerManager();

    // Remove items on all collections
    m_om->removeItems(m_om->itemIds());

    // Remove all collections (except the default)
    foreach (QOrganizerCollection collection, m_om->collections()) {
        if (collection.id() != m_om->defaultCollection().id())
            m_om->removeCollection(collection.id());
    }
}

void tst_symbianperformance::cleanup()
{
    // Remove items on all collections
    m_om->removeItems(m_om->itemIds());

    // Remove all collections (except the default)
    foreach (QOrganizerCollection collection, m_om->collections()) {
        if (collection.id() != m_om->defaultCollection().id())
            m_om->removeCollection(collection.id());
    }

    delete m_om;
    m_om = 0;
}

void tst_symbianperformance::items_data()
{
    QTest::addColumn<int>("itemCount");
    QTest::addColumn<int>("recurrenceCount");

    QTest::newRow(QString("1000 items").toAscii().constData()) << 1000 << 1;
    QTest::newRow(QString("1000 items, 2 occurrences").toAscii().constData()) << 1000 << 2;
    QTest::newRow(QString("1000 items, 4 occurrences").toAscii().constData()) << 1000 << 4;
}

void tst_symbianperformance::items()
{
    QFETCH(int, itemCount);
    QFETCH(int, recurrenceCount);

    QDateTime currentDateTime = QDateTime::currentDateTime();

    {
        // Create a list of items, one item per day for the next "itemCount" days
        QList<QOrganizerItem> items;
        for (int i(0); i < itemCount; i++) {
            items << createItem(
                    QOrganizerItemType::TypeEvent,
                    QString("addItem") + QString::number(i),
                    currentDateTime.addDays(i),
                    currentDateTime.addDays(i).addSecs(3600),
                    recurrenceCount);
        }

        // Save items
        QTime timerStart = QTime::currentTime();
        QVERIFY(m_om->saveItems(&items));
        QTime timerEnd = QTime::currentTime();
        qDebug() << "Save items [ms]: " << timerStart.msecsTo(timerEnd);
    }

    {
        // Fetch all exportable items
        QTime timerStart = QTime::currentTime();
        QList<QOrganizerItem> items = m_om->itemsForExport();
        QTime timerEnd = QTime::currentTime();
        QCOMPARE(items.count(), itemCount);
        qDebug() << "Fetching all items took [ms]: " << timerStart.msecsTo(timerEnd);
    }

    {
        // Fetch all exportable items for the following week
        QDateTime startDateTime = currentDateTime;
        QDateTime endDateTime = currentDateTime.addDays(7);
        QTime timerStart = QTime::currentTime();
        QList<QOrganizerItem> items = m_om->itemsForExport(startDateTime, endDateTime);
        QTime timerEnd = QTime::currentTime();
        // TODO: got "itemsForExport" is broken because not filtered with slowFilter any more: QCOMPARE(items.count(), 7);
        qDebug() << "Fetching 7 items with date filter took [ms]: " << timerStart.msecsTo(timerEnd);
    }

    {
        // Fetch all item instances
        QTime timerStart = QTime::currentTime();
        QList<QOrganizerItem> items = m_om->items();
        QTime timerEnd = QTime::currentTime();
        QCOMPARE(items.count(), itemCount * recurrenceCount);
        qDebug() << "Fetching all item instances took [ms]: " << timerStart.msecsTo(timerEnd);
    }

    {
        // Fetch all item instances for the week in the middle of the time period
        QDateTime startDateTime = currentDateTime.addDays(itemCount / 2);
        QDateTime endDateTime = currentDateTime.addDays(itemCount / 2 + 7);
        QTime timerStart = QTime::currentTime();
        QList<QOrganizerItem> items = m_om->items(startDateTime, endDateTime);
        QTime timerEnd = QTime::currentTime();
        // Known issue with symbian backend; the returned items may, or may not contain an extra item, depending on the platform
        QVERIFY(items.count() == recurrenceCount * 7 || items.count() == recurrenceCount * 8);
        qDebug() << "Fetching item instances with date filter took [ms]: " << timerStart.msecsTo(timerEnd);
    }

    {
        // Add an item with unlimited occurrences (symbian cal server limits occurrences to 31.12.2100)
        QOrganizerItem item = createItem(
                QOrganizerItemType::TypeEvent,
                QString("addItemUnltd"),
                currentDateTime.addSecs(1800),
                currentDateTime.addSecs(3600),
                0);

        QTime startTime = QTime::currentTime();
        QVERIFY(m_om->saveItem(&item));
        QTime endTime = QTime::currentTime();
        qDebug() << "Saving an item with unlimited occurrences took [ms]: " << startTime.msecsTo(endTime);

        // fetch
        startTime = QTime::currentTime();
        QList<QOrganizerItem> items = m_om->itemOccurrences(item);
        endTime = QTime::currentTime();
        qDebug() << "Fetching item occurrences of a parent [ms]: " << startTime.msecsTo(endTime);
    }

    {
        // Remove all items
        QList<QOrganizerItemId> itemIds = m_om->itemIds();
        QTime timerStart = QTime::currentTime();
        QVERIFY(m_om->removeItems(itemIds));
        QTime timerEnd = QTime::currentTime();
        QCOMPARE(m_om->items().count(), 0);
        qDebug() << "Removing all item instances took [ms]: " << timerStart.msecsTo(timerEnd);
    }
}

void tst_symbianperformance::itemsInCollections()
{
    if (!m_customCollectionsSupported)
        QSKIP("Saving/modifying collections not supported!", SkipSingle);

    int itemCount = 500;
    int recurrenceCount = 3;
    QDateTime currentDateTime = QDateTime::currentDateTime();

    // Create two collections
    QOrganizerCollection c1;
    c1.setMetaData("Name", "itemsInCollections1");
    c1.setMetaData("FileName", "c:itemsincollections");
    QVERIFY(m_om->saveCollection(&c1));
    QOrganizerCollection c2;
    c2.setMetaData("Name", "itemsInCollections2");
    c2.setMetaData("FileName", "c:itemsincollections2");
    QVERIFY(m_om->saveCollection(&c2));

    {
        // Save n items to two collections

        QList<QOrganizerItem> items1;
        QList<QOrganizerItem> items2;
        for (int i(0); i < itemCount; i++) {
            QOrganizerItem item1 = createItem(
                    QOrganizerItemType::TypeEvent,
                    QString("addItem") + QString::number(i),
                    currentDateTime.addDays(i),
                    currentDateTime.addDays(i).addSecs(3600),
                    recurrenceCount);
            QOrganizerItem item2 = item1;
            item1.setCollectionId(c1.id());
            items1 << item1;
            item2.setCollectionId(c2.id());
            items2 << item2;
        }

        QTime startTime = QTime::currentTime();
        QVERIFY(m_om->saveItems(&items1));
        QVERIFY(m_om->saveItems(&items2));
        QTime endTime = QTime::currentTime();
        qDebug() << "Saving items took [ms]: " << startTime.msecsTo(endTime);
    }

    {
        // Fetch all items for export
        QTime startTime = QTime::currentTime();
        QList<QOrganizerItem> items = m_om->itemsForExport();
        QTime endTime = QTime::currentTime();
        QCOMPARE(items.count(), itemCount * 2);
        qDebug() << "Fetching all items took [ms]: " << startTime.msecsTo(endTime);
    }

    {
        // Fetch items from collection c1
        QOrganizerItemCollectionFilter filter;
        filter.setCollectionId(c1.id());
        QTime startTime = QTime::currentTime();
        QList<QOrganizerItem> items = m_om->itemsForExport(QDateTime(), QDateTime(), filter);
        QTime endTime = QTime::currentTime();
        QCOMPARE(items.count(), itemCount);
        qDebug() << "Fetching 500 items with collection filter took [ms]: " << startTime.msecsTo(endTime);
    }

    {
        // Fetch all item instances
        QTime startTime = QTime::currentTime();
        QList<QOrganizerItem> items = m_om->items();
        QTime endTime = QTime::currentTime();
        QCOMPARE(items.count(), itemCount * 2 * 3);
        qDebug() << "Fetching all item instances took [ms]: " << startTime.msecsTo(endTime);
    }

    {
        // Fetch item instances with a collection filter
        QOrganizerItemCollectionFilter filter;
        filter.setCollectionId(c1.id());
        QTime startTime = QTime::currentTime();
        QList<QOrganizerItem> items = m_om->items(filter);
        QTime endTime = QTime::currentTime();
        QCOMPARE(items.count(), itemCount * 3);
        qDebug() << "Fetching 500 item instances with collection filter took [ms]: " << startTime.msecsTo(endTime);
    }

    {
        // Add an item with unlimited occurrences (symbian cal server limits occurrences to 31.12.2100)
        QOrganizerItem item = createItem(
                QOrganizerItemType::TypeEvent,
                QString("addItemUnltd"),
                currentDateTime.addSecs(1800),
                currentDateTime.addSecs(3600),
                0);

        QTime startTime = QTime::currentTime();
        QVERIFY(m_om->saveItem(&item));
        QTime endTime = QTime::currentTime();
        qDebug() << "Saving an item with unlimited occurrences took [ms]: " << startTime.msecsTo(endTime);

        // fetch
        startTime = QTime::currentTime();
        QList<QOrganizerItem> items = m_om->itemOccurrences(item);
        endTime = QTime::currentTime();
        qDebug() << "Fetching item occurrences of a parent [ms]: " << startTime.msecsTo(endTime);
    }

    {
        // Remove all items
        QList<QOrganizerItemId> itemIds = m_om->itemIds();
        QTime startTime = QTime::currentTime();
        QVERIFY(m_om->removeItems(itemIds));
        QTime endTime = QTime::currentTime();
        QCOMPARE(m_om->items().count(), 0);
        qDebug() << "Removing all item instances took [ms]: " << startTime.msecsTo(endTime);
    }
}

/*!
 * A helper method for creating a QOrganizerItem instance.
 */
QOrganizerItem tst_symbianperformance::createItem(
        QString itemType, QString label, QDateTime startTime, QDateTime endTime, int recurrenceCount)
{
    QOrganizerItem item;
    item.setType(itemType);
    item.setDisplayLabel(label);
    if (itemType == QOrganizerItemType::TypeTodo) {
        QOrganizerTodoTime timeRange;
        if (startTime.isValid())
            timeRange.setStartDateTime(startTime);
        if (endTime.isValid())
            timeRange.setDueDateTime(endTime);
        if (!timeRange.isEmpty())
            item.saveDetail(&timeRange);
    } else {
        QOrganizerEventTime timeRange;
        if (startTime.isValid())
            timeRange.setStartDateTime(startTime);
        if (endTime.isValid())
            timeRange.setEndDateTime(endTime);
        if (!timeRange.isEmpty())
            item.saveDetail(&timeRange);
    }

    // Set recurrence rule if needed
    if (recurrenceCount >= 0) {
        // Create a weekly recurrence rule
        QOrganizerItemRecurrence recurrence;
        QOrganizerRecurrenceRule rrule;
        rrule.setFrequency(QOrganizerRecurrenceRule::Weekly);
        if (recurrenceCount > 0) // 0 = unlimited
            rrule.setLimit(recurrenceCount);
        QSet<QOrganizerRecurrenceRule> ruleSet;
        ruleSet.insert(rrule);
        recurrence.setRecurrenceRules(ruleSet);
        item.saveDetail(&recurrence);
    }

    return item;
}

QTEST_MAIN(tst_symbianperformance);
#include "tst_symbianperformance.moc"