aboutsummaryrefslogtreecommitdiffstats
path: root/src/resultrecorder.cpp
blob: 574d89eb520931baa8f60c8c2d6a95b5d7f9efd3 (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
/****************************************************************************
**
** Copyright (C) 2017 Crimson AS <info@crimson.no>
** 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 <QGuiApplication>
#include <QOpenGLContext>
#include <QOffscreenSurface>
#include <QOpenGLFunctions>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>

#include <iostream>
#include <cmath>

#include "resultrecorder.h"
#include "options.h"

QVariantMap ResultRecorder::m_results;
bool ResultRecorder::opsAreActuallyFrames = false;

void ResultRecorder::startResults(const QString &id)
{
    // sub process will get all this.
    // safer that way, as then we keep OpenGL (and QGuiApplication) out of the host
    if (!Options::instance.isSubProcess)
        return;

    m_results["id"] = id;

    QString prettyProductName =
#if QT_VERSION >= 0x050400
        QSysInfo::prettyProductName();
#else
#  if defined(Q_OS_IOS)
        QStringLiteral("iOS");
#  elif defined(Q_OS_OSX)
        QString::fromLatin1("OSX %d").arg(QSysInfo::macVersion());
#  elif defined(Q_OS_WIN)
        QString::fromLatin1("Windows %d").arg(QSysInfo::windowsVersion());
#  elif defined(Q_OS_LINUX)
        QStringLiteral("Linux");
#  elif defined(Q_OS_ANDROID)
        QStringLiteral("Android");
#  else
        QStringLiteral("unknown");
#  endif
#endif

    QVariantMap osMap;
    osMap["prettyProductName"] = prettyProductName;
    osMap["platformPlugin"] = QGuiApplication::platformName();
    m_results["os"] = osMap;
    m_results["qt"] = QT_VERSION_STR;
    m_results["command-line"] = qApp->arguments().join(' ');

    // The following code makes the assumption that an OpenGL context the GUI
    // thread will get the same capabilities as the render thread's OpenGL
    // context. Not 100% accurate, but it works...
    QOpenGLContext context;
    context.create();
    QOffscreenSurface surface;
    // In very odd cases, we can get incompatible configs here unless we pass the
    // GL context's format on to the offscreen format.
    surface.setFormat(context.format());
    surface.create();
    if (!context.makeCurrent(&surface)) {
        qWarning() << "failed to acquire GL context to get version info.";
        return;
    }

    QOpenGLFunctions *func = context.functions();
#if QT_VERSION >= 0x050300
    const char *vendor = (const char *) func->glGetString(GL_VENDOR);
    const char *renderer = (const char *) func->glGetString(GL_RENDERER);
    const char *version = (const char *) func->glGetString(GL_VERSION);
#else
    Q_UNUSED(func);
    const char *vendor = (const char *) glGetString(GL_VENDOR);
    const char *renderer = (const char *) glGetString(GL_RENDERER);
    const char *version = (const char *) glGetString(GL_VERSION);
#endif

    if (!Options::instance.printJsonToStdout) {
        std::cout << "ID:          " << id.toStdString() << std::endl;
        std::cout << "OS:          " << prettyProductName.toStdString() << std::endl;
        std::cout << "QPA:         " << QGuiApplication::platformName().toStdString() << std::endl;
        std::cout << "GL_VENDOR:   " << vendor << std::endl;
        std::cout << "GL_RENDERER: " << renderer << std::endl;
        std::cout << "GL_VERSION:  " << version << std::endl;
    }

    QVariantMap glInfo;
    glInfo["vendor"] = vendor;
    glInfo["renderer"] = renderer;
    glInfo["version"] = version;

    m_results["opengl"] = glInfo;

    context.doneCurrent();
}

void ResultRecorder::recordWindowSize(const QSize &windowSize)
{
    m_results["windowSize"] = QString::number(windowSize.width()) + "x" + QString::number(windowSize.height());
}

void ResultRecorder::recordOperationsPerFrame(int ops)
{
    Q_ASSERT(Options::instance.isSubProcess);
    Benchmark &bm = Options::instance.benchmarks.first();
    QVariantMap benchMap = m_results[bm.fileName].toMap();
    QVariantList benchResults = benchMap["results"].toList();
    benchResults.append(ops);

    benchMap["results"] = benchResults;
    m_results[bm.fileName] = benchMap;

    if (opsAreActuallyFrames)
        std::cerr << "    " << ops << " frames" << std::endl;
    else
        std::cerr << "    " << ops << " ops/frame" << std::endl;
}

void ResultRecorder::recordOperationsPerFrameAverage(qreal ops, int samples, qreal stddev, qreal median)
{
    Q_ASSERT(Options::instance.isSubProcess);
    Benchmark &bm = Options::instance.benchmarks.first();
    QVariantMap benchMap = m_results[bm.fileName].toMap();
    benchMap["average"] = ops;
    benchMap["samples-in-average"] = samples;
    benchMap["samples-total"] = samples; // compatibility
    benchMap["standard-deviation"] = stddev;
    benchMap["standard-deviation-all-samples"] = stddev; // compatibility
    benchMap["standard-error"] = stddev / std::sqrt(samples);
    benchMap["coefficient-of-variation"] = stddev / ops;
    benchMap["median"] = median;

    std::string opsString;
    if (opsAreActuallyFrames)
        opsString = " frames";
    else
        opsString = " ops/frame";

    std::cerr << "    Average: " << ops << " " << opsString << ";"
              << "; MedianAll=" << median
              << "; StdDev=" << stddev
              << ", CoV=" << (stddev / ops)
              << std::endl;

    m_results[bm.fileName] = benchMap;
}

void ResultRecorder::mergeResults(const QJsonObject &o)
{
    // Merge the keys from the subprocess in where they should be.
    for (auto it = o.constBegin(); it != o.constEnd(); ++it) {
        m_results[it.key()] = it.value().toVariant();
    }
}

void ResultRecorder::finish()
{
    if (Options::instance.printJsonToStdout) {
        QJsonDocument results = QJsonDocument::fromVariant(m_results);
        std::cout << results.toJson().constData();
    }
    m_results.clear();
}