-
Notifications
You must be signed in to change notification settings - Fork 557
/
traffic_structures_performance_tests_real_traffic.cpp
343 lines (248 loc) · 11 KB
/
traffic_structures_performance_tests_real_traffic.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
#include <arpa/inet.h>
#include <fstream>
#include <iostream>
#include <math.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "../all_logcpp_libraries.hpp"
#include "../fast_library.hpp"
#include "../fast_endianless.hpp"
#include "../fastnetmon_types.hpp"
#include <boost/unordered/unordered_flat_map.hpp>
#include <boost/unordered_map.hpp>
#include <map>
#include <unordered_map>
#ifdef ABSEIL_TESTS
#include "absl/container/flat_hash_map.h"
#include "absl/container/node_hash_map.h"
#endif
#ifdef __MACH__
// On MacOS X we haven't clock_gettime(CLOCK_REALTIME, &ts) and should use another code
// http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
#include <mach/clock.h>
#include <mach/mach.h>
#define CLOCK_REALTIME 1111
clock_serv_t cclock;
// Create custom wrapper for Mac OS X
int clock_gettime(int clodk_type_do_not_used_really, struct timespec* ts) {
mach_timespec_t mts;
clock_get_time(cclock, &mts);
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
return 0;
}
#endif
log4cpp::Category& logger = log4cpp::Category::getRoot();
// We use these structures to tests smaller value
class udp_t {
public:
uint64_t in_bytes = 0;
};
class subnet_counter_small_t {
public:
udp_t udp;
};
// Runs tests for specific structure
template <typename T> void run_tests(std::vector<uint32_t> our_ips, T& data_structure) {
struct timespec start_time;
clock_gettime(CLOCK_REALTIME, &start_time);
unsigned long total_ops = 0;
uint64_t number_of_reruns = 1000;
total_ops = number_of_reruns * our_ips.size();
for (int j = 0; j < number_of_reruns; j++) {
for (const auto& ip : our_ips) {
data_structure[ip].udp.in_bytes++;
}
}
struct timespec finish_time;
clock_gettime(CLOCK_REALTIME, &finish_time);
unsigned long used_seconds = finish_time.tv_sec - start_time.tv_sec;
unsigned long used_nanoseconds = finish_time.tv_nsec - start_time.tv_nsec;
unsigned long total_used_nanoseconds = used_seconds * 1000000000 + used_nanoseconds;
float megaops_per_second = (float)total_ops / ((float)total_used_nanoseconds / (float)1000000000) / 1000000;
std::cout << "Total lookup time is " << used_seconds << " seconds" << std::endl;
std::cout << "Million of lookup ops per second: " << megaops_per_second << std::endl;
#ifdef __MACH__
mach_port_deallocate(mach_task_self(), cclock);
#endif
}
// Runs tests for specific structure
template <typename T> void run_scan_tests(T& data_structure, uint64_t& accumulator) {
struct timespec start_time;
clock_gettime(CLOCK_REALTIME, &start_time);
unsigned long total_ops = 0;
uint64_t number_of_reruns = 1000;
total_ops = number_of_reruns * data_structure.size();
for (int j = 0; j < number_of_reruns; j++) {
for (const auto& elem : data_structure) {
accumulator += elem.second.udp.in_bytes;
}
}
struct timespec finish_time;
clock_gettime(CLOCK_REALTIME, &finish_time);
unsigned long used_seconds = finish_time.tv_sec - start_time.tv_sec;
unsigned long used_nanoseconds = finish_time.tv_nsec - start_time.tv_nsec;
unsigned long total_used_nanoseconds = used_seconds * 1000000000 + used_nanoseconds;
float megaops_per_second = (float)total_ops / ((float)total_used_nanoseconds / (float)1000000000) / 1000000;
std::cout << "Total scan time is " << used_seconds << " seconds" << std::endl;
std::cout << "Million of full scan ops per second: " << megaops_per_second << std::endl;
#ifdef __MACH__
mach_port_deallocate(mach_task_self(), cclock);
#endif
}
int main() {
#ifdef __MACH__
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
#endif
std::string file_path = "/home/odintsov/cable_isp_ip_addresses_non_unique.txt";
std::string line;
std::ifstream myfile(file_path);
if (!myfile.is_open()) {
std::cerr << "Could not open file with IP list: " << file_path << std::endl;
return 1;
}
std::vector<uint32_t> our_ips_big_endian;
std::vector<uint32_t> our_ips_little_endian;
// We know file size and allocate that number of elements + some spare space
our_ips_big_endian.reserve(200000);
our_ips_little_endian.reserve(200000);
while (getline(myfile, line)) {
// It will be big endian internally
uint32_t ip = 0;
bool res = convert_ip_as_string_to_uint_safe(line, ip);
if (!res) {
std::cout << "Cannot parse IP " << line << std::endl;
continue;
}
our_ips_big_endian.push_back(ip);
// Convert it to little endian
our_ips_little_endian.push_back(fast_ntoh(ip));
}
std::cout << "Loaded " << our_ips_big_endian.size() << " IPs into memory" << std::endl;
{
uint64_t accumulator = 0;
std::cout << std::endl << "std::map big endian " << std::endl;
std::map<uint32_t, subnet_counter_t> std_map;
run_tests(our_ips_big_endian, std_map);
run_scan_tests(std_map, accumulator);
std_map.clear();
std::cout << "Accumulator value to guarantee no optimisation tricks from compiler: " << accumulator << std::endl;
}
{
uint64_t accumulator = 0;
std::cout << std::endl << "std::map little endian " << std::endl;
std::map<uint32_t, subnet_counter_t> std_map;
run_tests(our_ips_little_endian, std_map);
run_scan_tests(std_map, accumulator);
std_map.clear();
std::cout << "Accumulator value to guarantee no optimisation tricks from compiler: " << accumulator << std::endl;
}
{
uint64_t accumulator = 0;
std::cout << std::endl << "std::unordered_map big endian" << std::endl;
std::unordered_map<uint32_t, subnet_counter_t> std_unordered;
run_tests(our_ips_big_endian, std_unordered);
run_scan_tests(std_unordered, accumulator);
std_unordered.clear();
std::cout << "Bucket number: " << std_unordered.bucket_count() << std::endl;
std::cout << "Accumulator value to guarantee no optimisation tricks from compiler: " << accumulator << std::endl;
}
{
uint64_t accumulator = 0;
std::cout << std::endl << "std::unordered_map little endian" << std::endl;
std::unordered_map<uint32_t, subnet_counter_t> std_unordered;
run_tests(our_ips_little_endian, std_unordered);
run_scan_tests(std_unordered, accumulator);
std_unordered.clear();
std::cout << "Bucket number: " << std_unordered.bucket_count() << std::endl;
std::cout << "Accumulator value to guarantee no optimisation tricks from compiler: " << accumulator << std::endl;
}
{
uint64_t accumulator = 0;
std::cout << std::endl << "boost::unordered_map big endian " << std::endl;
boost::unordered_map<uint32_t, subnet_counter_t> boost_unordered;
run_tests(our_ips_big_endian, boost_unordered);
run_scan_tests(boost_unordered, accumulator);
boost_unordered.clear();
std::cout << "Bucket number: " << boost_unordered.bucket_count() << std::endl;
std::cout << "Accumulator value to guarantee no optimisation tricks from compiler: " << accumulator << std::endl;
}
{
uint64_t accumulator = 0;
std::cout << std::endl << "boost::unordered_map little endian " << std::endl;
boost::unordered_map<uint32_t, subnet_counter_t> boost_unordered;
run_tests(our_ips_little_endian, boost_unordered);
run_scan_tests(boost_unordered, accumulator);
boost_unordered.clear();
std::cout << "Bucket number: " << boost_unordered.bucket_count() << std::endl;
std::cout << "Accumulator value to guarantee no optimisation tricks from compiler: " << accumulator << std::endl;
}
{
uint64_t accumulator = 0;
std::cout << std::endl << "boost::unordered_flat_map big endian " << std::endl;
boost::unordered_flat_map<uint32_t, subnet_counter_t> boost_unordered;
run_tests(our_ips_big_endian, boost_unordered);
run_scan_tests(boost_unordered, accumulator);
boost_unordered.clear();
std::cout << "Bucket number: " << boost_unordered.bucket_count() << std::endl;
std::cout << "Accumulator value to guarantee no optimisation tricks from compiler: " << accumulator << std::endl;
}
{
uint64_t accumulator = 0;
std::cout << std::endl << "boost::unordered_flat_map little endian " << std::endl;
boost::unordered_flat_map<uint32_t, subnet_counter_t> boost_unordered;
run_tests(our_ips_little_endian, boost_unordered);
run_scan_tests(boost_unordered, accumulator);
boost_unordered.clear();
std::cout << "Bucket number: " << boost_unordered.bucket_count() << std::endl;
std::cout << "Accumulator value to guarantee no optimisation tricks from compiler: " << accumulator << std::endl;
}
#ifdef ABSEIL_TESTS
{
uint64_t accumulator = 0;
std::cout << std::endl << "absl::flat_hash_map little endian " << std::endl;
absl::flat_hash_map<uint32_t, subnet_counter_t> unordered;
run_tests(our_ips_little_endian, unordered);
run_scan_tests(unordered, accumulator);
unordered.clear();
std::cout << "Bucket number: " << unordered.bucket_count() << std::endl;
std::cout << "Accumulator value to guarantee no optimisation tricks from compiler: " << accumulator << std::endl;
}
{
uint64_t accumulator = 0;
std::cout << std::endl << "absl::flat_hash_map big endian " << std::endl;
absl::flat_hash_map<uint32_t, subnet_counter_t> unordered;
run_tests(our_ips_big_endian, unordered);
run_scan_tests(unordered, accumulator);
unordered.clear();
std::cout << "Bucket number: " << unordered.bucket_count() << std::endl;
std::cout << "Accumulator value to guarantee no optimisation tricks from compiler: " << accumulator << std::endl;
}
{
uint64_t accumulator = 0;
std::cout << std::endl << "absl::node_hash_map little endian " << std::endl;
absl::node_hash_map<uint32_t, subnet_counter_t> unordered;
run_tests(our_ips_little_endian, unordered);
run_scan_tests(unordered, accumulator);
unordered.clear();
std::cout << "Bucket number: " << unordered.bucket_count() << std::endl;
std::cout << "Accumulator value to guarantee no optimisation tricks from compiler: " << accumulator << std::endl;
}
{
uint64_t accumulator = 0;
std::cout << std::endl << "node_hash_map big endian " << std::endl;
absl::node_hash_map<uint32_t, subnet_counter_t> unordered;
run_tests(our_ips_big_endian, unordered);
run_scan_tests(unordered, accumulator);
unordered.clear();
std::cout << "Bucket number: " << unordered.bucket_count() << std::endl;
std::cout << "Accumulator value to guarantee no optimisation tricks from compiler: " << accumulator << std::endl;
}
#endif
}