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
|
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
/**@file
* Compare alternative implementations for BlockingQueue.
*/
#include "qpid/sys/BlockingQueue.h"
#include "qpid/sys/Thread.h"
#include "qpid/sys/Monitor.h"
#include "qpid/sys/Runnable.h"
#include "qpid/sys/Time.h"
#include <boost/test/test_tools.hpp>
#include <boost/bind.hpp>
#include <deque>
#include <vector>
#include <iostream>
#include "time.h"
using namespace qpid::sys;
using namespace std;
template <class T> class DualVectorDualLockQueue {
public:
/** Optionally specify initial capacity of the queue to minimize
* re-allocation.
*/
DualVectorDualLockQueue(size_t capacity=16) {
pushVec.reserve(capacity);
popVec.reserve(capacity);
popIter = popVec.end();
}
/** Push a data item onto the back of the queue */
void push(const T& data) {
Mutex::ScopedLock l(pushLock);
pushVec.push_back(data);
}
/** If the queue is non-empty, pop the front item into data and
* return true. If the queue is empty, return false
*/
bool tryPop(T& data) {
Mutex::ScopedLock l(popLock);
if (popIter == popVec.end()) {
popVec.clear();
Mutex::ScopedLock l(pushLock);
pushVec.swap(popVec);
popIter = popVec.begin();
}
if (popIter == popVec.end())
return false;
else {
data = *popIter++;
return true;
}
}
private:
Mutex pushLock, popLock;
std::vector<T> pushVec, popVec;
typename std::vector<T>::iterator popIter;
};
template <class T> struct LockedDequeQueue : public BlockingQueue<T> {
/** size_t ignored, can't pre-allocate space in a dequeue */
LockedDequeQueue(size_t=0) {};
};
// ================ Test code.
/** Pause by sleeping */
void nsleep(const Duration& delay) {
static Monitor m;
AbsTime stop(now(), delay);
while (now() < stop)
m.wait(stop);
}
/** Pause by spinning */
void nspin(const Duration& delay) {
AbsTime stop(now(), delay);
while (now() < stop)
;
}
/** Unlocked fake queue for comparison */
struct NullQueue {
NullQueue(int items=0) : npush(items), npop(items) {}
void push(int) { --npush; }
bool tryPop(int& n) {
if (npop == 0)
return false;
else {
n=npop--;
return true;
}
}
volatile int npush, npop;
};
// Global test parameters.
int items;
Duration delay(0);
boost::function<void()> npause;
template <class Q>
struct Pusher : public Runnable {
Pusher(Q& q) : queue(q) {}
void run() {
for (int i=items; i > 0; i--) {
queue.push(i);
npause();
}
}
Q& queue;
};
template <class Q>
struct Popper : public Runnable {
Popper(Q& q) : queue(q) {}
void run() {
for (int i=items; i > 0; i--) {
int n;
if (queue.tryPop(n))
BOOST_REQUIRE_EQUAL(i,n);
npause();
}
}
Q& queue;
};
ostream& operator<<(ostream& out, const Duration& d) {
return out << double(d)/TIME_MSEC << " msecs";
}
void report(const char* s, const Duration &d) {
cout << s << ": " << d
<< " (" << (double(items)*TIME_SEC)/d << " push-pops/sec" << ")"
<< endl;
}
template <class Q, class PusherT=Pusher<Q>, class PopperT=Popper<Q> >
struct Timer {
static Duration time() {
cout << endl << "==" << typeid(Q).name() << endl;
Q queue(items);
PusherT pusher(queue);
PopperT popper(queue);
// Serial
AbsTime start=now();
pusher.run();
popper.run();
Duration serial(start,now());
report ("Serial", serial);
// Concurrent
start=now();
Thread pushThread(pusher);
Thread popThread(popper);
pushThread.join();
popThread.join();
Duration concurrent(start,now());
report ("Concurrent", concurrent);
cout << "Serial/concurrent: " << double(serial)/concurrent << endl;
return concurrent;
}
};
int test_main(int argc, char** argv) {
items = (argc > 1) ? atoi(argv[1]) : 250*1000;
delay = (argc > 2) ? atoi(argv[2]) : 4*1000;
npause=boost::bind(nspin, delay);
cout << "Push/pop " << items << " items, delay=" << delay << endl;
Timer<NullQueue>::time();
Duration dv = Timer<DualVectorDualLockQueue<int> >::time();
Duration d = Timer<LockedDequeQueue<int> >::time();
cout << endl;
cout << "Ratio deque/dual vector=" << double(d)/dv << endl;
return 0;
}
// namespace
|