-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathtest_queueing.cpp
329 lines (254 loc) · 8.63 KB
/
test_queueing.cpp
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
/* Copyright 2013-present Barefoot Networks, Inc.
*
* Licensed 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.
*/
/*
* Antonin Bas ([email protected])
*
*/
#include <gtest/gtest.h>
#include <bm/bm_sim/queueing.h>
#include <thread>
#include <memory>
#include <array>
#include <vector>
#include <algorithm> // for std::count, std::max
using std::unique_ptr;
using std::thread;
using bm::QueueingLogic;
using bm::QueueingLogicRL;
using bm::QueueingLogicPriRL;
struct WorkerMapper {
WorkerMapper(size_t nb_workers)
: nb_workers(nb_workers) { }
size_t operator()(size_t queue_id) const {
return queue_id % nb_workers;
}
size_t nb_workers;
};
struct RndInput {
size_t queue_id;
int v;
};
template <class QType>
class QueueingTest : public ::testing::Test {
protected:
static constexpr size_t nb_queues = 3u;
static constexpr size_t nb_workers = 2u;
static constexpr size_t capacity = 128u;
static constexpr size_t iterations = 200000;
// QueueingLogic<T, WorkerMapper> queue;
QType queue;
std::vector<RndInput> values;
QueueingTest()
: queue(nb_workers, capacity, WorkerMapper(nb_workers)),
values(iterations) { }
virtual void SetUp() {
for (size_t i = 0; i < iterations; i++) {
values[i] = {rand() % nb_queues, rand()};
}
}
public:
void produce();
// virtual void TearDown() {}
};
using QEm = std::unique_ptr<int>;
template <typename QType>
void QueueingTest<QType>::produce() {
for (size_t i = 0; i < iterations; i++) {
queue.push_front(values[i].queue_id,
unique_ptr<int>(new int(values[i].v)));
}
}
namespace {
template <typename Q>
void produce_if_dropping(Q &queue, size_t iterations,
const std::vector<RndInput> &values) {
for (size_t i = 0; i < iterations; i++) {
size_t queue_id = values[i].queue_id;
// this is to avoid drops
// kind of makes me question if using type parameterization is really useful
// for this
while (!queue.push_front(queue_id, unique_ptr<int>(new int(values[i].v)))) {
// originally, I just had an empty loop, but Valgrind was running forever
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
}
} // namespace
template <>
void QueueingTest<QueueingLogicRL<QEm, WorkerMapper> >::produce() {
produce_if_dropping(queue, iterations, values);
}
template <>
void QueueingTest<QueueingLogicPriRL<QEm, WorkerMapper> >::produce() {
produce_if_dropping(queue, iterations, values);
}
using testing::Types;
using QueueingTypes = Types<QueueingLogic<QEm, WorkerMapper>,
QueueingLogicRL<QEm, WorkerMapper>,
QueueingLogicPriRL<QEm, WorkerMapper> >;
TYPED_TEST_SUITE(QueueingTest, QueueingTypes);
TYPED_TEST(QueueingTest, ProducerConsummer) {
thread producer_thread(&QueueingTest<TypeParam>::produce, this);
WorkerMapper mapper(this->nb_workers);
for (size_t i = 0; i < this->iterations; i++) {
size_t queue_id;
size_t expected_queue_id = this->values[i].queue_id;
unique_ptr<int> v;
size_t worker_id = mapper(expected_queue_id);
this->queue.pop_back(worker_id, &queue_id, &v);
ASSERT_EQ(expected_queue_id, queue_id);
ASSERT_EQ(this->values[i].v, *v);
}
producer_thread.join();
}
class QueueingRLTest : public ::testing::Test {
protected:
using T = std::unique_ptr<int>;
static constexpr size_t nb_queues = 1u;
static constexpr size_t nb_workers = 1u;
static constexpr size_t capacity = 1024u;
static constexpr size_t iterations = 512u;
static constexpr uint64_t pps = 100u;
QueueingLogicRL<T, WorkerMapper> queue;
std::vector<RndInput> values;
static_assert(capacity >= iterations,
"for RL test, capacity needs to be greater or equal to # pkts");
QueueingRLTest()
: queue(nb_workers, capacity, WorkerMapper(nb_workers)),
values(iterations) { }
virtual void SetUp() {
for (size_t i = 0; i < iterations; i++) {
values[i] = {rand() % nb_queues, rand()};
}
queue.set_rate(0u, pps);
}
public:
void produce() {
for (size_t i = 0; i < iterations; i++) {
queue.push_front(values[i].queue_id,
unique_ptr<int>(new int(values[i].v)));
}
}
// virtual void TearDown() {}
};
TEST_F(QueueingRLTest, RateLimiter) {
thread producer_thread(&QueueingRLTest::produce, this);
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using clock = std::chrono::high_resolution_clock;
std::vector<int> times;
times.reserve(iterations);
auto start = clock::now();
for (size_t i = 0; i < iterations; i++) {
size_t queue_id;
unique_ptr<int> v;
this->queue.pop_back(0u, &queue_id, &v);
ASSERT_EQ(0u, queue_id);
ASSERT_EQ(values[i].v, *v);
auto now = clock::now();
times.push_back(duration_cast<milliseconds>(now - start).count());
}
producer_thread.join();
int elapsed = times.back();
int expected = (iterations * 1000) / pps;
ASSERT_GT(elapsed, expected * 0.9);
ASSERT_LT(elapsed, expected * 1.1);
// TODO(antonin): better check of times vector?
}
struct RndInputPri {
size_t queue_id;
int v;
size_t priority;
};
#ifndef SKIP_UNDETERMINISTIC_TESTS
class QueueingPriRLTest : public ::testing::Test {
protected:
using T = std::unique_ptr<int>;
static constexpr size_t nb_queues = 1u;
static constexpr size_t nb_workers = 1u;
static constexpr size_t nb_priorities = 2u;
static constexpr size_t capacity = 200u;
static constexpr size_t iterations = 1000u;
static constexpr uint64_t consummer_pps = 50u;
static constexpr uint64_t producer_pps = 100u;
QueueingLogicPriRL<T, WorkerMapper> queue;
std::vector<RndInputPri> values;
QueueingPriRLTest()
: queue(nb_workers, capacity, WorkerMapper(nb_workers),
nb_priorities),
values(iterations) { }
virtual void SetUp() {
for (size_t i = 0; i < iterations; i++) {
values[i] = {rand() % nb_queues, rand(), rand() % nb_priorities};
}
}
std::vector<size_t> receive_all() {
using std::chrono::duration;
std::vector<size_t> priorities;
priorities.reserve(iterations);
while (priorities.size() < capacity || queue.size(0u) > 0) {
size_t queue_id, priority;
unique_ptr<int> v;
this->queue.pop_back(0u, &queue_id, &priority, &v);
std::this_thread::sleep_for(duration<double>(1. / consummer_pps));
priorities.push_back(priority);
}
return priorities;
}
public:
void produce() {
using std::chrono::duration;
for (size_t i = 0; i < iterations; i++) {
queue.push_front(values[i].queue_id, values[i].priority,
unique_ptr<int>(new int(values[i].v)));
std::this_thread::sleep_for(duration<double>(1. / producer_pps));
}
}
// virtual void TearDown() {}
};
TEST_F(QueueingPriRLTest, Pri) {
thread producer_thread(&QueueingPriRLTest::produce, this);
auto priorities = receive_all();
producer_thread.join();
size_t priority_0 = std::count(priorities.begin(), priorities.end(), 0);
size_t priority_1 = priorities.size() - priority_0;
// if removed, g++ complains that capacity was not defined
const size_t c = capacity;
// we have to receive at least capacity P0 elements (they are buffered and
// dequeued at the end...)
ASSERT_LT(c, priority_0);
// most elements should be P1
// was originally 10%, but replaced it with 20% as the test would fail from
// time to time
ASSERT_GT(0.2, (priority_0 - c) / static_cast<double>(priority_1));
}
TEST_F(QueueingPriRLTest, PriRateLimiter) {
static constexpr size_t rate_pps = consummer_pps / 2u;
queue.set_rate(0u, rate_pps);
thread producer_thread(&QueueingPriRLTest::produce, this);
auto priorities = receive_all();
producer_thread.join();
size_t priority_0 = std::count(priorities.begin(), priorities.end(), 0);
size_t priority_1 = priorities.size() - priority_0;
size_t diff;
if (priority_0 < priority_1)
diff = priority_1 - priority_0;
else
diff = priority_0 - priority_1;
// was originally 10%, but replaced it with 20% as the test would fail from
// time to time (on slower machines?)
ASSERT_LT(diff, std::max(priority_0, priority_1) * 0.2);
}
#endif // SKIP_UNDETERMINISTIC_TESTS