-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboost_test.cpp
401 lines (307 loc) · 12.1 KB
/
boost_test.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <iostream>
#include <system_error>
#include <ctime>
#include <sys/select.h>
#include <boost/asio.hpp>
#include <boost/outcome.hpp>
#include <boost/url.hpp>
#include <boost/heap/priority_queue.hpp>
#include <boost/pool/pool.hpp>
#include <boost/pool/object_pool.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <google/protobuf/util/time_util.h>
#include <boost/unordered_map.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/lockfree/queue.hpp>
#include <boost/chrono.hpp>
#include <boost/thread.hpp>
#include <boost/random.hpp>
#include <boost/pool/object_pool.hpp>
#include "e2sarHeaders.hpp"
using namespace boost::asio;
namespace outcome = BOOST_OUTCOME_V2_NAMESPACE;
using namespace std::string_literals;
void funcRef(const std::vector<int> &v)
{
std::cout << "Ref" << std::endl;
for(auto a: v)
std::cout << a << " ";
std::cout << std::endl;
}
void funcMv(std::vector<int> &&v)
{
std::cout << "Move" << std::endl;
for(auto a: v)
std::cout << a << " ";
std::cout << std::endl;
}
// generated by chatGPT
struct pair_hash {
std::size_t operator()(const std::pair<u_int64_t, u_int16_t>& p) const {
u_int64_t hash1 = p.first;
u_int64_t tmp = p.second;
u_int64_t hash2 = tmp | tmp << 16 | tmp << 32 | tmp << 48;
return hash1 ^ hash2; // Combine the two hashes
}
};
struct pair_equal {
bool operator()(const std::pair<u_int64_t, u_int16_t>& lhs, const std::pair<u_int64_t, u_int16_t>& rhs) const {
return lhs.first == rhs.first && lhs.second == rhs.second;
}
};
int main()
{
// Test boost IP addresses
ip::address ipv4 = ip::make_address("192.168.1.1");
ip::address ipv6 = ip::make_address("2001:db8:0000:1:1:1:1:1");
try
{
ip::make_address("blaaaa");
}
catch (boost::system::system_error &e)
{
std::cout << "Unable to convert string to address" << std::endl;
}
std::cout << "IPv4 " << ipv4 << ' ' << ipv4.is_v4() << '\n';
std::cout << "IPv6 " << ipv6 << ' ' << ipv6.is_v6() << '\n';
// Test name resolution
boost::asio::io_service io_service;
ip::udp::resolver resolver(io_service);
ip::udp::resolver::query query("www.renci.org", "445");
ip::udp::resolver::iterator iter = resolver.resolve(query);
ip::udp::resolver::iterator end; // End marker.
while (iter != end)
{
ip::udp::endpoint endpoint = *iter++;
ip::address address = endpoint.address();
std::cout << address << ' ' << address.is_v4() << ' ' << std::endl;
}
// Test URL parsing
std::string uri_string{"ejfat://[email protected]:18020/lb/36?sync=192.188.29.6:19020&data=192.188.29.20"};
boost::system::result<boost::url_view> r = boost::urls::parse_uri(uri_string);
if (!r)
{
if (r.error())
std::cout << "Unable to convert! "s << r.error() << std::endl;
}
else
{
boost::url_view u = r.value();
std::cout << u.scheme() << std::endl;
std::cout << u.userinfo() << " " << u.userinfo().length() << std::endl;
std::cout << u.host() << std::endl;
std::cout << u.port() << std::endl;
std::cout << u.path() << std::endl;
std::cout << u.query() << std::endl;
for (auto param : u.params())
std::cout << param.key << ": " << param.value << std::endl;
std::vector<std::string> lb_path;
boost::split(lb_path, u.path(), boost::is_any_of("/"));
for (auto g : lb_path)
{
std::cout << ": " << g << std::endl;
}
}
// Timestamp conversions
// from std::time_t
std::time_t now = std::time(nullptr);
google::protobuf::Timestamp ts = google::protobuf::util::TimeUtil::TimeTToTimestamp(now);
// from boost::ptime with time difference
boost::posix_time::ptime pt = boost::posix_time::second_clock::local_time();
boost::posix_time::time_duration td = boost::posix_time::hours(24);
boost::posix_time::ptime pt1 = pt + td;
std::cout << "Now + 1 day is " << pt1 << std::endl;
google::protobuf::Timestamp ts1 = google::protobuf::util::TimeUtil::TimeTToTimestamp(to_time_t(pt1));
std::cout << ts1 << std::endl;
// pool tests
// object pool for queue items
struct qItem {
char preamble[2] {'L', 'B'};
uint32_t bytes;
uint64_t tick;
char *event;
void *cbArg;
void* (*callback)(void *);
qItem(): bytes{100}, tick{1000} {};
};
std::cout << "Item size " << sizeof(qItem) << std::endl;
boost::object_pool<qItem> pool{sizeof(qItem) * 10, 0};
std::cout << pool.get_next_size() << '\n';
auto newItem = pool.construct();
std::cout << pool.get_next_size() << '\n';
newItem->tick = 1001;
std::cout << newItem->tick << " " << newItem->bytes << std::endl;
std::cout << newItem->preamble[0] << newItem->preamble[1] << std::endl;
pool.free(newItem);
std::cout << (1 << 4) << std::endl;
std::cout << "Sync header size (expecting 28) = " << sizeof(e2sar::SyncHdr) << std::endl;
{
std::cout << " Empty scope executes once" << std::endl;
}
std::cout << " LB Hdr size (expecting 16) = " << sizeof(e2sar::LBHdr) << std::endl;
std::cout << " RE Hdr size (expecting 20) = " << sizeof(e2sar::REHdr) << std::endl;
std::cout << " LB+RE Hdr size (expecting 36) = " << sizeof(e2sar::LBREHdr) << std::endl;
// test allocating N ports to M threads
std::vector<int> ports{1,2,3,4};
std::cout << "Assignable ports: " << std::endl;
std::cout << " ";
for(auto i: ports)
std::cout << i << " ";
std::cout << std::endl;
size_t threads{3};
std::vector<std::list<int>> ptt(threads);
for(size_t i=0;i<ports.size();)
{
for(size_t j=0;j<threads && i<ports.size();i++,j++)
{
ptt[j].push_back(ports[i]);
}
}
std::cout << "Assigned ports to threads: " << std::endl;
int t{0};
for(auto l: ptt)
{
std::cout << " Thread " << t++ << ": " << std::endl;
for(auto e: l)
std::cout << e << " ";
std::cout << std::endl;
}
std::list<std::unique_ptr<int>> recvThreadState(5);
std::cout << "Testing list: allocated size is (5) " << recvThreadState.size() << std::endl;
// testing dealing with portRange
std::cout << "Testing portRange:" << std::endl;
int portRange{3};
size_t numPorts{static_cast<size_t>(2 << (portRange - 1))};
u_int16_t startPort{1850};
size_t numThreads{3};
std::cout << " Allocating " << numPorts << " ports from portRange " << portRange << " to " << numThreads << " threads" << std::endl;
for(size_t i=0; i<numPorts;)
{
for(size_t j=0; i<numPorts && j<numThreads; i++, j++)
{
std::cout << " Assigning port " << startPort + i << " to thread " << j << std::endl;
}
}
// testing passing vectors
std::cout << "Passing vectors " << std::endl;
std::vector<int> va{1,2,3};
std::vector<int> vb{2,3,4};
funcRef(va);
for(auto a: va)
std::cout << " " << a << " ";
std::cout << std::endl;
funcMv(std::move(vb));
for(auto a: vb)
std::cout << " " << a << " ";
std::cout << std::endl;
// testing fd_set copy constructor
std::cout << "Testing fdset" << std::endl;
fd_set fdSet{};
FD_SET(0, &fdSet);
FD_SET(2, &fdSet);
fd_set newSet{fdSet};
std::cout << " old set " << FD_ISSET(0, &fdSet) << " " << FD_ISSET(1, &fdSet) << " " << FD_ISSET(2, &fdSet) << std::endl;
std::cout << " new set " << FD_ISSET(0, &newSet) << " " << FD_ISSET(1, &newSet) << " " << FD_ISSET(2, &newSet) << std::endl;
// test priority queue for custom events
std::cout << "Priority queue" << std::endl;
struct Event {
int len;
int offset;
};
struct EventComparator {
bool operator() (const Event &l, const Event &r) const {
return l.offset > r.offset;
}
};
Event e1{5, 0};
Event e2{3, 5};
Event e3{2, 8};
boost::heap::priority_queue<Event, boost::heap::compare<EventComparator>> pq;
pq.push(e2);
pq.push(e3);
pq.push(e1);
for(; !pq.empty(); pq.pop())
std::cout << " Event len " << pq.top().len << " offset " << pq.top().offset << std::endl;
// test boost pool array allocation
std::cout << "Test pool allocation" << std::endl;
boost::pool<> bufpool(20);
auto ar1 = static_cast<char*>(bufpool.malloc());
auto ar2 = static_cast<char*>(bufpool.malloc());
std::strcpy(ar1, "Hello world!");
std::strcpy(ar2, "I have come to help!");
std::cout << " Ar1 " << ar1 << std::endl;
std::cout << " Ar2 " << ar2 << std::endl;
bufpool.free(ar1);
bufpool.free(ar2);
std::cout << "Test array of lists" << std::endl;
std::vector<std::list<int>> portsToThreads;
portsToThreads.emplace(portsToThreads.end());
portsToThreads[0].push_back(1);
std::cout << "Test unordered map with pairs as keys" << std::endl;
boost::unordered_map<std::pair<u_int64_t, u_int16_t>, std::string, pair_hash, pair_equal> map;
map[std::make_pair(0x123456, 1)] = "First event"s;
map[std::make_pair(0x123456, 2)] = "Second event"s;
map[std::make_pair(0x1234567, 10)] = "Third event"s;
std::cout << map[std::make_pair(0x123456, 1)] << std::endl;
std::cout << map[std::make_pair(0x123456, 2)] << std::endl;
std::cout << map[std::make_pair(0x1234567, 10)] << std::endl;
std::cout << "Test circular buffer" << std::endl;
boost::circular_buffer<int> pidSampleBuffer(5);
for (int i = 0; i<6; i++)
pidSampleBuffer.push_back(i);
std::cout << "Head of buffer " << pidSampleBuffer.front() << std::endl;
std::cout << "Tail of buffer " << pidSampleBuffer.back() << std::endl;
for (int i = 10; i<20; i++)
pidSampleBuffer.push_back(i);
std::cout << "Head of buffer " << pidSampleBuffer.front() << std::endl;
std::cout << "Tail of buffer " << pidSampleBuffer.back() << std::endl;
std::cout << "Test allocate deallocate" << std::endl;
boost::lockfree::queue<std::pair<int, u_int16_t>*> lostEventsQueue{20};
for(int i=0; i<5; i++)
{
lostEventsQueue.push(new std::pair<int, u_int16_t>(i, i*10));
}
std::pair<int, u_int16_t> *res;
while(lostEventsQueue.pop(res))
{
auto ret = *res;
std::cout << "Retrieved " << ret.first << ":" << ret.second << std::endl;
delete res;
}
std::cout << "Clock tests" << std::endl;
// we want to know if system clock usecs produces enough randomness in the bottom 9
// bits and if not add randomness in them.
std::vector<int_least64_t> points;
int total{10000};
std::vector<int> bins(256, 0);
for (int i = 0; i < total; i++)
{
auto now1 = boost::chrono::system_clock::now();
auto now1Usec = boost::chrono::duration_cast<boost::chrono::microseconds>(now1.time_since_epoch()).count();
bins[now1Usec & 0xff]++;
auto until = now1 + boost::chrono::milliseconds(1);
boost::this_thread::sleep_until(until);
}
// compute the probabilities and entropy
float prob{0.0}, probsum{0.0};
float entropy{0.0};
for (size_t i = 0; i < bins.size(); i++)
{
prob = static_cast<float>(bins[i])/(total*1.0);
entropy += prob * std::log(prob);
probsum += prob;
}
entropy *= -1.0/std::log(2);
std::cout << "Probability sum " << probsum << std::endl;
std::cout << "Entropy is " << entropy << " bits" << std::endl;
// add entropy to a clock sample
boost::random::ranlux24_base ranlux;
boost::random::uniform_int_distribution<> randDist{0, 255};
auto now1 = boost::chrono::system_clock::now();
auto now1Usec = boost::chrono::duration_cast<boost::chrono::microseconds>(now1.time_since_epoch()).count();
// replace lower 8 bits
auto addedEntropy = (now1Usec & ~0xFF) | randDist(ranlux);
std::cout << "Original timestamp " << now1Usec << " with added entropy " << addedEntropy << std::endl;
std::cout << "Original & 0xFF " << (now1Usec & 0xFF) << " with added entropy & 0xFF " << (addedEntropy & 0xFF) << std::endl;
}