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