summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/widgets/itemviews/qheaderview/qheaderviewbench.cpp
blob: f04ca82cdc3c9662f4b9158f7322b7305621a942 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (C) 2012 Thorbjørn Lund Martsum - tmartsum[at]gmail.com
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QTest>
#include <QtWidgets/QtWidgets>

class BenchQHeaderView : public QObject
{
    Q_OBJECT
public:
    BenchQHeaderView() : QObject() {m_tv = 0; m_hv = 0; m_model = 0;}

protected:
    QTableView *m_tv;
    QHeaderView *m_hv;
    QStandardItemModel *m_model;
    QStandardItemModel m_normalmodel;
    QElapsedTimer t;
    bool m_worst_case;
    void setupTestData();

    bool m_blockSomeSignals;
    bool m_updatesEnabled;
    int m_rowcount;
    int m_colcount;


private slots:
    void init();
    void cleanupTestCase();
    void initTestCase();
    void cleanup();
    void visualIndexAtSpecial_data()   {setupTestData();}
    void visualIndexAt_data()          {setupTestData();}
    void hideShowBench_data()          {setupTestData();}
    void swapSectionsBench_data()      {setupTestData();}
    void moveSectionBench_data()       {setupTestData();}
    void defaultSizeBench_data()       {setupTestData();}
    void removeBench_data()            {setupTestData();}
    void insertBench_data()            {setupTestData();}
    void truncBench_data()             {setupTestData();}

    void visualIndexAtSpecial();
    void visualIndexAt();
    void hideShowBench();
    void swapSectionsBench();
    void moveSectionBench();
    void defaultSizeBench();
    void removeBench();
    void insertBench();
    void truncBench();
};

void BenchQHeaderView::setupTestData()
{
    QTest::addColumn<bool>("worst_case");
    QTest::newRow("Less relevant best case") << false;
    QTest::newRow("__* More important worst case *__") << true;
}

void BenchQHeaderView::initTestCase()
{
    m_tv = new QTableView();
    m_hv = m_tv->verticalHeader();
    m_model = &m_normalmodel;
    m_tv->setModel(m_model);
    m_tv->show();
}

void BenchQHeaderView::cleanupTestCase()
{
    delete m_tv;
    m_tv = 0;
    m_hv = 0;
}

void BenchQHeaderView::init()
{
    QFETCH(bool, worst_case);

    m_blockSomeSignals = true;
    m_updatesEnabled = false;
    m_rowcount = 2500;
    m_colcount = 10;

    m_worst_case = worst_case;
    m_model->clear();
    if (worst_case) {
        for (int  u = 0; u <= m_rowcount; ++u) // ensures fragment in Qt 4.x
            m_model->setRowCount(u);
        m_model->setColumnCount(m_colcount);
        m_hv->swapSections(0, m_rowcount - 1);
        m_hv->hideSection(m_rowcount / 2);
    } else {
        m_model->setColumnCount(m_colcount);
        m_model->setRowCount(m_rowcount);
    }

    QString s;
    for (int i = 0; i < m_model->rowCount(); ++i) {
        m_model->setData(m_model->index(i, 0), QVariant(i));
        s.setNum(i);
        s += QLatin1Char('.');
        s += QLatin1Char('a' + (i % 25));
        m_model->setData(m_model->index(i, 1), QVariant(s));
    }
    m_tv->setUpdatesEnabled(m_updatesEnabled);
    m_hv->blockSignals(m_blockSomeSignals);

    const int default_section_size = 25;
    m_hv->setDefaultSectionSize(default_section_size);
}

void BenchQHeaderView::cleanup()
{
    m_tv->setUpdatesEnabled(true);
    m_hv->blockSignals(false);
}

void BenchQHeaderView::visualIndexAtSpecial()
{
    int lookup_pos = m_hv->length() - 50;
    int testnum = 0;

    QBENCHMARK {
            ++testnum;
            m_hv->resizeSection(0, testnum % 47);
            m_hv->visualIndexAt(lookup_pos);
    }
}

void BenchQHeaderView::visualIndexAt()
{
    const int center_pos = m_hv->length() / 2;
    const int maxpos = m_hv->length() - 1;

    QBENCHMARK {
        m_hv->visualIndexAt(0);
        m_hv->visualIndexAt(center_pos);
        m_hv->visualIndexAt(maxpos);
    }
}

void BenchQHeaderView::hideShowBench()
{
    int n = 0;
    bool hide_set = true;

    QBENCHMARK {
        m_hv->setSectionHidden(n, hide_set);
        if (n >= m_hv->count()) {
            n = -1;
            hide_set = !hide_set;
        }
        ++n;
    }
}

void BenchQHeaderView::swapSectionsBench()
{
    int n = 0;
    QBENCHMARK {
        m_hv->swapSections(n, n + 1);
        if (++n >= m_hv->count())
            n = 0;
    }
}

void BenchQHeaderView::moveSectionBench()
{
    QBENCHMARK {
        m_hv->moveSection(0, m_hv->count() - 2);
    }
}

void BenchQHeaderView::defaultSizeBench()
{
    int n = 1;
    QBENCHMARK {
        m_hv->setDefaultSectionSize(n);
        ++n;
    }
}

void BenchQHeaderView::removeBench()
{
    QBENCHMARK {
        m_model->removeRows(0, 1);
        if (m_hv->count() == 0) { // setup a new hard model
            m_model->setRowCount(m_rowcount);
            if (m_worst_case) {
                m_hv->swapSections(0, m_rowcount - 1);
                m_hv->hideSection(m_rowcount / 2);
            }
        }
    }
}

void BenchQHeaderView::insertBench()
{
    QBENCHMARK {
        m_model->insertRows(1, 1);
        if (m_hv->count() == 10000000) { // setup a new hard model
            m_model->setRowCount(m_rowcount);
            if (m_worst_case) {
                m_hv->swapSections(0, m_rowcount - 1);
                m_hv->hideSection(m_rowcount / 2);
            }
        }
    }
}

void BenchQHeaderView::truncBench()
{
    QBENCHMARK {
        m_model->setRowCount(1);
        m_model->setRowCount(m_rowcount);
        if (m_worst_case) {
            m_hv->swapSections(0, m_rowcount - 1);
            m_hv->hideSection(m_rowcount / 2);
        }
    }
}

QTEST_MAIN(BenchQHeaderView)
#include "qheaderviewbench.moc"