summaryrefslogtreecommitdiffstats
path: root/tests/arthur/baselineserver/src/baselineserver.cpp
blob: 86554fd8e8a05a820b34b11d3ac4c420951b13b2 (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include "baselineserver.h"
#include <QBuffer>
#include <QFile>
#include <QDir>
#include <QCoreApplication>
#include <QFileInfo>
#include <QHostInfo>
#include <QTextStream>
#include <QProcess>

QString BaselineServer::storage;

BaselineServer::BaselineServer(QObject *parent)
    : QTcpServer(parent)
{
    QFileInfo me(QCoreApplication::applicationFilePath());
    meLastMod = me.lastModified();
    heartbeatTimer = new QTimer(this);
    connect(heartbeatTimer, SIGNAL(timeout()), this, SLOT(heartbeat()));
    heartbeatTimer->start(HEARTBEAT*1000);
}

QString BaselineServer::storagePath()
{
    if (storage.isEmpty()) {
        QDir dir(QCoreApplication::applicationDirPath());
        dir.cdUp();
        storage =  dir.path() + QLS("/storage/");
    }
    return storage;
}

QString BaselineServer::baseUrl()
{
    return QLS("http://")
            + QHostInfo::localHostName().toLatin1() + '.'
            + QHostInfo::localDomainName().toLatin1() + '/';
}

void BaselineServer::incomingConnection(int socketDescriptor)
{
    qDebug() << "Server: New connection!";
    BaselineThread *thread = new BaselineThread(socketDescriptor, this);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
}

void BaselineServer::heartbeat()
{
    // The idea is to exit to be restarted when modified, as soon as not actually serving
    QFileInfo me(QCoreApplication::applicationFilePath());
    if (me.lastModified() == meLastMod)
        return;

    // (could close() here to avoid accepting new connections, to avoid livelock)
    // also, could check for a timeout to force exit, to avoid hung threads blocking
    bool isServing = false;
    foreach(BaselineThread *thread, findChildren<BaselineThread *>()) {
        if (thread->isRunning()) {
            isServing = true;
            break;
        }
    }

    if (!isServing)
        QCoreApplication::exit();
}

BaselineThread::BaselineThread(int socketDescriptor, QObject *parent)
    : QThread(parent), socketDescriptor(socketDescriptor)
{
}

void BaselineThread::run()
{
    BaselineHandler handler(socketDescriptor);
    exec();
}


BaselineHandler::BaselineHandler(int socketDescriptor)
    : QObject(), connectionEstablished(false)
{
    runId = QDateTime::currentDateTime().toString(QLS("MMMdd-hhmmss"));

    if (socketDescriptor == -1)
        return;

    connect(&proto.socket, SIGNAL(readyRead()), this, SLOT(receiveRequest()));
    connect(&proto.socket, SIGNAL(disconnected()), this, SLOT(receiveDisconnect()));
    proto.socket.setSocketDescriptor(socketDescriptor);
}

QString BaselineHandler::logtime()
{
    return QTime::currentTime().toString(QLS("mm:ss.zzz"));
}

void BaselineHandler::receiveRequest()
{
    if (!connectionEstablished) {
        if (!proto.acceptConnection(&plat)) {
            qWarning() << runId << logtime() << "Accepting new connection failed. " << proto.errorMessage();
            QThread::currentThread()->exit(1);
            return;
        }
        connectionEstablished = true;
        qDebug() << runId << logtime() << "Connection established with" << plat.hostName << "[" << proto.socket.peerAddress().toString() << "]"
                 << "OS:" << plat.osName << "[" << plat.osVersion << "]" << "Qt version:" << plat.qtVersion << "[" << plat.buildKey << "]"
                 << "git commit:" << plat.gitCommit;
                    return;
    }

    QByteArray block;
    BaselineProtocol::Command cmd;
    if (!proto.receiveBlock(&cmd, &block)) {
        qWarning() << runId << logtime() << "Command reception failed. "<< proto.errorMessage();
        QThread::currentThread()->exit(1);
        return;
    }

    switch(cmd) {
    case BaselineProtocol::RequestBaselineChecksums:
        provideBaselineChecksums(block);
        break;
    case BaselineProtocol::AcceptNewBaseline:
        storeImage(block, true);
        break;
    case BaselineProtocol::AcceptMismatch:
        storeImage(block, false);
        break;
    default:
        qWarning() << runId << logtime() << "Unknown command received. " << proto.errorMessage();
        proto.sendBlock(BaselineProtocol::UnknownError, QByteArray());
    }
}


void BaselineHandler::provideBaselineChecksums(const QByteArray &itemListBlock)
{
    ImageItemList itemList;
    QDataStream ds(itemListBlock);
    ds >> itemList;
    qDebug() << runId << logtime() << "Received request for checksums for" << itemList.count() << "items, engine"
             << itemList.at(0).engineAsString() << "pixel format" << itemList.at(0).formatAsString();

    for (ImageItemList::iterator i = itemList.begin(); i != itemList.end(); ++i) {
        i->imageChecksums.clear();
        QString prefix = pathForItem(*i, true);
        QFile file(prefix + QLS("metadata"));
        if (file.open(QIODevice::ReadOnly)) {
            QDataStream checkSums(&file);
            checkSums >> i->imageChecksums;
            file.close();
            i->status = ImageItem::Ok;
        }
        if (!i->imageChecksums.count())
            i->status = ImageItem::BaselineNotFound;
    }

    // Find and mark blacklisted items
    if (itemList.count() > 0) {
        QString prefix = pathForItem(itemList.at(0), true).section(QLC('/'), 0, -2);
        QFile file(prefix + QLS("/.blacklist"));
        if (file.open(QIODevice::ReadOnly)) {
            QTextStream in(&file);
            do {
                QString scriptName = in.readLine();
                if (!scriptName.isNull()) {
                    for (ImageItemList::iterator i = itemList.begin(); i != itemList.end(); ++i) {
                        if (i->scriptName == scriptName)
                            i->status = ImageItem::IgnoreItem;
                    }
                }
            } while (!in.atEnd());
        }
    }

    QByteArray block;
    QDataStream ods(&block, QIODevice::WriteOnly);
    ods << itemList;
    proto.sendBlock(BaselineProtocol::Ack, block);
    report.start(BaselineServer::storagePath(), runId, plat, itemList);
}


void BaselineHandler::storeImage(const QByteArray &itemBlock, bool isBaseline)
{
    QDataStream ds(itemBlock);
    ImageItem item;
    ds >> item;

    QString prefix = pathForItem(item, isBaseline);
    qDebug() << runId << logtime() << "Received" << (isBaseline ? "baseline" : "mismatched") << "image for:" << item.scriptName << "Storing in" << prefix;

    QString dir = prefix.section(QLC('/'), 0, -2);
    QDir cwd;
    if (!cwd.exists(dir))
        cwd.mkpath(dir);
    item.image.save(prefix + QLS(FileFormat), FileFormat);

    //# Could use QSettings or XML or even DB, could use common file for whole dir or even whole storage - but for now, keep it simple
    QFile file(prefix + QLS("metadata"));
    file.open(QIODevice::WriteOnly | QIODevice::Truncate);
    QDataStream checkSums(&file);
    checkSums << item.imageChecksums;
    file.close();

    if (!isBaseline)
        report.addItem(pathForItem(item, true, false) + QLS(FileFormat),
                       pathForItem(item, false, false) + QLS(FileFormat),
                       item);

    QByteArray msg(isBaseline ? "New baseline image stored: " :
                                "Mismatch report: " );
    msg += BaselineServer::baseUrl();
    if (isBaseline)
        msg += pathForItem(item, true, false).toLatin1() + FileFormat;
    else
        msg += report.filePath();

    proto.sendBlock(BaselineProtocol::Ack, msg);
}


void BaselineHandler::receiveDisconnect()
{
    qDebug() << runId << logtime() << "Client disconnected.";
    report.end();
    QThread::currentThread()->exit(0);
}


QString BaselineHandler::itemSubPath(const QString &engine, const QString &format, bool isBaseline)
{
    if (isBaseline)
        return QString(QLS("baselines_%1_%2/")).arg(engine, format);
    else
        return QString(QLS("mismatches_%1_%2/")).arg(engine, format);
}

QString BaselineHandler::pathForItem(const ImageItem &item, bool isBaseline, bool absolute)
{
    if (pathForRun.isNull()) {
        QString host = plat.hostName.section(QLC('.'), 0, 0);  // Filter away domain, if any
        if (host.isEmpty() || host == QLS("localhost")) {
            host = proto.socket.peerAddress().toString();
            if (host.isEmpty())
                host = QLS("Unknown");
        } else {
            host.replace(QRegExp(QLS("^(bq|oslo?)-(.*)$")), QLS("\\2"));
            host.replace(QRegExp(QLS("^(.*)-\\d+$")), QLS("vm-\\1"));
        }
        pathForRun = host + QLC('/');
    }

    QString storePath = pathForRun;
    if (isBaseline)
        storePath += itemSubPath(item.engineAsString(), item.formatAsString(), isBaseline);
    else
        storePath += itemSubPath(item.engineAsString(), item.formatAsString(), isBaseline) + runId + QLC('/');

    QString itemName = item.scriptName;
    if (itemName.contains(QLC('.')))
        itemName.replace(itemName.lastIndexOf(QLC('.')), 1, QLC('_'));
    itemName.append(QLC('_'));
    itemName.append(QString::number(item.scriptChecksum, 16).rightJustified(4, QLC('0')));

    if (absolute)
        storePath.prepend(BaselineServer::storagePath());
    return storePath + itemName + QLC('.');
}


QString BaselineHandler::updateAllBaselines(const QString &host, const QString &id,
                                            const QString &engine, const QString &format)
{
    QString basePath(BaselineServer::storagePath());
    QString srcDir(basePath + host + QLC('/') + itemSubPath(engine, format, false) + id);
    QString dstDir(basePath + host + QLC('/') + itemSubPath(engine, format));

    QDir dir(srcDir);
    QStringList nameFilter;
    nameFilter << "*.metadata" << "*.png";
    QStringList fileList = dir.entryList(nameFilter, QDir::Files | QDir::NoDotAndDotDot);

    // remove the generated _fuzzycompared.png and _compared.png files from the list
    QMutableStringListIterator it(fileList);
    while (it.hasNext()) {
        it.next();
        if (it.value().endsWith(QLS("compared.png")))
            it.remove();
    }

    QString res;
    QProcess proc;
    proc.setWorkingDirectory(srcDir);
    proc.setProcessChannelMode(QProcess::MergedChannels);
    proc.start(QLS("cp"), QStringList() << QLS("-f") << fileList << dstDir);
    proc.waitForFinished();
    if (proc.exitCode() == 0)
        res = QLS("Successfully updated baseline for all failed tests.");
    else
        res = QString("Error updating baseline: %1<br>"
                      "Command output: <pre>%2</pre>").arg(proc.errorString(), proc.readAll().constData());

    return res;
}

QString BaselineHandler::updateSingleBaseline(const QString &oldBaseline, const QString &newBaseline)
{
    QString res;
    QString basePath(BaselineServer::storagePath());
    QString srcBase(basePath + newBaseline.left(newBaseline.length() - 3));
    QString dstDir(basePath + oldBaseline.left(oldBaseline.lastIndexOf(QLC('/'))));

    QProcess proc;
    proc.setProcessChannelMode(QProcess::MergedChannels);
    proc.start(QLS("cp"), QStringList() << QLS("-f") << srcBase + QLS("png") << srcBase + QLS("metadata") << dstDir);
    proc.waitForFinished();
    if (proc.exitCode() == 0)
        res = QString("Successfully updated '%1'").arg(oldBaseline + QLS("/metadata"));
    else
        res = QString("Error updating baseline: %1<br>"
                      "Command output: <pre>%2</pre>").arg(proc.errorString(), proc.readAll().constData());

    return res;
}

QString BaselineHandler::blacklistTest(const QString &scriptName, const QString &host, const QString &engine,
                                       const QString &format)
{
    QString configFile(BaselineServer::storagePath() + host + QLC('/')
                       + itemSubPath(engine, format) + QLS(".blacklist"));
    QFile file(configFile);
    if (file.open(QIODevice::Append)) {
        QTextStream out(&file);
        out << scriptName << endl;
        return QLS("Blacklisted ") + scriptName;
    } else {
        return QLS("Unable to update blacklisted tests.");
    }
}

QString BaselineHandler::whitelistTest(const QString &scriptName, const QString &host, const QString &engine,
                                       const QString &format)
{
    QString configFile(BaselineServer::storagePath() + host + QLC('/')
                       + itemSubPath(engine, format) + QLS(".blacklist"));
    QFile file(configFile);
    QStringList tests;
    if (file.open(QIODevice::ReadOnly)) {
        QTextStream in(&file);
        do {
            tests << in.readLine();
        } while (!in.atEnd());
        if (tests.count() != 0) {
            QMutableStringListIterator it(tests);
            while (it.hasNext()) {
                it.next();
                if (it.value() == scriptName)
                    it.remove();
            }
        }
        file.close();
        if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
            QTextStream out(&file);
            for (int i=0; i<tests.count(); ++i)
                out << tests.at(i);
            return QLS("Whitelisted ") + scriptName;
        } else {
            QLS("Unable to whitelist ") + scriptName + QLS(". Unable to truncate blacklist file.");
        }
    }
    return QLS("Unable to whitelist ") + scriptName + QLS(". Unable to open blacklist file.");
}

