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 387 388 389 390 391 392 393 394 395
|
/****************************************************************************
**
** Copyright (C) 2017-2015 Ford Motor Company
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtRemoteObjects module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QString>
#include <QDataStream>
#include <QLocalSocket>
#include <QLocalServer>
#include <QtTest>
#include <QtRemoteObjects/QAbstractItemModelReplica>
#include <QtRemoteObjects/QRemoteObjectNode>
#include "rep_localdatacenter_replica.h"
#include "rep_localdatacenter_source.h"
class BenchmarksModel : public QAbstractListModel
{
// QAbstractItemModel interface
public:
int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
};
int BenchmarksModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 100000;
}
QVariant BenchmarksModel::data(const QModelIndex &index, int role) const
{
switch (role) {
case Qt::DisplayRole:
return QStringLiteral("Benchmark data %1").arg(index.row());
case Qt::BackgroundRole:
return index.row() % 2 ? QStringLiteral("red") : QStringLiteral("green");
}
return QVariant();
}
QHash<int, QByteArray> BenchmarksModel::roleNames() const
{
static QHash<int,QByteArray> roleNames = {
{Qt::DisplayRole, "_text"},
{Qt::BackgroundRole, "_color"}
};
return roleNames;
}
class BenchmarksTest : public QObject
{
Q_OBJECT
public:
BenchmarksTest();
private:
QRemoteObjectHost m_basicServer;
QRemoteObjectNode m_basicClient;
QScopedPointer<LocalDataCenterSimpleSource> dataCenterLocal;
BenchmarksModel m_sourceModel;
private Q_SLOTS:
void initTestCase();
void benchPropertyChangesInt();
void benchQDataStreamInt();
void benchQLocalSocketInt();
void benchQLocalSocketQDataStreamInt();
void benchModelLinearAccess();
void benchModelRandomAccess();
};
BenchmarksTest::BenchmarksTest()
{
}
void BenchmarksTest::initTestCase() {
m_basicServer.setHostUrl(QUrl(QStringLiteral("local:benchmark_replica")));
dataCenterLocal.reset(new LocalDataCenterSimpleSource);
dataCenterLocal->setData1(5);
const bool remoted = m_basicServer.enableRemoting(dataCenterLocal.data());
Q_ASSERT(remoted);
Q_UNUSED(remoted);
m_basicClient.connectToNode(QUrl(QStringLiteral("local:benchmark_replica")));
Q_ASSERT(m_basicClient.lastError() == QRemoteObjectNode::NoError);
m_basicServer.enableRemoting(&m_sourceModel, QStringLiteral("BenchmarkRemoteModel"),
m_sourceModel.roleNames().keys().toVector());
}
void BenchmarksTest::benchPropertyChangesInt()
{
QScopedPointer<LocalDataCenterReplica> center;
center.reset(m_basicClient.acquire<LocalDataCenterReplica>());
if (!center->isInitialized()) {
QEventLoop loop;
connect(center.data(), &LocalDataCenterReplica::initialized, &loop, &QEventLoop::quit);
loop.exec();
}
QEventLoop loop;
int lastValue = 0;
connect(center.data(), &LocalDataCenterReplica::data1Changed, [&lastValue, ¢er, &loop]() {
const bool res = (lastValue++ == center->data1());
Q_ASSERT(res);
Q_UNUSED(res);
if (lastValue == 50000)
loop.quit();
});
QBENCHMARK {
for (int i = 0; i < 50000; ++i) {
dataCenterLocal->setData1(i);
}
loop.exec();
}
}
// This ONLY tests the optimal case of a non resizing QByteArray
void BenchmarksTest::benchQDataStreamInt()
{
QByteArray buffer;
QDataStream stream(&buffer, QIODevice::WriteOnly);
QDataStream rStream(&buffer, QIODevice::ReadOnly);
int readout = 0;
QBENCHMARK {
for (int i = 0; i < 50000; ++i) {
stream << i;
}
for (int i = 0; i < 50000; ++i) {
rStream >> readout;
Q_ASSERT(i == readout);
}
}
}
void BenchmarksTest::benchQLocalSocketInt()
{
const QString socketName = QStringLiteral("benchLocalSocket");
QLocalServer server;
QLocalServer::removeServer(socketName);
server.listen(socketName);
QLocalSocket client;
client.connectToServer(socketName);
QEventLoop loop;
QScopedPointer<QLocalSocket> serverSock;
if (!server.hasPendingConnections()) {
connect(&server, &QLocalServer::newConnection, &loop, &QEventLoop::quit);
loop.exec();
}
Q_ASSERT(server.hasPendingConnections());
serverSock.reset(server.nextPendingConnection());
int lastValue = 0;
connect(&client, &QLocalSocket::readyRead, [&loop, &lastValue, &client]() {
int readout = 0;
while (client.bytesAvailable() && lastValue < 50000) {
client.read(reinterpret_cast<char*>(&readout), sizeof(int));
const bool res = (lastValue++ == readout);
Q_ASSERT(res);
Q_UNUSED(res);
}
if (lastValue >= 50000)
loop.quit();
});
QBENCHMARK {
for (int i = 0; i < 50000; ++i) {
const int res = serverSock->write(reinterpret_cast<char*>(&i), sizeof(int));
Q_ASSERT(res == sizeof(int));
Q_UNUSED(res);
}
loop.exec();
}
#ifdef Q_OS_WIN
// Work-around QTBUG-38185: immediately close socket
client.abort();
#endif
}
void BenchmarksTest::benchQLocalSocketQDataStreamInt()
{
const QString socketName = QStringLiteral("benchLocalSocket");
QLocalServer server;
QLocalServer::removeServer(socketName);
server.listen(socketName);
QLocalSocket client;
client.connectToServer(socketName);
QDataStream readStream(&client);
QEventLoop loop;
QScopedPointer<QLocalSocket> serverSock;
if (!server.hasPendingConnections()) {
connect(&server, &QLocalServer::newConnection, &loop, &QEventLoop::quit);
loop.exec();
}
Q_ASSERT(server.hasPendingConnections());
serverSock.reset(server.nextPendingConnection());
QDataStream writeStream(serverSock.data());
int lastValue = 0;
connect(&client, &QIODevice::readyRead, [&loop, &lastValue, &readStream]() {
int readout = 0;
while (readStream.device()->bytesAvailable() && lastValue < 50000) {
readStream >> readout;
const bool res = (lastValue++ == readout);
Q_ASSERT(res);
Q_UNUSED(res);
}
if (lastValue >= 50000)
loop.quit();
});
QBENCHMARK {
for (int i = 0; i < 50000; ++i) {
writeStream << i;
}
loop.exec();
}
#ifdef Q_OS_WIN
// Work-around QTBUG-38185: immediately close socket
client.abort();
#endif
}
void BenchmarksTest::benchModelLinearAccess()
{
// Simulate an user browse through item them stops.
// We're measuring the time needed needed to deliver the visible chunk of data
// which are the last 50 items
QBENCHMARK {
QRemoteObjectNode localClient;
localClient.connectToNode(QUrl(QStringLiteral("local:benchmark_replica")));
QScopedPointer<QAbstractItemModelReplica> model(localClient.acquireModel(QStringLiteral("BenchmarkRemoteModel")));
QEventLoop loop;
QHash<int, QPair<QString, QString>> dataToWait;
connect(model.data(), &QAbstractItemModelReplica::dataChanged, [&model, &loop, &dataToWait](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles) {
for (int row = topLeft.row(); row <= bottomRight.row(); ++row) {
// we're assuming that the view will try use the sent data,
// therefore we're not optimizing the code
auto it = dataToWait.find(row);
if (it == dataToWait.end()) {
// simulate some work with the received data
QThread::usleep(10);
continue;
}
for (int role : roles) {
QVariant data = model->data(model->index(row, 0), role);
switch (role) {
case Qt::DisplayRole:
it->first = data.toString();
break;
case Qt::BackgroundRole:
it->second = data.toString();
break;
}
}
if (it->first == QStringLiteral("Benchmark data %1").arg(row) &&
it->second == (row % 2 ? QStringLiteral("red") : QStringLiteral("green"))) {
dataToWait.erase(it);
if (dataToWait.isEmpty())
break;
}
}
if (dataToWait.isEmpty())
loop.quit();
});
auto beginBenchmark = [&model, &dataToWait] {
for (int row = 0; row < 1000; ++row) {
if (row >= 950)
dataToWait.insert(row, QPair<QString, QString>());
model->data(model->index(row, 0), Qt::DisplayRole);
model->data(model->index(row, 0), Qt::BackgroundRole);
// Views (e.g. QTreeView) are accessing other roles
model->data(model->index(row, 0), Qt::FontRole);
model->data(model->index(row, 0), Qt::DecorationRole);
model->data(model->index(row, 0), Qt::SizeHintRole);
}
};
connect(model.data(), &QAbstractItemModelReplica::initialized, [&model, &loop, &beginBenchmark] {
if (model->isInitialized()) {
beginBenchmark();
} else {
Q_ASSERT(false);
loop.quit();
}
});
if (model->isInitialized())
beginBenchmark();
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY(dataToWait.isEmpty());
}
}
void BenchmarksTest::benchModelRandomAccess()
{
QBENCHMARK {
QRemoteObjectNode localClient;
localClient.connectToNode(QUrl(QStringLiteral("local:benchmark_replica")));
QScopedPointer<QAbstractItemModelReplica> model(localClient.acquireModel(QStringLiteral("BenchmarkRemoteModel")));
model->setRootCacheSize(5000); // we need to make room for all 5000 rows that we'll use
QEventLoop loop;
QHash<int, QPair<QString, QString>> dataToWait;
connect(model.data(), &QAbstractItemModelReplica::dataChanged, [&model, &loop, &dataToWait](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles) {
for (int row = topLeft.row(); row <= bottomRight.row(); ++row) {
// we're assuming that the view will try use the sent data,
// therefore we're not optimizing the code
auto it = dataToWait.find(row);
if (it == dataToWait.end()) {
QThread::yieldCurrentThread(); // instead to wait
continue;
}
for (int role : roles) {
QVariant data = model->data(model->index(row, 0), role);
switch (role) {
case Qt::DisplayRole:
it->first = data.toString();
break;
case Qt::BackgroundRole:
it->second = data.toString();
break;
}
}
if (it->first == QStringLiteral("Benchmark data %1").arg(row) &&
it->second == (row % 2 ? QStringLiteral("red") : QStringLiteral("green"))) {
dataToWait.erase(it);
if (dataToWait.isEmpty())
break;
}
}
if (dataToWait.isEmpty())
loop.quit();
});
auto beginBenchmark = [&model, &dataToWait] {
for (int chunck = 0; chunck < 100; ++chunck) {
int row = chunck * 950;
for (int r = 0; r < 50; ++r) {
dataToWait.insert(r + row, QPair<QString, QString>());
model->data(model->index(r + row, 0), Qt::DisplayRole);
model->data(model->index(r + row, 0), Qt::BackgroundRole);
// Views (e.g. QTreeView) are accessing other roles
model->data(model->index(r + row, 0), Qt::FontRole);
model->data(model->index(r + row, 0), Qt::DecorationRole);
model->data(model->index(r + row, 0), Qt::SizeHintRole);
}
}
};
connect(model.data(), &QAbstractItemModelReplica::initialized, [&model, &loop, &beginBenchmark] {
if (model->isInitialized()) {
beginBenchmark();
} else {
Q_ASSERT(false);
loop.quit();
}
});
if (model->isInitialized())
beginBenchmark();
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY(dataToWait.isEmpty());
}
}
QTEST_MAIN(BenchmarksTest)
#include "tst_benchmarkstest.moc"
|