-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreservoir_test.cpp
147 lines (113 loc) · 3.75 KB
/
reservoir_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
#include <catch2/catch_all.hpp>
#include <cxxmetrics/simple_reservoir.hpp>
#include <cxxmetrics/uniform_reservoir.hpp>
#include <cxxmetrics/sliding_window.hpp>
#include "helpers.hpp"
using namespace std;
using namespace cxxmetrics;
using namespace cxxmetrics_literals;
TEST_CASE("Uniform Reservoir on exact count", "[reservoir]")
{
uniform_reservoir<double, 5> r;
r.update(10.0);
r.update(15.0);
r.update(30.0);
r.update(40.0);
r.update(45.0);
auto s = r.snapshot();
REQUIRE_THAT(s.min(), Catch::Matchers::WithinULP(10.0, 1));
REQUIRE_THAT(s.max(), Catch::Matchers::WithinULP(45.0, 1));
REQUIRE(std::abs(static_cast<double>(s.value<99_p>()) - 45.0) < 1);
REQUIRE(std::abs(static_cast<double>(s.value<60_p>()) - 35.0) <= 1);
REQUIRE_THAT(s.mean(), Catch::Matchers::WithinULP(28.0, 1));
uniform_reservoir<double, 5> q = r;
}
TEST_CASE("Uniform Reservoir with overflow", "[reservoir]")
{
uniform_reservoir<double, 100> r;
uniform_real_distribution<> d(100.0, 200.0);
default_random_engine engine;
for (int i = 0; i < 1000; i++)
r.update(d(engine));
auto s = r.snapshot();
auto min = s.min();
auto max = s.max();
auto mean = s.mean();
auto p50 = s.value<50_p>();
REQUIRE(std::abs(static_cast<double>(min) - 100) < 20);
REQUIRE(std::abs(static_cast<double>(max) - 200) < 20);
REQUIRE(std::abs(static_cast<double>(mean) - 150) < 20);
REQUIRE(std::abs(static_cast<double>(p50) - 150) < 20);
}
TEST_CASE("Simple Reservoir overflow", "[reservoir]")
{
simple_reservoir<double, 5> r;
r.update(200);
r.update(10);
r.update(13);
r.update(10.0);
r.update(15.0);
r.update(30.0);
r.update(40.0);
r.update(45.0);
auto s = r.snapshot();
REQUIRE_THAT(s.min(), Catch::Matchers::WithinULP(10.0, 1));
REQUIRE_THAT(s.max(), Catch::Matchers::WithinULP(45.0, 1));
REQUIRE(std::abs(static_cast<double>(s.value<99_p>()) - 45.0) < 1);
REQUIRE(std::abs(static_cast<double>(s.value<60_p>()) - 35.0) <= 1);
REQUIRE_THAT(s.mean(), Catch::Matchers::WithinULP(28.0, 1));
simple_reservoir<double, 5> q = r;
}
TEST_CASE("Simple Reservoir threaded updates with snapshots", "[reservoir]")
{
simple_reservoir<double, 50> r;
bool go = true;
auto worker = [&r, &go]() {
std::default_random_engine eng;
std::uniform_real_distribution<double> dist(0, 100000);
while (go)
r.update(dist(eng));
};
std::vector<std::thread> threads;
threads.emplace_back(worker);
// make sure the reservoir is full
for (int i = 0; i < 50; i++)
r.update(50);
std::size_t minsize = 40; // a reasonable ratio to account for threads competing
for (int i = 0; i < 1000; i++)
{
auto s = r.snapshot();
CAPTURE(i);
REQUIRE(s.size() >= minsize);
}
go = false;
for (auto& thr : threads)
thr.join();
}
TEST_CASE("Sliding Window Reservoir only gets window data", "[reservoir]")
{
unsigned time = 500;
mock_clock clk(time);
sliding_window_reservoir<double, 10, mock_clock> r(100, clk);
r.update(200);
time += 20;
r.update(10);
time += 20;
r.update(13);
time += 20;
r.update(10.0);
time += 20;
r.update(20.0);
time += 60;
r.update(30.0);
r.update(40.0);
r.update(60.0);
time += 40;
auto s = r.snapshot();
REQUIRE_THAT(s.min(), Catch::Matchers::WithinULP(20.0, 1));
REQUIRE_THAT(s.max(), Catch::Matchers::WithinULP(60.0, 1));
REQUIRE(std::abs(static_cast<double>(s.value<99_p>()) - 60.0) < 1);
REQUIRE(std::abs(static_cast<double>(s.value<60_p>()) - 40.0) <= 1);
REQUIRE_THAT(s.mean(), Catch::Matchers::WithinULP(37.5, 1));
sliding_window_reservoir<double, 10, mock_clock> q = r;
}