-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathstats_tests.cpp
More file actions
399 lines (345 loc) · 12.4 KB
/
stats_tests.cpp
File metadata and controls
399 lines (345 loc) · 12.4 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
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
// Copyright 2024 Joaquin M Lopez Munoz.
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#define BOOST_UNORDERED_ENABLE_STATS
#ifdef BOOST_UNORDERED_CFOA_TESTS
#include <boost/unordered/concurrent_flat_map.hpp>
#include <boost/unordered/concurrent_flat_set.hpp>
#include <boost/unordered/concurrent_node_map.hpp>
#include <boost/unordered/concurrent_node_set.hpp>
#include <boost/unordered/unordered_flat_map.hpp>
#include <boost/unordered/unordered_flat_set.hpp>
#include <boost/unordered/unordered_node_map.hpp>
#include <boost/unordered/unordered_node_set.hpp>
#include "../cfoa/helpers.hpp"
#else
#include "../helpers/unordered.hpp"
#endif
#include "../helpers/helpers.hpp"
#include "../helpers/random_values.hpp"
#include "../helpers/test.hpp"
#include <boost/assert.hpp>
#include <boost/core/make_span.hpp>
#include <cmath>
#include <cstring>
template <class T> struct unequal_allocator
{
typedef T value_type;
unequal_allocator(int n = 0): n_{n} {}
unequal_allocator(unequal_allocator const&) = default;
unequal_allocator(unequal_allocator&&) = default;
template <class U>
unequal_allocator(unequal_allocator<U> const& x): n_{x.n_} {}
BOOST_ATTRIBUTE_NODISCARD T* allocate(std::size_t n)
{
return static_cast<T*>(::operator new(n * sizeof(T)));
}
void deallocate(T* p, std::size_t) noexcept { ::operator delete(p); }
bool operator==(unequal_allocator const& x) const { return n_ == x.n_; }
bool operator!=(unequal_allocator const& x) const { return n_ != x.n_; }
int n_;
};
bool esentially_same(double x, double y)
{
// Some optimizer-related issues in GCC X86 result in last-bit differences
// on doubles that should otherwise be identical.
// https://stackoverflow.com/a/253874/213114
static constexpr double epsilon = 1.0E-6;
return fabs(x - y) <= ( (fabs(x) > fabs(y) ? fabs(x) : fabs(y)) * epsilon);
}
bool not_esentially_same(double x, double y)
{
return !esentially_same(x, y);
}
enum check_stats_contition
{
stats_empty=0,
stats_full,
stats_mostly_full // unsuccesful lookups may result in num_comparisons == 0
};
template <class Stats>
void check_stat(const Stats& s, check_stats_contition cond)
{
switch (cond) {
case stats_empty:
BOOST_TEST(esentially_same(s.average, 0.0));
BOOST_TEST(esentially_same(s.variance, 0.0));
BOOST_TEST(esentially_same(s.deviation, 0.0));
break;
case stats_full:
BOOST_TEST_GT(s.average, 0.0);
if(not_esentially_same(s.variance, 0.0)) {
BOOST_TEST_GT(s.variance, 0.0);
BOOST_TEST_GT(s.deviation, 0.0);
}
break;
case stats_mostly_full:
if(not_esentially_same(s.variance, 0.0)) {
BOOST_TEST_GT(s.average, 0.0);
BOOST_TEST_GT(s.variance, 0.0);
BOOST_TEST_GT(s.deviation, 0.0);
}
break;
default:
break;
}
}
template <class Stats> void check_stat(const Stats& s1, const Stats& s2)
{
BOOST_TEST(esentially_same(s1.average, s2.average));
BOOST_TEST(esentially_same(s1.variance, s2.variance));
BOOST_TEST(esentially_same(s1.deviation, s2.deviation));
}
template <class Stats>
void check_insertion_stats(const Stats& s, check_stats_contition cond)
{
switch (cond) {
case stats_empty:
BOOST_TEST_EQ(s.count, 0);
check_stat(s.probe_length, stats_empty);
break;
case stats_full:
BOOST_TEST_NE(s.count, 0);
check_stat(s.probe_length, stats_full);
break;
default:
BOOST_ASSERT(false); // insertion can't be mostly full
}
}
template <class Stats>
void check_insertion_stats(const Stats& s1, const Stats& s2)
{
BOOST_TEST_EQ(s1.count, s2.count);
check_stat(s1.probe_length, s2.probe_length);
}
template <class Stats>
void check_lookup_stats(const Stats& s, check_stats_contition cond)
{
check_stat(s.probe_length, cond == stats_empty? stats_empty : stats_full);
check_stat(s.num_comparisons, cond);
}
template <class Stats>
void check_lookup_stats(const Stats& s1, const Stats& s2)
{
BOOST_TEST_EQ(s1.count, s2.count);
check_stat(s1.probe_length, s2.probe_length);
check_stat(s1.num_comparisons, s2.num_comparisons);
}
template <class Stats>
void check_container_stats(const Stats& s, check_stats_contition cond)
{
if(cond == stats_mostly_full) {
BOOST_ASSERT(false); // mostly full only applies to unsuccessful lookup
}
check_insertion_stats(s.insertion, cond);
check_lookup_stats(s.successful_lookup, cond);
check_lookup_stats(
s.unsuccessful_lookup,
cond == stats_empty? stats_empty : stats_mostly_full);
}
template <class Stats>
void check_container_stats(const Stats& s1, const Stats& s2)
{
check_insertion_stats(s1.insertion, s2.insertion);
check_lookup_stats(s1.successful_lookup, s2.successful_lookup);
check_lookup_stats(s1.unsuccessful_lookup, s2.unsuccessful_lookup);
}
template <class Container> void insert_n(Container& c, std::size_t n)
{
#if defined(BOOST_UNORDERED_CFOA_TESTS)
using value_type = typename Container::value_type;
test::reset_sequence();
test::random_values<Container> l(n, test::sequential);
std::vector<value_type> v(l.begin(), l.end());
thread_runner(v, [&c](boost::span<value_type> sp) {
for (auto const& x : sp) {
c.insert(x);
}
});
#else
test::reset_sequence();
test::random_values<Container> l(n, test::sequential);
c.insert(l.begin(), l.end());
#endif
}
template <class Container> void test_stats()
{
using allocator_type = typename Container::allocator_type;
using stats = typename Container::stats;
Container c;
const Container& cc = c;
// Stats initially empty
stats s = cc.get_stats(); // using cc -> get_stats() is const
check_container_stats(s, stats_empty);
// Stats after insertion
insert_n(c, 10000);
s = cc.get_stats();
check_insertion_stats(s.insertion, stats_full); // insertions happened
check_lookup_stats(s.successful_lookup, stats_empty); // no duplicate values
check_lookup_stats(
s.unsuccessful_lookup, stats_mostly_full); // from insertion
#if !defined(BOOST_UNORDERED_CFOA_TESTS)
// Inequality due to rehashing
// May not hold in concurrent containers because of insertion retries
BOOST_TEST_GT(
s.insertion.count, s.unsuccessful_lookup.count);
#endif
// resets_stats() actually clears stats
c.reset_stats();
check_container_stats(cc.get_stats(), stats_empty);
// Stats after lookup
test::reset_sequence();
#if defined(BOOST_UNORDERED_CFOA_TESTS)
using key_type = typename Container::key_type;
using value_type = typename Container::value_type;
test::random_values<Container> l2(15000, test::sequential);
std::vector<value_type> v2(l2.begin(), l2.end());
std::atomic<std::size_t> found{0}, not_found{0};
thread_runner(v2, [&cc, &found, ¬_found](boost::span<value_type> sp) {
// Half the span looked up elementwise
auto sp1 = boost::make_span(sp.begin(), sp.size()/2);
for (auto const& x : sp1) {
if(cc.contains(test::get_key<Container>(x))) ++found;
else ++not_found;
}
// Second half looked up in bulk
std::vector<key_type> ksp2;
for (auto const& x : boost::make_span(
sp1.end(), static_cast<std::size_t>(sp.end() - sp1.end()))) {
ksp2.push_back(test::get_key<Container>(x));
}
auto visited = cc.visit(
ksp2.begin(), ksp2.end(), [](const value_type&) {});
found += visited;
not_found += ksp2.size() - visited;
});
#else
test::random_values<Container> v2(15000, test::sequential);
std::size_t found = 0, not_found = 0;
for (const auto& x: v2) {
if (cc.contains(test::get_key<Container>(x))) ++found;
else ++not_found;
}
#endif
// As many [un]successful lookups as recorded externally
s=cc.get_stats();
check_lookup_stats(s.successful_lookup, stats_full);
check_lookup_stats(s.unsuccessful_lookup, stats_mostly_full);
BOOST_TEST_EQ(s.successful_lookup.count, found);
BOOST_TEST_EQ(s.unsuccessful_lookup.count, not_found);
c.reset_stats();
s = cc.get_stats();
check_container_stats(s, stats_empty);
// Move constructor tests
c.clear();
insert_n(c, 1000);
insert_n(c, 1000); // produces successful lookups
// Move contructor
// Stats transferred to target and reset in source
s = cc.get_stats();
Container c2 = std::move(c);
check_container_stats(c.get_stats(), stats_empty);
check_container_stats(c2.get_stats(), s);
// Move constructor with equal allocator
// Stats transferred to target and reset in source
Container c3(std::move(c2), allocator_type());
check_container_stats(c2.get_stats(), stats_empty);
check_container_stats(c3.get_stats(), s);
// Move constructor with unequal allocator
// Target only has insertions, stats reset in source
Container c4(std::move(c3), allocator_type(1));
check_container_stats(c3.get_stats(), stats_empty);
check_insertion_stats(c4.get_stats().insertion, stats_full);
check_lookup_stats(c4.get_stats().successful_lookup, stats_empty);
check_lookup_stats(c4.get_stats().unsuccessful_lookup, stats_empty);
// Move assignment tests
// Move assignment with equal allocator
// Stats transferred to target and reset in source
Container c5, c6;
insert_n(c5,1000);
insert_n(c5,1000); // produces successful lookups
insert_n(c6,500);
insert_n(c6,500); // produces successful lookups
s = c5.get_stats();
check_container_stats(s, stats_full);
check_container_stats(c6.get_stats(), stats_full);
c6 = std::move(c5);
check_container_stats(c5.get_stats(), stats_empty);
check_container_stats(c6.get_stats(), s);
// Move assignment with unequal allocator
// Target only has insertions (if reset previously), stats reset in source
Container c7(allocator_type(1));
insert_n(c7,250);
insert_n(c7,250); // produces successful lookups
check_container_stats(c7.get_stats(), stats_full);
c7.reset_stats();
c7 = std::move(c6);
check_container_stats(c6.get_stats(), stats_empty);
check_insertion_stats(c7.get_stats().insertion, stats_full);
check_lookup_stats(c7.get_stats().successful_lookup, stats_empty);
check_lookup_stats(c7.get_stats().unsuccessful_lookup, stats_empty);
}
#if defined(BOOST_UNORDERED_CFOA_TESTS)
template <class Container, class ConcurrentContainer>
void test_stats_concurrent_unordered_interop()
{
ConcurrentContainer cc1;
insert_n(cc1,5000);
insert_n(cc1,5000); // produces successful lookups
auto s=cc1.get_stats();
Container c1(std::move(cc1));
check_container_stats(cc1.get_stats(),stats_empty);
check_container_stats(c1.get_stats(),s);
ConcurrentContainer cc2(std::move(c1));
check_container_stats(c1.get_stats(),stats_empty);
check_container_stats(cc2.get_stats(),s);
}
#endif
UNORDERED_AUTO_TEST (stats_) {
#if defined(BOOST_UNORDERED_CFOA_TESTS)
test_stats<
boost::concurrent_flat_map<
int, int, boost::hash<int>, std::equal_to<int>,
unequal_allocator< std::pair< const int, int> >>>();
test_stats<
boost::concurrent_node_map<
int, int, boost::hash<int>, std::equal_to<int>,
unequal_allocator< std::pair< const int, int> >>>();
test_stats<
boost::concurrent_flat_set<
int, boost::hash<int>, std::equal_to<int>, unequal_allocator<int>>>();
test_stats<
boost::concurrent_node_set<
int, boost::hash<int>, std::equal_to<int>, unequal_allocator<int>>>();
test_stats_concurrent_unordered_interop<
boost::unordered_flat_map<int, int>,
boost::concurrent_flat_map<int, int>>();
test_stats_concurrent_unordered_interop<
boost::unordered_node_map<int, int>,
boost::concurrent_node_map<int, int>>();
test_stats_concurrent_unordered_interop<
boost::unordered_flat_set<int>,
boost::concurrent_flat_set<int>>();
test_stats_concurrent_unordered_interop<
boost::unordered_node_set<int>,
boost::concurrent_node_set<int>>();
#elif defined(BOOST_UNORDERED_FOA_TESTS)
test_stats<
boost::unordered_flat_map<
int, int, boost::hash<int>, std::equal_to<int>,
unequal_allocator< std::pair< const int, int> >>>();
test_stats<
boost::unordered_flat_set<
int, boost::hash<int>, std::equal_to<int>, unequal_allocator<int>>>();
test_stats<
boost::unordered_node_map<
int, int, boost::hash<int>, std::equal_to<int>,
unequal_allocator< std::pair< const int, int> >>>();
test_stats<
boost::unordered_node_set<
int, boost::hash<int>, std::equal_to<int>, unequal_allocator<int>>>();
#else
// Closed-addressing containers do not provide stats
#endif
}
RUN_TESTS()