Debian Codegraph @

tests/benchmarks/gui/painting/qtbench/tst_qtbench.cpp

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