diff options
Diffstat (limited to 'tests/mailquerybenchmark.cpp')
-rw-r--r-- | tests/mailquerybenchmark.cpp | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/tests/mailquerybenchmark.cpp b/tests/mailquerybenchmark.cpp new file mode 100644 index 0000000..4c6f13f --- /dev/null +++ b/tests/mailquerybenchmark.cpp | |||
@@ -0,0 +1,173 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2016 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||
8 | * later version accepted by the membership of KDE e.V. (or its | ||
9 | * successor approved by the membership of KDE e.V.), which shall | ||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||
11 | * | ||
12 | * This library is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | ||
20 | #include <QtTest> | ||
21 | |||
22 | #include <QString> | ||
23 | |||
24 | #include "testimplementations.h" | ||
25 | |||
26 | #include <common/facade.h> | ||
27 | #include <common/domainadaptor.h> | ||
28 | #include <common/resultprovider.h> | ||
29 | #include <common/synclistresult.h> | ||
30 | #include <common/definitions.h> | ||
31 | #include <common/query.h> | ||
32 | #include <common/store.h> | ||
33 | #include <common/pipeline.h> | ||
34 | #include <common/index.h> | ||
35 | #include <common/indexupdater.h> | ||
36 | |||
37 | #include "hawd/dataset.h" | ||
38 | #include "hawd/formatter.h" | ||
39 | |||
40 | #include <iostream> | ||
41 | #include <math.h> | ||
42 | |||
43 | #include "mail_generated.h" | ||
44 | #include "createentity_generated.h" | ||
45 | #include "getrssusage.h" | ||
46 | |||
47 | /** | ||
48 | * Benchmark mail query performance. | ||
49 | */ | ||
50 | class MailQueryBenchmark : public QObject | ||
51 | { | ||
52 | Q_OBJECT | ||
53 | |||
54 | QByteArray resourceIdentifier; | ||
55 | HAWD::State mHawdState; | ||
56 | |||
57 | void populateDatabase(int count) | ||
58 | { | ||
59 | TestResource::removeFromDisk(resourceIdentifier); | ||
60 | |||
61 | auto pipeline = QSharedPointer<Sink::Pipeline>::create(resourceIdentifier); | ||
62 | |||
63 | auto mailFactory = QSharedPointer<TestMailAdaptorFactory>::create(); | ||
64 | auto indexer = QSharedPointer<DefaultIndexUpdater<Sink::ApplicationDomain::Mail> >::create(); | ||
65 | |||
66 | pipeline->setPreprocessors("mail", QVector<Sink::Preprocessor*>() << indexer.data()); | ||
67 | pipeline->setAdaptorFactory("mail", mailFactory); | ||
68 | |||
69 | auto domainTypeAdaptorFactory = QSharedPointer<TestMailAdaptorFactory>::create(); | ||
70 | |||
71 | pipeline->startTransaction(); | ||
72 | const auto date = QDateTime::currentDateTimeUtc(); | ||
73 | for (int i = 0; i < count; i++) { | ||
74 | auto domainObject = Sink::ApplicationDomain::Mail::Ptr::create(); | ||
75 | domainObject->setProperty("uid", "uid"); | ||
76 | domainObject->setProperty("subject", QString("subject%1").arg(i)); | ||
77 | domainObject->setProperty("date", date.addSecs(count)); | ||
78 | domainObject->setProperty("folder", "folder1"); | ||
79 | // domainObject->setProperty("attachment", attachment); | ||
80 | const auto command = createCommand<Sink::ApplicationDomain::Mail>(*domainObject, *domainTypeAdaptorFactory); | ||
81 | pipeline->newEntity(command.data(), command.size()); | ||
82 | } | ||
83 | pipeline->commit(); | ||
84 | } | ||
85 | |||
86 | void testLoad(int count) | ||
87 | { | ||
88 | const auto startingRss = getCurrentRSS(); | ||
89 | |||
90 | Sink::Query query; | ||
91 | query.liveQuery = false; | ||
92 | query.requestedProperties << "uid" << "subject" << "date"; | ||
93 | query.sortProperty = "date"; | ||
94 | query.propertyFilter.insert("folder", "folder1"); | ||
95 | query.limit = 1000; | ||
96 | |||
97 | //Benchmark | ||
98 | QTime time; | ||
99 | time.start(); | ||
100 | |||
101 | auto resultSet = QSharedPointer<Sink::ResultProvider<Sink::ApplicationDomain::Mail::Ptr> >::create(); | ||
102 | auto resourceAccess = QSharedPointer<TestResourceAccess>::create(); | ||
103 | TestMailResourceFacade facade(resourceIdentifier, resourceAccess); | ||
104 | |||
105 | auto ret = facade.load(query); | ||
106 | ret.first.exec().waitForFinished(); | ||
107 | auto emitter = ret.second; | ||
108 | QList<Sink::ApplicationDomain::Mail::Ptr> list; | ||
109 | emitter->onAdded([&list](const Sink::ApplicationDomain::Mail::Ptr &mail) { | ||
110 | list << mail; | ||
111 | }); | ||
112 | bool done = false; | ||
113 | emitter->onInitialResultSetComplete([&done](const Sink::ApplicationDomain::Mail::Ptr &mail) { | ||
114 | done = true; | ||
115 | }); | ||
116 | emitter->fetch(Sink::ApplicationDomain::Mail::Ptr()); | ||
117 | QTRY_VERIFY(done); | ||
118 | QCOMPARE(list.size(), count); | ||
119 | |||
120 | const auto elapsed = time.elapsed(); | ||
121 | |||
122 | const auto finalRss = getCurrentRSS(); | ||
123 | const auto rssGrowth = finalRss - startingRss; | ||
124 | //Since the database is memory mapped it is attributted to the resident set size. | ||
125 | const auto rssWithoutDb = finalRss - Sink::Storage(Sink::storageLocation(), resourceIdentifier, Sink::Storage::ReadWrite).diskUsage(); | ||
126 | const auto peakRss = getPeakRSS(); | ||
127 | //How much peak deviates from final rss in percent (should be around 0) | ||
128 | const auto percentageRssError = static_cast<double>(peakRss - finalRss)*100.0/static_cast<double>(finalRss); | ||
129 | auto rssGrowthPerEntity = rssGrowth/count; | ||
130 | |||
131 | std::cout << "Loaded " << list.size() << " results." << std::endl; | ||
132 | std::cout << "The query took [ms]: " << elapsed << std::endl; | ||
133 | std::cout << "Current Rss usage [kb]: " << finalRss/1024 << std::endl; | ||
134 | std::cout << "Peak Rss usage [kb]: " << peakRss/1024 << std::endl; | ||
135 | std::cout << "Rss growth [kb]: " << rssGrowth/1024 << std::endl; | ||
136 | std::cout << "Rss growth per entity [byte]: " << rssGrowthPerEntity << std::endl; | ||
137 | std::cout << "Rss without db [kb]: " << rssWithoutDb/1024 << std::endl; | ||
138 | std::cout << "Percentage error: " << percentageRssError << std::endl; | ||
139 | |||
140 | HAWD::Dataset dataset("facade_query", mHawdState); | ||
141 | HAWD::Dataset::Row row = dataset.row(); | ||
142 | row.setValue("rows", list.size()); | ||
143 | row.setValue("queryResultPerMs", (qreal)list.size()/elapsed); | ||
144 | dataset.insertRow(row); | ||
145 | HAWD::Formatter::print(dataset); | ||
146 | |||
147 | QVERIFY(percentageRssError < 10); | ||
148 | //TODO This is much more than it should it seems, although adding the attachment results in pretty exactly a 1k increase, | ||
149 | //so it doesn't look like that memory is being duplicated. | ||
150 | QVERIFY(rssGrowthPerEntity < 3300); | ||
151 | |||
152 | // Print memory layout, RSS is what is in memory | ||
153 | // std::system("exec pmap -x \"$PPID\""); | ||
154 | // std::system("top -p \"$PPID\" -b -n 1"); | ||
155 | } | ||
156 | |||
157 | private Q_SLOTS: | ||
158 | |||
159 | void init() | ||
160 | { | ||
161 | resourceIdentifier = "org.kde.test.instance1"; | ||
162 | } | ||
163 | |||
164 | void test50k() | ||
165 | { | ||
166 | // populateDatabase(50000); | ||
167 | testLoad(50000); | ||
168 | } | ||
169 | |||
170 | }; | ||
171 | |||
172 | QTEST_MAIN(MailQueryBenchmark) | ||
173 | #include "mailquerybenchmark.moc" | ||