]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/recursordist/test-rec-tcounters_cc.cc
1 #define BOOST_TEST_DYN_LINK
2 #define BOOST_TEST_NO_MAIN
8 #include <boost/test/unit_test.hpp>
12 #include "rec-tcounters.hh"
14 static rec::GlobalCounters global
;
15 static thread_local
rec::TCounters
tlocal(global
);
17 BOOST_AUTO_TEST_SUITE(test_rec_tcounters_cc
)
19 BOOST_AUTO_TEST_CASE(destruct
)
23 const size_t count
= 100000;
24 std::thread
thread1([] {
25 for (size_t i
= 0; i
< count
; i
++) {
26 ++tlocal
.at(rec::Counter::servFails
);
29 std::thread
thread2([] {
30 for (size_t i
= 0; i
< count
; i
++) {
31 ++tlocal
.at(rec::Counter::nxDomains
);
36 BOOST_CHECK_EQUAL(global
.sum(rec::Counter::servFails
), count
);
37 BOOST_CHECK_EQUAL(global
.sum(rec::Counter::nxDomains
), count
);
40 BOOST_AUTO_TEST_CASE(update_fast
)
44 std::atomic
<uint64_t> done
{};
46 const size_t count
= 10000000;
47 std::thread
thread1([&done
] {
48 for (size_t i
= 0; i
< count
; i
++) {
49 ++tlocal
.at(rec::Counter::servFails
);
50 ++tlocal
.at(rec::Counter::nxDomains
);
51 tlocal
.at(rec::DoubleWAvgCounter::avgLatencyUsec
).add(1.1);
52 if (random() % 10000 == 0) {
58 std::thread
thread2([&done
] {
59 for (size_t i
= 0; i
< count
/ 2; i
++) {
60 ++tlocal
.at(rec::Counter::servFails
);
61 ++tlocal
.at(rec::Counter::nxDomains
);
62 tlocal
.at(rec::DoubleWAvgCounter::avgLatencyUsec
).add(2.2);
63 if (random() % 10000 == 0) {
69 std::thread
thread3([&done
] {
71 auto counts
= global
.aggregatedSnap();
72 BOOST_CHECK_EQUAL(counts
.uint64Count
[0], counts
.uint64Count
[1]);
73 auto avg
= counts
.at(rec::DoubleWAvgCounter::avgLatencyUsec
).avg
;
74 BOOST_CHECK(avg
== 0.0 || (avg
>= 1.1 && avg
<= 2.2));
80 BOOST_CHECK_EQUAL(global
.sum(rec::Counter::servFails
), count
+ count
/ 2);
81 BOOST_CHECK_EQUAL(global
.sum(rec::Counter::nxDomains
), count
+ count
/ 2);
82 auto avg
= global
.avg(rec::DoubleWAvgCounter::avgLatencyUsec
);
83 BOOST_CHECK(avg
>= 1.1 && avg
<= 2.2);
86 BOOST_AUTO_TEST_CASE(update_with_sleep
)
91 std::atomic
<int> done
{};
93 const size_t count
= 1000000;
94 std::thread
thread1([&done
] {
95 for (size_t i
= 0; i
< count
; i
++) {
96 ++tlocal
.at(rec::Counter::servFails
);
97 ++tlocal
.at(rec::Counter::nxDomains
);
98 tlocal
.at(rec::DoubleWAvgCounter::avgLatencyUsec
).add(1.1);
99 if (random() % 10000 == 0) {
102 struct timespec interval
106 nanosleep(&interval
, nullptr);
110 std::thread
thread2([&done
] {
111 for (size_t i
= 0; i
< count
/ 2; i
++) {
112 ++tlocal
.at(rec::Counter::servFails
);
113 ++tlocal
.at(rec::Counter::nxDomains
);
114 tlocal
.at(rec::DoubleWAvgCounter::avgLatencyUsec
).add(2.2);
115 if (random() % 10000 == 0) {
118 struct timespec interval
122 nanosleep(&interval
, nullptr);
126 std::thread
thread3([&done
] {
128 auto counts
= global
.aggregatedSnap();
129 BOOST_CHECK_EQUAL(counts
.uint64Count
[0], counts
.uint64Count
[1]);
130 auto avg
= counts
.at(rec::DoubleWAvgCounter::avgLatencyUsec
).avg
;
131 // std::cerr << avg << std::endl;
132 BOOST_CHECK(avg
== 0.0 || (avg
>= 1.1 && avg
<= 2.2));
133 struct timespec interval
137 nanosleep(&interval
, nullptr);
143 BOOST_CHECK_EQUAL(global
.sum(rec::Counter::servFails
), count
+ count
/ 2);
144 BOOST_CHECK_EQUAL(global
.sum(rec::Counter::nxDomains
), count
+ count
/ 2);
145 auto avg
= global
.avg(rec::DoubleWAvgCounter::avgLatencyUsec
);
146 BOOST_CHECK(avg
>= 1.1 && avg
<= 2.2);
149 BOOST_AUTO_TEST_SUITE_END()