void BaselineHandler::testPathMapping()
{
    qDebug() << "Storage prefix:" << BaselineServer::storagePath();

    QStringList hosts;
    hosts << QLS("bq-ubuntu910-x86-01")
          << QLS("bq-ubuntu910-x86-15")
          << QLS("osl-mac-master-5.test.qt.nokia.com")
          << QLS("osl-mac-master-6.test.qt.nokia.com")
          << QLS("sv-xp-vs-010")
          << QLS("sv-xp-vs-011")
          << QLS("chimera")
          << QLS("localhost");

    ImageItem item;
    item.scriptName = QLS("arcs.qps");
    item.engine = ImageItem::Raster;
    item.renderFormat = QImage::Format_ARGB32_Premultiplied;
    item.imageChecksums << 0x0123456789abcdefULL;
    item.scriptChecksum = 0x0123;

    plat.qtVersion = QLS("4.8.0");
    plat.buildKey = QLS("(nobuildkey)");
    foreach(const QString& host, hosts) {
        pathForRun = QString();
        plat.hostName = host;
        qDebug() << "Baseline from" << host << "->" << pathForItem(item, true).remove(BaselineServer::storagePath());
        qDebug() << "Mismatch from" << host << "->" << pathForItem(item, false).remove(BaselineServer::storagePath());
    }
}


