aboutsummaryrefslogtreecommitdiffstats
path: root/tools/compareresults/main.cpp
blob: 899a6bcc9550db6f78ac6f536530096a124a8c82 (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
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the qmlbench tool.
**
** $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 <QCoreApplication>
#include <QCommandLineParser>
#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>
#include <QSet>
#include <QVector>

#include <algorithm>

static QJsonObject loadFile(const QString &fileName, QString *error)
{
    QFile f(fileName);
    if (!f.open(QIODevice::ReadOnly)) {
        *error = f.errorString();
        return QJsonObject();
    }
    QJsonParseError jsonError;
    auto document = QJsonDocument::fromJson(f.readAll(), &jsonError);
    if (jsonError.error != QJsonParseError::NoError) {
        *error = jsonError.errorString();
        return QJsonObject();
    }

    if (!document.isObject()) {
        *error = QLatin1String("JSON data is an array, expected an object");
        return QJsonObject();
    }

    return document.object();
}

struct Result
{
    QString name;
    double differenceInPercent = 0;
};

static QString trimPrefix(const QString &str, const QString &prefix)
{
    if (!str.startsWith(prefix))
        return str;
    QString result(str);
    result.remove(0, prefix.length());
    return result;
}

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    QCommandLineParser parser;
    parser.setApplicationDescription("Compare results of json output of qmlbench");
    parser.addHelpOption();

    QCommandLineOption keyOption("key");
    keyOption.setDescription(QCoreApplication::translate("main", "Result key in JSON data to compare"));
    keyOption.setDefaultValue("average");
    parser.addOption(keyOption);

    QCommandLineOption prefixOption("prefix");
    prefixOption.setDescription(QCoreApplication::translate("main", "Prefix to strip from test case names"));
    prefixOption.setValueName(QString("path"));
    parser.addOption(prefixOption);

    parser.addPositionalArgument("baseline", QCoreApplication::translate("main", "Base line or reference performance results, in JSON format"));
    parser.addPositionalArgument("results", QCoreApplication::translate("main", "New results to compare against the base line, in JSON format"));

    parser.process(app);

    const QString key = parser.value(keyOption);
    const QString prefix = parser.value(prefixOption);

    QStringList args = parser.positionalArguments();
    if (args.count() != 2) {
        qFatal("Internal error, two arguments are required, but QCommandLineParser should have checked for that...");
        return EXIT_FAILURE;
    }

    const QString baseLineFileName = args.at(0);
    const QString newResultsFileName = args.at(1);

    QString error;
    QJsonObject baseLine = loadFile(baseLineFileName, &error);
    if (!error.isEmpty()) {
        qWarning("Error loading reference file %s: %s", qPrintable(baseLineFileName), qPrintable(error));
        return EXIT_FAILURE;
    }

    QJsonObject newResults = loadFile(newResultsFileName, &error);
    if (!error.isEmpty()) {
        qWarning("Error loading new results file %s: %s", qPrintable(baseLineFileName), qPrintable(error));
        return EXIT_FAILURE;
    }

    const QSet<QString> unrelatedKeys{"command-line", "id", "opengl", "os", "qt", "windowSize"};

    auto filter = [&unrelatedKeys](const QStringList &list) -> QStringList {
        return list.toSet().subtract(unrelatedKeys).toList();
    };

    QStringList tests = filter(baseLine.keys());
    QStringList newResultKeys = filter(newResults.keys());
    if (tests != newResultKeys) {
        QSet<QString> baseLineSet = tests.toSet();
        QSet<QString> resultsSet = newResultKeys.toSet();

        qWarning("Error: The two result files do not cover the same set of tests");
        qWarning("Tests existing in the base line but missing from the new results: %s", qPrintable(baseLineSet.subtract(resultsSet).toList().join("\t\n")));
        qWarning("Tests existing in the new results but missing from the base line: %s", qPrintable(resultsSet.subtract(baseLineSet).toList().join("\t\n")));

        return EXIT_FAILURE;
    }

    QVector<Result> differencesInPercent;

    for (const QString &test: qAsConst(tests)) {
        const QJsonObject baseLineResult = baseLine[test].toObject();
        const QJsonObject newResult = newResults[test].toObject();
        const QString testName = trimPrefix(test, prefix);

        const double oldValue = baseLineResult[key].toDouble();
        const double newValue = newResult[key].toDouble();

        const double differenceInPercent = (newValue - oldValue) * 100 / oldValue;

        if (differenceInPercent > 0) {
            printf("%s: improvement by %.2f%%\n", qPrintable(testName), differenceInPercent);
        } else if (differenceInPercent < 0) {
            printf("%s: regression by %.2f%%\n", qPrintable(testName), differenceInPercent);
        }

        differencesInPercent << Result{testName, differenceInPercent};
    }

    auto minMax = std::minmax_element(differencesInPercent.constBegin(), differencesInPercent.constEnd(), [](const auto &lhs, const auto &rhs) {
        return lhs.differenceInPercent < rhs.differenceInPercent;
    });

    const Result biggestLoser = *minMax.first;
    const Result biggestWinner = *minMax.second;

    printf("Biggest improvement: %s with %.2f%%\n", qPrintable(biggestWinner.name), biggestWinner.differenceInPercent);
    printf("Biggest regression: %s with %.2f%%\n", qPrintable(biggestLoser.name), biggestLoser.differenceInPercent);

    double overallAverage = std::accumulate(differencesInPercent.constBegin(), differencesInPercent.constEnd(), 0.0, [](double v, const Result &r) {
        return v + r.differenceInPercent; })
        / double(differencesInPercent.count());
    printf("Overall average of differences: %.2f%%\n", overallAverage);

    return EXIT_SUCCESS;
}