-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpublisher_tests.cpp
357 lines (292 loc) · 10.5 KB
/
publisher_tests.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
#include <catch2/catch_all.hpp>
#include <thread>
#include <cxxmetrics/metrics_registry.hpp>
#include <cxxmetrics/simple_reservoir.hpp>
#include <cxxmetrics/sliding_window.hpp>
using namespace cxxmetrics;
using namespace cxxmetrics_literals;
using namespace std::chrono_literals;
template<typename TRepo = default_repository>
class test_publisher : public metrics_publisher<TRepo>
{
public:
constexpr test_publisher(metrics_registry<TRepo>& registry) noexcept :
metrics_publisher<TRepo>(registry)
{ }
template<typename T, typename... TArgs>
T& data(TArgs&&... args)
{
return this->template get_data<T>(std::forward<TArgs>(args)...);
}
template<typename T, typename... TArgs>
T* metric_data(const metric_path& name, TArgs&&... args)
{
return this->template get_data_for<T>(name, std::forward<TArgs>(args)...);
}
template<typename T>
bool has_metric_data(const metric_path& name)
{
return this->template has_data_for<T>(name);
}
bool exists(const metric_path& name) const
{
return this->has_metric(name);
}
std::string type_of(const metric_path& name) const
{
return this->metric_type(name);
}
std::string type_of(const basic_registered_metric& metric) const
{
return this->metric_type(metric);
}
template<typename THandler>
void visit_metric(const metric_path& path, THandler&& handler) const
{
return this->visit_one(path, std::forward<THandler>(handler));
}
template<typename THandler>
void visit_registry(THandler&& handler) const
{
return this->visit_all(std::forward<THandler>(handler));
}
const auto& opts(basic_registered_metric& metric)
{
return this->effective_options(metric);
}
};
struct test_pubdata : public basic_publish_options
{
int value_;
public:
test_pubdata() :
value_(0)
{ }
test_pubdata(int v) :
value_(v)
{ }
int value() const
{
return value_;
}
};
TEST_CASE("Publisher can get custom publish data", "[publisher]")
{
metrics_registry<> r;
test_publisher<> subject(r);
auto& v = subject.data<test_pubdata>(10);
REQUIRE(v.value() == 10);
auto& e = subject.data<test_pubdata>(2000);
REQUIRE(e.value() == 10);
REQUIRE(&e == &v);
}
TEST_CASE("Publisher can get metric-attached custom publish data", "[publisher]")
{
metrics_registry<> r;
test_publisher<> subject(r);
auto path = "Test"_m/"Counter"/"blah";
REQUIRE(!subject.exists(path));
r.counter(path);
REQUIRE(subject.exists(path));
REQUIRE(!subject.has_metric_data<test_pubdata>(path));
auto v = subject.metric_data<test_pubdata>(path, 10);
REQUIRE(v != nullptr);
REQUIRE(subject.has_metric_data<test_pubdata>(path));
REQUIRE(v->value() == 10);
auto e = subject.metric_data<test_pubdata>(path, 2000);
REQUIRE(e->value() == 10);
REQUIRE(e == v);
REQUIRE(subject.metric_data<test_pubdata>(path/"other") == nullptr);
REQUIRE(!subject.has_metric_data<test_pubdata>(path/"other"));
}
TEST_CASE("Publisher metric types are correctly resolved", "[publisher]")
{
int gaugeProvider = 7;
int gp2 = 8;
metrics_registry<> r;
test_publisher<> subject(r);
auto myCounter = r.counter("MyCounter");
REQUIRE(subject.type_of("MyCounter").find("counter") != std::string::npos);
r.ewma<1_min>("MyEWMA");
REQUIRE(subject.type_of("MyEWMA").find("ewma") != std::string::npos);
r.gauge("Gauge"/"Ref"_m, gaugeProvider);
r.gauge("Gauge"/"Ptr"_m, &gp2);
REQUIRE(subject.type_of("Gauge"/"Ref"_m).find("gauge") != std::string::npos);
REQUIRE(subject.type_of("Gauge"/"Ptr"_m).find("gauge") != std::string::npos);
r.histogram("HistogramS", simple_reservoir<long, 100>());
r.histogram("HistogramU", uniform_reservoir<long, 100>());
r.histogram("HistogramW", sliding_window_reservoir<long, 100>(100s));
REQUIRE(subject.type_of("HistogramS").find("histogram") != std::string::npos);
REQUIRE(subject.type_of("HistogramU").find("histogram") != std::string::npos);
REQUIRE(subject.type_of("HistogramW").find("histogram") != std::string::npos);
r.meter<1_sec, 1_min, 1_sec, 5_min>("Meter");
REQUIRE(subject.type_of("Meter").find("meter") != std::string::npos);
r.timer<1_sec, std::chrono::system_clock, simple_reservoir<typename std::chrono::system_clock::duration, 1024>, 1_min, 5_min>("TimerVerbose");
REQUIRE(subject.type_of("TimerVerbose").find("timer") != std::string::npos);
r.register_existing("MyCounter"/"Alias"_m, myCounter);
REQUIRE(subject.type_of("MyCounter"/"Alias"_m).find("counter") != std::string::npos);
REQUIRE(subject.type_of("NonExistent"/"Metric"_m).empty());
}
TEST_CASE("Publisher visiting one metric only visits one metric", "[publisher]")
{
metrics_registry<> r;
test_publisher<> subject(r);
*r.counter("MyCounter") += 99;
r.ewma<1_min>("MyEWMA");
r.meter<1_sec, 1_min, 1_sec, 5_min>("Meter");
int count = 0;
metric_value value(0);
auto visitor = [&count, &value](const metric_path& path, basic_registered_metric& metric) {
++count;
metric.aggregate([&value](const value_snapshot& ss) { value = ss.value(); });
};
subject.visit_metric("MyCounter", visitor);
REQUIRE(count == 1);
REQUIRE(value == metric_value(99));
subject.visit_metric("Nonexistent"/"Metric"_m, visitor);
REQUIRE(count == 1);
}
TEST_CASE("Publisher visit_all sanity check", "[publisher]")
{
metrics_registry<> r;
test_publisher<> subject(r);
r.counter("MyCounter");
r.ewma<1_min>("MyEWMA");
r.meter<1_sec, 1_min, 1_sec, 5_min>("Meter");
int count = 0;
auto visitor = [&count](const metric_path& path, basic_registered_metric& metric) {
++count;
};
subject.visit_registry(visitor);
REQUIRE(count == 3);
}
TEST_CASE("Publisher can get value publish options", "[publisher]")
{
metrics_registry<> r;
test_publisher<> subject(r);
*r.counter("MyCounter") += 100;
r.ewma<1_min>("MyEWMA");
r.meter<1_sec, 1_min, 1_sec, 5_min>("Meter");
int count = 0;
metric_value value(0);
auto visitor = [&](const metric_path& path, basic_registered_metric& metric) {
++count;
metric.aggregate([&](const value_snapshot& ss) {
if (subject.type_of(metric).find("counter") == std::string::npos)
return;
auto& opts = subject.opts(metric);
if (opts.value_options().scale())
value = ss.value() * metric_value(opts.value_options().scale().factor());
else
value = ss.value();
});
};
subject.visit_registry(visitor);
REQUIRE(value == metric_value(100));
REQUIRE(count == 3);
r.publish_options(publish_options(value_publish_options(0.5)));
subject.visit_registry(visitor);
REQUIRE(value == metric_value(50));
REQUIRE(count == 6);
}
TEST_CASE("Publisher can handle metric overrides on publish options", "[publisher]")
{
metrics_registry<> r;
test_publisher<> subject(r);
auto ctr = r.counter("MyCounter1");
*ctr += 1000;
r.register_existing("MyCounter2", ctr);
int count = 0;
metric_value value(0);
auto visitor = [&](const metric_path& path, basic_registered_metric& metric) {
++count;
metric.aggregate([&](const value_snapshot& ss) {
if (subject.type_of(metric).find("counter") == std::string::npos)
return;
auto& opts = subject.opts(metric);
if (opts.value_options().scale())
value += (ss.value() * metric_value(opts.value_options().scale().factor()));
else
value += ss.value();
});
};
r.publish_options("MyCounter2", publish_options(value_publish_options(0.5)));
subject.visit_registry(visitor);
REQUIRE(value == metric_value(1500));
REQUIRE(count == 2);
}
TEST_CASE("Publisher can get meter publish options", "[publisher]")
{
metrics_registry<> r;
test_publisher<> subject(r);
*r.counter("MyCounter") += 100;
auto& m = *r.meter<1_sec, 1_min, 1_sec, 5_min>("Meter");
for (int i = 0; i < 50; i++)
m.mark(10000);
int count = 0;
metric_value value(0);
auto visitor = [&](const metric_path& path, basic_registered_metric& metric) {
++count;
metric.aggregate([&](const meter_snapshot& ss) {
auto& opts = subject.opts(metric);
if (opts.meter_options().include_mean())
value = ss.value();
else
value = 0;
});
};
subject.visit_registry(visitor);
REQUIRE(round(value) >= 1);
REQUIRE(count == 2);
r.publish_options(publish_options(meter_publish_options(false)));
subject.visit_registry(visitor);
REQUIRE(value == metric_value(0));
REQUIRE(count == 4);
}
TEST_CASE("Publisher can get histogram options", "[publisher]")
{
metrics_registry<> r;
test_publisher<> subject(r);
*r.counter("MyCounter") += 100;
auto& h1 = *r.histogram("histogram", simple_reservoir<int64_t, 100>());
for (int i = 0; i < 50; i++)
h1.update(i * 2000);
int count = 0;
metric_value value(0);
std::unordered_map<long double, metric_value> values;
auto visitor = [&](const metric_path& path, basic_registered_metric& metric) {
++count;
metric.aggregate([&](const histogram_snapshot& ss) {
auto& opts = subject.opts(metric);
if (opts.histogram_options().include_count())
value = ss.count();
else
value = 0;
auto qvisitors = opts.histogram_options().quantiles();
qvisitors->visit(ss, [&](quantile q, const metric_value& v)
{
values.emplace(q, round(v));
});
ss.sample(9, [&](const metric_value& v, quantile q) {
values.emplace(q, round(v));
});
});
};
auto print_percentiles = [&](const auto& dict) {
INFO("Percentiles: ");
for (const auto &pair : dict)
INFO(" " << pair.first << ": " << pair.second);
};
subject.visit_registry(visitor);
REQUIRE(value == metric_value(50));
REQUIRE(values.size() == 12); // defaults
REQUIRE(count == 2);
print_percentiles(values);
values.clear();
r.publish_options(publish_options(histogram_publish_options(quantile_options<99_p>(), false)));
subject.visit_registry(visitor);
print_percentiles(values);
REQUIRE(value == metric_value(0));
REQUIRE(values.size() == 10);
REQUIRE(count == 4);
}