-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestPerformance.cc
More file actions
337 lines (250 loc) · 11 KB
/
testPerformance.cc
File metadata and controls
337 lines (250 loc) · 11 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
// SPDX-License-Identifier: LGPL-3.0-or-later
#define BOOST_TEST_MODULE test_future_queue
#include <boost/test/included/unit_test.hpp>
using namespace boost::unit_test_framework;
#include "barrier.hpp"
#include "future_queue.hpp"
#include <boost/lockfree/queue.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/next_prior.hpp>
#include <boost/thread/future.hpp>
#include <iterator>
#include <thread>
constexpr size_t queueLength = 1000;
constexpr size_t nTransfers = 1e6;
constexpr size_t nQueues = 10; // only for when_any & related
// #define ENABLE_BOOST_LOCKFREE_QUEUE_PERFORMANCE_MEASUREMENT // just for
// comparison
/**********************************************************************************************************************/
class helper_iterator {
public:
helper_iterator(std::list<std::unique_ptr<boost::lockfree::spsc_queue<boost::shared_future<int32_t>>>>::iterator _it)
: it(_it) {}
helper_iterator operator++(int) {
++it;
return *this;
}
helper_iterator operator++() {
auto rval = *this;
++it;
return rval;
}
boost::shared_future<int32_t>& operator*() {
std::unique_ptr<boost::lockfree::spsc_queue<boost::shared_future<int32_t>>>& q = *it;
return q->front();
}
bool operator!=(const helper_iterator& other) const { return it != other.it; }
bool operator==(const helper_iterator& other) const { return it == other.it; }
std::unique_ptr<boost::lockfree::spsc_queue<boost::shared_future<int32_t>>>& get_queue() { return *it; }
private:
std::list<std::unique_ptr<boost::lockfree::spsc_queue<boost::shared_future<int32_t>>>>::iterator it;
};
namespace std {
template<>
struct iterator_traits<helper_iterator> {
typedef boost::shared_future<int32_t> value_type;
typedef size_t difference_type;
typedef std::forward_iterator_tag iterator_category;
};
} // namespace std
/**********************************************************************************************************************/
BOOST_AUTO_TEST_SUITE(testPerformance)
/**********************************************************************************************************************/
BOOST_AUTO_TEST_CASE(future_queue_spin_wait) {
std::cout << "Measure performance of future_queue with spin-waiting" << std::endl;
cppext::future_queue<int32_t> theQueue(queueLength);
auto start = std::chrono::steady_clock::now();
std::thread sender([&theQueue] {
for(size_t i = 0; i < nTransfers; ++i) {
while(theQueue.push(i & 0xFFFF) == false) usleep(1);
}
}); // end thread sender
for(size_t i = 0; i < nTransfers; ++i) {
int32_t val;
while(theQueue.pop(val) == false) continue;
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Time for " << nTransfers << " transfers: " << diff.count() << " s\n";
std::cout << "Average time per transfer: " << diff.count() / (double)nTransfers * 1e6 << " us\n";
sender.join();
}
/**********************************************************************************************************************/
#ifdef ENABLE_BOOST_LOCKFREE_QUEUE_PERFORMANCE_MEASUREMENT
BOOST_AUTO_TEST_CASE(boost_queue_spin_wait) {
std::cout << "Measure performance of boost::lockfree::queue" << std::endl;
boost::lockfree::queue<int32_t> theQueue(queueLength);
auto start = std::chrono::steady_clock::now();
std::thread sender([&theQueue] {
for(size_t i = 0; i < nTransfers; ++i) {
while(theQueue.push(i & 0xFFFF) == false) usleep(1);
}
}); // end thread sender
for(size_t i = 0; i < nTransfers; ++i) {
int32_t val;
while(theQueue.pop(val) == false) continue;
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Time for " << nTransfers << " transfers: " << diff.count() << " s\n";
std::cout << "Average time per transfer: " << diff.count() / (double)nTransfers * 1e6 << " us\n";
sender.join();
}
#endif
/**********************************************************************************************************************/
#ifdef ENABLE_BOOST_LOCKFREE_QUEUE_PERFORMANCE_MEASUREMENT
BOOST_AUTO_TEST_CASE(boost_spsc_queue_spin_wait) {
std::cout << "Measure performance of boost::lockfree::spsc_queue" << std::endl;
boost::lockfree::spsc_queue<int32_t> theQueue(queueLength);
auto start = std::chrono::steady_clock::now();
std::thread sender([&theQueue] {
for(size_t i = 0; i < nTransfers; ++i) {
while(theQueue.push(i & 0xFFFF) == false) usleep(1);
}
}); // end thread sender
for(size_t i = 0; i < nTransfers; ++i) {
int32_t val;
while(theQueue.pop(val) == false) continue;
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Time for " << nTransfers << " transfers: " << diff.count() << " s\n";
std::cout << "Average time per transfer: " << diff.count() / (double)nTransfers * 1e6 << " us\n";
sender.join();
}
#endif
/**********************************************************************************************************************/
BOOST_AUTO_TEST_CASE(future_queue_pop_wait) {
std::cout << "Measure performance of future_queue with pop_wait" << std::endl;
cppext::future_queue<int32_t> theQueue(queueLength);
auto start = std::chrono::steady_clock::now();
std::thread sender([&theQueue] {
for(size_t i = 0; i < nTransfers; ++i) {
while(theQueue.push(i & 0xFFFF) == false) usleep(1);
}
}); // end thread sender
for(size_t i = 0; i < nTransfers; ++i) {
int32_t val;
theQueue.pop_wait(val);
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Time for " << nTransfers << " transfers: " << diff.count() << " s\n";
std::cout << "Average time per transfer: " << diff.count() / (double)nTransfers * 1e6 << " us\n";
sender.join();
}
/**********************************************************************************************************************/
#ifdef ENABLE_BOOST_LOCKFREE_QUEUE_PERFORMANCE_MEASUREMENT
BOOST_AUTO_TEST_CASE(boost_spsc_queue_of_futures) {
std::cout << "Measure performance of "
"boost::lockfree::spsc_queue<std::shared_future<T>>"
<< std::endl;
boost::lockfree::spsc_queue<boost::shared_future<int32_t>> theQueue(queueLength);
boost::promise<int32_t> thePromise;
theQueue.push(thePromise.get_future().share());
auto start = std::chrono::steady_clock::now();
std::thread sender([&theQueue, &thePromise] {
for(size_t i = 0; i < nTransfers; ++i) {
boost::promise<int32_t> newPromise;
auto newFuture = newPromise.get_future().share();
while(theQueue.push(newFuture) == false) usleep(1);
thePromise.set_value(i & 0xFFFF);
thePromise = std::move(newPromise);
}
}); // end thread sender
for(size_t i = 0; i < nTransfers; ++i) {
boost::shared_future<int32_t> theFuture;
theQueue.pop(theFuture);
theFuture.get();
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Time for " << nTransfers << " transfers: " << diff.count() << " s\n";
std::cout << "Average time per transfer: " << diff.count() / (double)nTransfers * 1e6 << " us\n";
sender.join();
}
#endif
/**********************************************************************************************************************/
BOOST_AUTO_TEST_CASE(future_queue_when_any) {
std::cout << "Measure performance of future_queue with when_any" << std::endl;
static_assert(nTransfers % nQueues == 0, "nQueues must be an integer divider of nTransfers.");
std::vector<cppext::future_queue<int32_t>> vectorOfQueues;
for(size_t i = 0; i < nQueues; ++i) vectorOfQueues.emplace_back(queueLength);
auto notificationQueue = when_any(vectorOfQueues.begin(), vectorOfQueues.end());
cppext::barrier b1(nQueues + 1), b2(nQueues + 1);
std::vector<std::thread> senders;
for(auto& q : vectorOfQueues) {
senders.emplace_back([&q, &b1, &b2] {
b1.wait();
b2.wait();
for(size_t i = 0; i < nTransfers / nQueues; ++i) {
while(q.push(i & 0xFFFF) == false) usleep(1);
}
}); // end thread sender
}
b1.wait();
auto start = std::chrono::steady_clock::now();
b2.wait();
for(size_t i = 0; i < nTransfers; ++i) {
size_t id;
notificationQueue.pop_wait(id);
int32_t val;
vectorOfQueues[id].pop(val);
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Time for " << nTransfers << " transfers: " << diff.count() << " s\n";
std::cout << "Average time per transfer: " << diff.count() / (double)nTransfers * 1e6 << " us\n";
for(auto& t : senders) t.join();
}
/**********************************************************************************************************************/
#ifdef ENABLE_BOOST_LOCKFREE_QUEUE_PERFORMANCE_MEASUREMENT
BOOST_AUTO_TEST_CASE(boost_spsc_queue_wait_for_any) {
std::cout << "Measure performance of "
"boost::lockfree::spsc_queue<boost::shared_future<T>> with "
"wait_for_any"
<< std::endl;
static_assert(nTransfers % nQueues == 0, "nQueues must be an integer divider of nTransfers.");
std::list<std::unique_ptr<boost::lockfree::spsc_queue<boost::shared_future<int32_t>>>> listOfQueues;
for(size_t i = 0; i < nQueues; ++i) {
listOfQueues.emplace_back(new boost::lockfree::spsc_queue<boost::shared_future<int32_t>>(queueLength));
}
cppext::barrier b1(nQueues + 1), b2(nQueues + 1), b3(nQueues + 1);
std::vector<std::thread> senders;
for(auto& q : listOfQueues) {
senders.emplace_back([&q, &b1, &b2, &b3] {
boost::promise<int32_t> thePromise;
q->push(thePromise.get_future().share());
b1.wait();
b2.wait();
for(size_t i = 0; i < nTransfers / nQueues; ++i) {
boost::promise<int32_t> newPromise;
auto newFuture = newPromise.get_future().share();
while(q->push(newFuture) == false) usleep(1);
thePromise.set_value(i & 0xFFFF);
thePromise = std::move(newPromise);
}
b3.wait();
}); // end thread sender
}
b1.wait();
auto start = std::chrono::steady_clock::now();
b2.wait();
for(size_t i = 0; i < nTransfers; ++i) {
auto ret = boost::wait_for_any(helper_iterator(listOfQueues.begin()), helper_iterator(listOfQueues.end()));
auto& theQueue = ret.get_queue();
boost::shared_future<int32_t> theFuture;
theQueue->pop(theFuture);
theFuture.get();
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Time for " << nTransfers << " transfers: " << diff.count() << " s\n";
std::cout << "Average time per transfer: " << diff.count() / (double)nTransfers * 1e6 << " us\n";
b3.wait();
for(auto& t : senders) t.join();
}
#endif
/**********************************************************************************************************************/
BOOST_AUTO_TEST_SUITE_END()