Debian Codegraph @

tests/benchmarks/widgets/itemviews/qheaderview/qheaderviewbench.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Thorbjørn Lund Martsum - tmartsum[at]gmail.com
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 <QtTest/QtTest>
30 #include <QtWidgets/QtWidgets>
31
32 class BenchQHeaderView : public QObject
33 {
34 Q_OBJECT
35 public:
36 BenchQHeaderView() : QObject() {m_tv = 0; m_hv = 0; m_model = 0;}
37
38 protected:
39 QTableView *m_tv;
40 QHeaderView *m_hv;
41 QStandardItemModel *m_model;
42 QStandardItemModel m_normalmodel;
43 QElapsedTimer t;
44 bool m_worst_case;
45 void setupTestData();
46
47 bool m_blockSomeSignals;
48 bool m_updatesEnabled;
49 int m_rowcount;
50 int m_colcount;
51
52
53 private slots:
54 void init();
55 void cleanupTestCase();
56 void initTestCase();
57 void cleanup();
58 void visualIndexAtSpecial_data() {setupTestData();}
59 void visualIndexAt_data() {setupTestData();}
60 void hideShowBench_data() {setupTestData();}
61 void swapSectionsBench_data() {setupTestData();}
62 void moveSectionBench_data() {setupTestData();}
63 void defaultSizeBench_data() {setupTestData();}
64 void removeBench_data() {setupTestData();}
65 void insertBench_data() {setupTestData();}
66 void truncBench_data() {setupTestData();}
67
68 void visualIndexAtSpecial();
69 void visualIndexAt();
70 void hideShowBench();
71 void swapSectionsBench();
72 void moveSectionBench();
73 void defaultSizeBench();
74 void removeBench();
75 void insertBench();
76 void truncBench();
77 };
78
79 void BenchQHeaderView::setupTestData()
80 {
81 QTest::addColumn<bool>("worst_case");
82 QTest::newRow("Less relevant best case") << false;
83 QTest::newRow("__* More important worst case *__") << true;
84 }
85
86 void BenchQHeaderView::initTestCase()
87 {
88 m_tv = new QTableView();
89 m_hv = m_tv->verticalHeader();
90 m_model = &m_normalmodel;
91 m_tv->setModel(m_model);
92 m_tv->show();
93 }
94
95 void BenchQHeaderView::cleanupTestCase()
96 {
97 delete m_tv;
98 m_tv = 0;
99 m_hv = 0;
100 }
101
102 void BenchQHeaderView::init()
103 {
104 QFETCH(bool, worst_case);
105
106 m_blockSomeSignals = true;
107 m_updatesEnabled = false;
108 m_rowcount = 2500;
109 m_colcount = 10;
110
111 m_worst_case = worst_case;
112 m_model->clear();
113 if (worst_case) {
114 for (int u = 0; u <= m_rowcount; ++u) // ensures fragment in Qt 4.x
115 m_model->setRowCount(u);
116 m_model->setColumnCount(m_colcount);
117 m_hv->swapSections(0, m_rowcount - 1);
118 m_hv->hideSection(m_rowcount / 2);
119 } else {
120 m_model->setColumnCount(m_colcount);
121 m_model->setRowCount(m_rowcount);
122 }
123
124 QString s;
125 for (int i = 0; i < m_model->rowCount(); ++i) {
126 m_model->setData(m_model->index(i, 0), QVariant(i));
127 s.setNum(i);
128 s += QLatin1Char('.');
129 s += 'a' + (i % 25);
130 m_model->setData(m_model->index(i, 1), QVariant(s));
131 }
132 m_tv->setUpdatesEnabled(m_updatesEnabled);
133 m_hv->blockSignals(m_blockSomeSignals);
134
135 const int default_section_size = 25;
136 m_hv->setDefaultSectionSize(default_section_size);
137 }
138
139 void BenchQHeaderView::cleanup()
140 {
141 m_tv->setUpdatesEnabled(true);
142 m_hv->blockSignals(false);
143 }
144
145 void BenchQHeaderView::visualIndexAtSpecial()
146 {
147 int lookup_pos = m_hv->length() - 50;
148 int testnum = 0;
149
150 QBENCHMARK {
151 ++testnum;
152 m_hv->resizeSection(0, testnum % 47);
153 m_hv->visualIndexAt(lookup_pos);
154 }
155 }
156
157 void BenchQHeaderView::visualIndexAt()
158 {
159 const int center_pos = m_hv->length() / 2;
160 const int maxpos = m_hv->length() - 1;
161
162 QBENCHMARK {
163 m_hv->visualIndexAt(0);
164 m_hv->visualIndexAt(center_pos);
165 m_hv->visualIndexAt(maxpos);
166 }
167 }
168
169 void BenchQHeaderView::hideShowBench()
170 {
171 int n = 0;
172 bool hide_set = true;
173
174 QBENCHMARK {
175 m_hv->setSectionHidden(n, hide_set);
176 if (n >= m_hv->count()) {
177 n = -1;
178 hide_set = !hide_set;
179 }
180 ++n;
181 }
182 }
183
184 void BenchQHeaderView::swapSectionsBench()
185 {
186 int n = 0;
187 QBENCHMARK {
188 m_hv->swapSections(n, n + 1);
189 if (++n >= m_hv->count())
190 n = 0;
191 }
192 }
193
194 void BenchQHeaderView::moveSectionBench()
195 {
196 QBENCHMARK {
197 m_hv->moveSection(0, m_hv->count() - 2);
198 }
199 }
200
201 void BenchQHeaderView::defaultSizeBench()
202 {
203 int n = 1;
204 QBENCHMARK {
205 m_hv->setDefaultSectionSize(n);
206 ++n;
207 }
208 }
209
210 void BenchQHeaderView::removeBench()
211 {
212 QBENCHMARK {
213 m_model->removeRows(0, 1);
214 if (m_hv->count() == 0) { // setup a new hard model
215 m_model->setRowCount(m_rowcount);
216 if (m_worst_case) {
217 m_hv->swapSections(0, m_rowcount - 1);
218 m_hv->hideSection(m_rowcount / 2);
219 }
220 }
221 }
222 }
223
224 void BenchQHeaderView::insertBench()
225 {
226 QBENCHMARK {
227 m_model->insertRows(1, 1);
228 if (m_hv->count() == 10000000) { // setup a new hard model
229 m_model->setRowCount(m_rowcount);
230 if (m_worst_case) {
231 m_hv->swapSections(0, m_rowcount - 1);
232 m_hv->hideSection(m_rowcount / 2);
233 }
234 }
235 }
236 }
237
238 void BenchQHeaderView::truncBench()
239 {
240 QBENCHMARK {
241 m_model->setRowCount(1);
242 m_model->setRowCount(m_rowcount);
243 if (m_worst_case) {
244 m_hv->swapSections(0, m_rowcount - 1);
245 m_hv->hideSection(m_rowcount / 2);
246 }
247 }
248 }
249
250 QTEST_MAIN(BenchQHeaderView)
251 #include "qheaderviewbench.moc"