| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2020 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <qtest.h> |
| 30 | |
| 31 | #include <QtCore/qmath.h> |
| 32 | #include <QtCore/QElapsedTimer> |
| 33 | #include <QtWidgets/QWidget> |
| 34 | |
| 35 | #include "benchmarktests.h" |
| 36 | |
| 37 | class BenchWidget : public QWidget |
| 38 | { |
| 39 | public: |
| 40 | BenchWidget(Benchmark *benchmark); |
| 41 | |
| 42 | void paintEvent(QPaintEvent *event); |
| 43 | |
| 44 | bool done() const { return m_done; } |
| 45 | qreal result() const { return m_result; } |
| 46 | |
| 47 | public: |
| 48 | QElapsedTimer timer; |
| 49 | |
| 50 | Benchmark *m_benchmark; |
| 51 | |
| 52 | bool m_done; |
| 53 | qreal m_result; |
| 54 | |
| 55 | uint m_total; |
| 56 | uint m_iteration; |
| 57 | |
| 58 | QVector<uint> iterationTimes; |
| 59 | }; |
| 60 | |
| 61 | void BenchWidget::paintEvent(QPaintEvent *) |
| 62 | { |
| 63 | if (m_done) |
| 64 | return; |
| 65 | |
| 66 | QPainter p(this); |
| 67 | |
| 68 | m_benchmark->begin(&p, 100); |
| 69 | |
| 70 | PaintingRectAdjuster adjuster; |
| 71 | adjuster.setNewBenchmark(m_benchmark); |
| 72 | adjuster.reset(rect()); |
| 73 | |
| 74 | for (int i = 0; i < 100; ++i) |
| 75 | m_benchmark->draw(&p, adjuster.newPaintingRect(), i); |
| 76 | |
| 77 | m_benchmark->end(&p); |
| 78 | |
| 79 | ++m_iteration; |
| 80 | |
| 81 | uint currentElapsed = timer.isValid() ? timer.elapsed() : 0; |
| 82 | timer.restart(); |
| 83 | |
| 84 | m_total += currentElapsed; |
| 85 | |
| 86 | // warm up for at most 5 iterations or half a second |
| 87 | if (m_iteration >= 5 || m_total >= 500) { |
| 88 | iterationTimes << currentElapsed; |
| 89 | |
| 90 | if (iterationTimes.size() >= 5) { |
| 91 | qreal mean = 0; |
| 92 | qreal stddev = 0; |
| 93 | uint min = INT_MAX; |
| 94 | |
| 95 | for (int i = 0; i < iterationTimes.size(); ++i) { |
| 96 | mean += iterationTimes.at(i); |
| 97 | min = qMin(min, iterationTimes.at(i)); |
| 98 | } |
| 99 | |
| 100 | mean /= qreal(iterationTimes.size()); |
| 101 | |
| 102 | for (int i = 0; i < iterationTimes.size(); ++i) { |
| 103 | qreal delta = iterationTimes.at(i) - mean; |
| 104 | stddev += delta * delta; |
| 105 | } |
| 106 | |
| 107 | stddev = qSqrt(stddev / iterationTimes.size()); |
| 108 | |
| 109 | stddev = 100 * stddev / mean; |
| 110 | // do 50 iterations, break earlier if we spend more than 5 seconds or have a low std deviation after 2 seconds |
| 111 | if (iterationTimes.size() >= 50 || m_total >= 5000 || (m_total >= 2000 && stddev < 4)) { |
| 112 | m_result = min; |
| 113 | m_done = true; |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | BenchWidget::BenchWidget(Benchmark *benchmark) |
| 121 | : m_benchmark(benchmark) |
| 122 | , m_done(false) |
| 123 | , m_result(0) |
| 124 | , m_total(0) |
| 125 | , m_iteration(0) |
| 126 | { |
| 127 | setWindowTitle(benchmark->name()); |
| 128 | resize(640, 480); |
| 129 | } |
| 130 | |
| 131 | class tst_QtBench : public QObject |
| 132 | { |
| 133 | Q_OBJECT |
| 134 | |
| 135 | private slots: |
| 136 | void qtBench(); |
| 137 | void qtBench_data(); |
| 138 | }; |
| 139 | |
| 140 | QString makeString(int length) |
| 141 | { |
| 142 | const char chars[] = "abcd efgh ijkl mnop qrst uvwx yz!$. ABCD 1234"; |
| 143 | const int len = int(strlen(chars)); |
| 144 | |
| 145 | QString ret; |
| 146 | for (int j = 0; j < length; j++) { |
| 147 | ret += QChar(chars[(j * 97) % len]); |
| 148 | } |
| 149 | |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | void tst_QtBench::qtBench_data() |
| 154 | { |
| 155 | QTest::addColumn<void *>("benchmark"); |
| 156 | |
| 157 | QString shortString = makeString(5); |
| 158 | QString middleString = makeString(50); |
| 159 | QString longString = makeString(35) + "\n" |
| 160 | + makeString(45) + "\n" |
| 161 | + makeString(75); |
| 162 | QString superLongString = "Lorem ipsum dolor sit am\n" |
| 163 | "et, consectetur adipisci\n" |
| 164 | "ng elit. Integer mi leo,\n" |
| 165 | "interdum ut congue at, p\n" |
| 166 | "ulvinar et tellus. Quisq\n" |
| 167 | "ue pretium eleifend laci\n" |
| 168 | "nia. Ut semper gravida l\n" |
| 169 | "ectus in commodo. Vestib\n" |
| 170 | "ulum pharetra arcu in en\n" |
| 171 | "im ultrices hendrerit. P\n" |
| 172 | "ellentesque habitant mor\n" |
| 173 | "bi tristique senectus et\n" |
| 174 | "netus et malesuada fames\n" |
| 175 | "ac turpis egestas. Ut er\n" |
| 176 | "os sem, feugiat in eleme\n" |
| 177 | "ntum in, porta sit amet \n" |
| 178 | "neque. Fusce mi tellus, \n" |
| 179 | "congue non dapibus eget,\n" |
| 180 | "pharetra quis quam. Duis\n" |
| 181 | "dui massa, pulvinar ac s\n" |
| 182 | "odales pharetra, dictum \n" |
| 183 | "in enim. Phasellus a nis\n" |
| 184 | "i erat, sed pellentesque\n" |
| 185 | "mi. Curabitur sed."; |
| 186 | |
| 187 | QList<Benchmark *> benchmarks; |
| 188 | benchmarks << (new DrawText(shortString, DrawText::PainterMode)); |
| 189 | benchmarks << (new DrawText(middleString, DrawText::PainterMode)); |
| 190 | benchmarks << (new DrawText(longString, DrawText::PainterMode)); |
| 191 | benchmarks << (new DrawText(superLongString, DrawText::PainterMode)); |
| 192 | |
| 193 | benchmarks << (new DrawText(shortString, DrawText::PainterQPointMode)); |
| 194 | benchmarks << (new DrawText(middleString, DrawText::PainterQPointMode)); |
| 195 | benchmarks << (new DrawText(longString, DrawText::PainterQPointMode)); |
| 196 | benchmarks << (new DrawText(superLongString, DrawText::PainterQPointMode)); |
| 197 | |
| 198 | benchmarks << (new DrawText(shortString, DrawText::PixmapMode)); |
| 199 | benchmarks << (new DrawText(middleString, DrawText::PixmapMode)); |
| 200 | benchmarks << (new DrawText(longString, DrawText::PixmapMode)); |
| 201 | benchmarks << (new DrawText(superLongString, DrawText::PixmapMode)); |
| 202 | |
| 203 | benchmarks << (new DrawText(shortString, DrawText::StaticTextMode)); |
| 204 | benchmarks << (new DrawText(middleString, DrawText::StaticTextMode)); |
| 205 | benchmarks << (new DrawText(longString, DrawText::StaticTextMode)); |
| 206 | benchmarks << (new DrawText(superLongString, DrawText::StaticTextMode)); |
| 207 | |
| 208 | benchmarks << (new DrawText(shortString, DrawText::StaticTextWithMaximumSizeMode)); |
| 209 | benchmarks << (new DrawText(middleString, DrawText::StaticTextWithMaximumSizeMode)); |
| 210 | benchmarks << (new DrawText(longString, DrawText::StaticTextWithMaximumSizeMode)); |
| 211 | benchmarks << (new DrawText(superLongString, DrawText::StaticTextWithMaximumSizeMode)); |
| 212 | |
| 213 | benchmarks << (new DrawText(shortString, DrawText::StaticTextBackendOptimizations)); |
| 214 | benchmarks << (new DrawText(middleString, DrawText::StaticTextBackendOptimizations)); |
| 215 | benchmarks << (new DrawText(longString, DrawText::StaticTextBackendOptimizations)); |
| 216 | benchmarks << (new DrawText(superLongString, DrawText::StaticTextBackendOptimizations)); |
| 217 | |
| 218 | foreach (Benchmark *benchmark, benchmarks) |
| 219 | QTest::newRow(qPrintable(benchmark->name())) << reinterpret_cast<void *>(benchmark); |
| 220 | } |
| 221 | |
| 222 | void tst_QtBench::qtBench() |
| 223 | { |
| 224 | QFETCH(void *, benchmark); |
| 225 | |
| 226 | BenchWidget widget(reinterpret_cast<Benchmark *>(benchmark)); |
| 227 | widget.show(); |
| 228 | QVERIFY(QTest::qWaitForWindowExposed(&widget)); |
| 229 | |
| 230 | while (!widget.done()) { |
| 231 | widget.update(); |
| 232 | QApplication::processEvents(); |
| 233 | } |
| 234 | |
| 235 | QTest::setBenchmarkResult(widget.result(), QTest::WalltimeMilliseconds); |
| 236 | } |
| 237 | |
| 238 | QTEST_MAIN(tst_QtBench) |
| 239 | #include "tst_qtbench.moc" |