QString BaselineHandler::computeMismatchScore(const QImage &baseline, const QImage &rendered)
{
    if (baseline.size() != rendered.size() || baseline.format() != rendered.format())
        return QLS("[No score, incomparable images.]");
    if (baseline.depth() != 32)
        return QLS("[Score computation not implemented for format.]");

    int w = baseline.width();
    int h = baseline.height();

    uint ncd = 0; // number of differing color pixels
    uint nad = 0; // number of differing alpha pixels
    uint scd = 0; // sum of color pixel difference
    uint sad = 0; // sum of alpha pixel difference

    for (int y=0; y<h; ++y) {
        const QRgb *bl = (const QRgb *) baseline.constScanLine(y);
        const QRgb *rl = (const QRgb *) rendered.constScanLine(y);
        for (int x=0; x<w; ++x) {
            QRgb b = bl[x];
            QRgb r = rl[x];
            if (r != b) {
                int dr = qAbs(qRed(b) - qRed(r));
                int dg = qAbs(qGreen(b) - qGreen(r));
                int db = qAbs(qBlue(b) - qBlue(r));
                int ds = dr + dg + db;
                int da = qAbs(qAlpha(b) - qAlpha(r));
                if (ds) {
                    ncd++;
                    scd += ds;
                }
                if (da) {
                    nad++;
                    sad += da;
                }
            }
        }
    }

    double pcd = 100.0 * ncd / (w*h);  // percent of pixels that differ
    double acd = ncd ? double(scd) / (3*ncd) : 0;         // avg. difference
    QString res = QString(QLS("Diffscore: %1% (Num:%2 Avg:%3)")).arg(pcd, 0, 'g', 2).arg(ncd).arg(acd, 0, 'g', 2);
    if (baseline.hasAlphaChannel()) {
        double pad = 100.0 * nad / (w*h);  // percent of pixels that differ
        double aad = nad ? double(sad) / (3*nad) : 0;         // avg. difference
        res += QString(QLS(" Alpha-diffscore: %1% (Num:%2 Avg:%3)")).arg(pad, 0, 'g', 2).arg(nad).arg(aad, 0, 'g', 2);
    }
    return res;
}