summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/corelib/thread/qfuture/tst_bench_qfuture.cpp
blob: 38b3f636c97e563b013833a54a729d423f8e1fff (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QTest>

#include <qexception.h>
#include <qfuture.h>
#include <qpromise.h>
#include <qsemaphore.h>

class tst_QFuture : public QObject
{
    Q_OBJECT

private slots:
    void makeReadyValueFuture();
#ifndef QT_NO_EXCEPTIONS
    void makeExceptionalFuture();
#endif
    void result();
    void results();
    void takeResult();
    void reportResult();
    void reportResults();
    void reportResultsManualProgress();
#ifndef QT_NO_EXCEPTIONS
    void reportException();
#endif
    void then();
    void thenVoid();
    void onCanceled();
    void onCanceledVoid();
#ifndef QT_NO_EXCEPTIONS
    void onFailed();
    void onFailedVoid();
    void thenOnFailed();
    void thenOnFailedVoid();
#endif
    void reportProgress();
    void progressMinimum();
    void progressMaximum();
    void progressValue();
    void progressText();
};

void tst_QFuture::makeReadyValueFuture()
{
    QBENCHMARK {
        auto future = QtFuture::makeReadyValueFuture(42);
        Q_UNUSED(future);
    }
}

#ifndef QT_NO_EXCEPTIONS
void tst_QFuture::makeExceptionalFuture()
{
    QException e;
    QBENCHMARK {
        auto future = QtFuture::makeExceptionalFuture(e);
        Q_UNUSED(future);
    }
}
#endif

void tst_QFuture::result()
{
    auto future = QtFuture::makeReadyValueFuture(42);

    QBENCHMARK {
        auto value = future.result();
        Q_UNUSED(value);
    }
}

void tst_QFuture::results()
{
    QFutureInterface<int> fi;
    fi.reportStarted();

    for (int i = 0; i < 1000; ++i)
        fi.reportResult(i);

    fi.reportFinished();

    auto future = fi.future();
    QBENCHMARK {
        auto values = future.results();
        Q_UNUSED(values);
    }
}

void tst_QFuture::takeResult()
{
    QBENCHMARK {
        auto future = QtFuture::makeReadyValueFuture(42);
        auto value = future.takeResult();
        Q_UNUSED(value);
    }
}

void tst_QFuture::reportResult()
{
    QFutureInterface<int> fi;
    QBENCHMARK {
        fi.reportResult(42);
    }
}

void tst_QFuture::reportResults()
{
    QFutureInterface<int> fi;
    QList<int> values(1000);
    std::iota(values.begin(), values.end(), 0);
    QBENCHMARK {
        fi.reportResults(values);
    }
}

void tst_QFuture::reportResultsManualProgress()
{
    QFutureInterface<int> fi;
    const int resultCount = 1000;
    fi.setProgressRange(0, resultCount);
    QBENCHMARK {
        for (int i = 0; i < resultCount; ++i)
            fi.reportResult(i);
    }
}

#ifndef QT_NO_EXCEPTIONS
void tst_QFuture::reportException()
{
    QException e;
    QBENCHMARK {
        QFutureInterface<int> fi;
        fi.reportException(e);
    }
}
#endif

void tst_QFuture::then()
{
    auto f = QtFuture::makeReadyValueFuture(42);
    QBENCHMARK {
        auto future = f.then([](int value) { return value; });
        Q_UNUSED(future);
    }
}

void tst_QFuture::thenVoid()
{
    auto f = QtFuture::makeReadyVoidFuture();
    QBENCHMARK {
        auto future = f.then([] {});
        Q_UNUSED(future);
    }
}

void tst_QFuture::onCanceled()
{
    QFutureInterface<int> fi;
    fi.reportStarted();
    fi.reportCanceled();
    fi.reportFinished();

    QBENCHMARK {
        auto future = fi.future().onCanceled([] { return 0; });
        Q_UNUSED(future);
    }
}

void tst_QFuture::onCanceledVoid()
{
    QFutureInterface<void> fi;
    fi.reportStarted();
    fi.reportCanceled();
    fi.reportFinished();

    QBENCHMARK {
        auto future = fi.future().onCanceled([] {});
        Q_UNUSED(future);
    }
}

#ifndef QT_NO_EXCEPTIONS
void tst_QFuture::onFailed()
{
    QException e;
    auto f = QtFuture::makeExceptionalFuture<int>(e);
    QBENCHMARK {
        auto future = f.onFailed([] { return 0; });
        Q_UNUSED(future);
    }
}

void tst_QFuture::onFailedVoid()
{
    QException e;
    auto f = QtFuture::makeExceptionalFuture(e);
    QBENCHMARK {
        auto future = f.onFailed([] {});
        Q_UNUSED(future);
    }
}

void tst_QFuture::thenOnFailed()
{
    auto f = QtFuture::makeReadyValueFuture(42);
    QBENCHMARK {
        auto future =
                f.then([](int) { throw std::runtime_error("error"); }).onFailed([] { return 0; });
        Q_UNUSED(future);
    }
}

void tst_QFuture::thenOnFailedVoid()
{
    auto f = QtFuture::makeReadyVoidFuture();
    QBENCHMARK {
        auto future = f.then([] { throw std::runtime_error("error"); }).onFailed([] {});
        Q_UNUSED(future);
    }
}

#endif

void tst_QFuture::reportProgress()
{
    QFutureInterface<void> fi;
    fi.reportStarted();
    fi.reportFinished();
    QBENCHMARK {
        for (int i = 0; i < 100; ++i) {
            fi.setProgressValue(i);
        }
    }
}

void tst_QFuture::progressMinimum()
{
    QFutureInterface<void> fi;
    fi.setProgressRange(0, 100);
    fi.reportStarted();
    fi.reportFinished();
    auto future = fi.future();

    QBENCHMARK {
        auto value = future.progressMinimum();
        Q_UNUSED(value);
    }
}

void tst_QFuture::progressMaximum()
{
    QFutureInterface<void> fi;
    fi.setProgressRange(0, 100);
    fi.reportStarted();
    fi.reportFinished();
    auto future = fi.future();

    QBENCHMARK {
        auto value = future.progressMaximum();
        Q_UNUSED(value);
    }
}

void tst_QFuture::progressValue()
{
    QFutureInterface<void> fi;
    fi.setProgressValue(50);
    fi.reportStarted();
    fi.reportFinished();
    auto future = fi.future();

    QBENCHMARK {
        auto value = future.progressValue();
        Q_UNUSED(value);
    }
}

void tst_QFuture::progressText()
{
    QFutureInterface<void> fi;
    fi.setProgressValueAndText(50, "text");
    fi.reportStarted();
    fi.reportFinished();
    auto future = fi.future();

    QBENCHMARK {
        auto text = future.progressText();
        Q_UNUSED(text);
    }
}

QTEST_MAIN(tst_QFuture)

#include "tst_bench_qfuture.moc"