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
|
#include "counters.h"
#include <library/cpp/testing/unittest/registar.h>
using namespace NMonitoring;
class TCountersPrinter: public ICountableConsumer {
public:
TCountersPrinter(IOutputStream* out)
: Out_(out)
, Level_(0)
{
}
private:
void OnCounter(
const TString& labelName, const TString& labelValue,
const TCounterForPtr* counter) override {
Indent(Out_, Level_)
<< labelName << ':' << labelValue
<< " = " << counter->Val() << '\n';
}
void OnHistogram(
const TString& labelName, const TString& labelValue,
IHistogramSnapshotPtr snapshot, bool /*derivative*/) override {
Indent(Out_, Level_)
<< labelName << ':' << labelValue
<< " = " << *snapshot << '\n';
}
void OnGroupBegin(
const TString& labelName, const TString& labelValue,
const TDynamicCounters*) override {
Indent(Out_, Level_++) << labelName << ':' << labelValue << " {\n";
}
void OnGroupEnd(
const TString&, const TString&,
const TDynamicCounters*) override {
Indent(Out_, --Level_) << "}\n";
}
static IOutputStream& Indent(IOutputStream* out, int level) {
for (int i = 0; i < level; i++) {
out->Write(" ");
}
return *out;
}
private:
IOutputStream* Out_;
int Level_ = 0;
};
Y_UNIT_TEST_SUITE(TDynamicCountersTest) {
Y_UNIT_TEST(CountersConsumer) {
TDynamicCounterPtr rootGroup(new TDynamicCounters());
auto usersCounter = rootGroup->GetNamedCounter("users", "count");
*usersCounter = 7;
auto hostGroup = rootGroup->GetSubgroup("counters", "resources");
auto cpuCounter = hostGroup->GetNamedCounter("resource", "cpu");
*cpuCounter = 30;
auto memGroup = hostGroup->GetSubgroup("resource", "mem");
auto usedCounter = memGroup->GetCounter("used");
auto freeCounter = memGroup->GetCounter("free");
*usedCounter = 100;
*freeCounter = 28;
auto netGroup = hostGroup->GetSubgroup("resource", "net");
auto rxCounter = netGroup->GetCounter("rx", true);
auto txCounter = netGroup->GetCounter("tx", true);
*rxCounter = 8;
*txCounter = 9;
TStringStream ss;
TCountersPrinter printer(&ss);
rootGroup->Accept("root", "counters", printer);
UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
"root:counters {\n"
" counters:resources {\n"
" resource:cpu = 30\n"
" resource:mem {\n"
" sensor:free = 28\n"
" sensor:used = 100\n"
" }\n"
" resource:net {\n"
" sensor:rx = 8\n"
" sensor:tx = 9\n"
" }\n"
" }\n"
" users:count = 7\n"
"}\n");
}
Y_UNIT_TEST(MergeSubgroup) {
TDynamicCounterPtr rootGroup(new TDynamicCounters());
auto sensor1 = rootGroup->GetNamedCounter("sensor", "1");
*sensor1 = 1;
auto group1 = rootGroup->GetSubgroup("group", "1");
auto sensor2 = group1->GetNamedCounter("sensor", "2");
*sensor2 = 2;
auto group2 = group1->GetSubgroup("group", "2");
auto sensor3 = group2->GetNamedCounter("sensor", "3");
*sensor3 = 3;
rootGroup->MergeWithSubgroup("group", "1");
TStringStream ss;
TCountersPrinter printer(&ss);
rootGroup->Accept("root", "counters", printer);
UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
"root:counters {\n"
" group:2 {\n"
" sensor:3 = 3\n"
" }\n"
" sensor:1 = 1\n"
" sensor:2 = 2\n"
"}\n");
}
Y_UNIT_TEST(ResetCounters) {
TDynamicCounterPtr rootGroup(new TDynamicCounters());
auto sensor1 = rootGroup->GetNamedCounter("sensor", "1");
*sensor1 = 1;
auto group1 = rootGroup->GetSubgroup("group", "1");
auto sensor2 = group1->GetNamedCounter("sensor", "2");
*sensor2 = 2;
auto group2 = group1->GetSubgroup("group", "2");
auto sensor3 = group2->GetNamedCounter("sensor", "3", true);
*sensor3 = 3;
rootGroup->ResetCounters(true);
TStringStream ss1;
TCountersPrinter printer1(&ss1);
rootGroup->Accept("root", "counters", printer1);
UNIT_ASSERT_STRINGS_EQUAL(ss1.Str(),
"root:counters {\n"
" group:1 {\n"
" group:2 {\n"
" sensor:3 = 0\n"
" }\n"
" sensor:2 = 2\n"
" }\n"
" sensor:1 = 1\n"
"}\n");
rootGroup->ResetCounters();
TStringStream ss2;
TCountersPrinter printer2(&ss2);
rootGroup->Accept("root", "counters", printer2);
UNIT_ASSERT_STRINGS_EQUAL(ss2.Str(),
"root:counters {\n"
" group:1 {\n"
" group:2 {\n"
" sensor:3 = 0\n"
" }\n"
" sensor:2 = 0\n"
" }\n"
" sensor:1 = 0\n"
"}\n");
}
Y_UNIT_TEST(RemoveCounter) {
TDynamicCounterPtr rootGroup(new TDynamicCounters());
rootGroup->GetNamedCounter("label", "1");
rootGroup->GetCounter("2");
rootGroup->GetCounter("3");
rootGroup->GetSubgroup("group", "1");
rootGroup->RemoveNamedCounter("label", "1");
rootGroup->RemoveNamedCounter("label", "5");
rootGroup->RemoveNamedCounter("group", "1");
rootGroup->RemoveCounter("2");
rootGroup->RemoveCounter("5");
TStringStream ss;
TCountersPrinter printer(&ss);
rootGroup->Accept("root", "counters", printer);
UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
"root:counters {\n"
" group:1 {\n"
" }\n"
" sensor:3 = 0\n"
"}\n");
}
Y_UNIT_TEST(RemoveSubgroup) {
TDynamicCounterPtr rootGroup(new TDynamicCounters());
rootGroup->GetSubgroup("group", "1");
rootGroup->GetSubgroup("group", "2");
rootGroup->GetCounter("2");
rootGroup->RemoveSubgroup("group", "1");
rootGroup->RemoveSubgroup("group", "3");
rootGroup->RemoveSubgroup("sensor", "2");
TStringStream ss;
TCountersPrinter printer(&ss);
rootGroup->Accept("root", "counters", printer);
UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
"root:counters {\n"
" group:2 {\n"
" }\n"
" sensor:2 = 0\n"
"}\n");
}
Y_UNIT_TEST(ExpiringCounters) {
TDynamicCounterPtr rootGroup{new TDynamicCounters()};
{
auto c = rootGroup->GetExpiringCounter("foo");
auto h = rootGroup->GetExpiringHistogram("bar", ExplicitHistogram({1, 42}));
h->Collect(15);
TStringStream ss;
TCountersPrinter printer(&ss);
rootGroup->Accept("root", "counters", printer);
UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
"root:counters {\n"
" sensor:bar = {1: 0, 42: 1, inf: 0}\n"
" sensor:foo = 0\n"
"}\n");
}
TStringStream ss;
TCountersPrinter printer(&ss);
rootGroup->Accept("root", "counters", printer);
UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
"root:counters {\n"
"}\n");
}
Y_UNIT_TEST(ExpiringCountersDiesAfterRegistry) {
TDynamicCounters::TCounterPtr ptr;
{
TDynamicCounterPtr rootGroup{new TDynamicCounters()};
ptr = rootGroup->GetExpiringCounter("foo");
TStringStream ss;
TCountersPrinter printer(&ss);
rootGroup->Accept("root", "counters", printer);
UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
"root:counters {\n"
" sensor:foo = 0\n"
"}\n");
}
}
Y_UNIT_TEST(HistogramCounter) {
TDynamicCounterPtr rootGroup(new TDynamicCounters());
auto h = rootGroup->GetHistogram("timeMillis", ExponentialHistogram(4, 2));
for (i64 i = 1; i < 100; i++) {
h->Collect(i);
}
TStringStream ss;
TCountersPrinter printer(&ss);
rootGroup->Accept("root", "counters", printer);
UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
"root:counters {\n"
" sensor:timeMillis = {1: 1, 2: 1, 4: 2, inf: 95}\n"
"}\n");
}
Y_UNIT_TEST(CounterLookupCounter) {
TDynamicCounterPtr rootGroup(new TDynamicCounters());
TDynamicCounters::TCounterPtr lookups = rootGroup->GetCounter("Lookups", true);
rootGroup->SetLookupCounter(lookups);
// Create subtree and check that counter is inherited
TDynamicCounterPtr serviceGroup = rootGroup->GetSubgroup("service", "MyService");
UNIT_ASSERT_VALUES_EQUAL(lookups->Val(), 1);
TDynamicCounterPtr subGroup = serviceGroup->GetSubgroup("component", "MyComponent");
UNIT_ASSERT_VALUES_EQUAL(lookups->Val(), 2);
auto counter = subGroup->GetNamedCounter("range", "20 msec", true);
UNIT_ASSERT_VALUES_EQUAL(lookups->Val(), 3);
auto hist = subGroup->GetHistogram("timeMsec", ExponentialHistogram(4, 2));
UNIT_ASSERT_VALUES_EQUAL(lookups->Val(), 4);
// Replace the counter for subGroup
auto subGroupLookups = rootGroup->GetCounter("LookupsInMyComponent", true);
UNIT_ASSERT_VALUES_EQUAL(lookups->Val(), 5);
subGroup->SetLookupCounter(subGroupLookups);
auto counter2 = subGroup->GetNamedCounter("range", "30 msec", true);
UNIT_ASSERT_VALUES_EQUAL(subGroupLookups->Val(), 1);
UNIT_ASSERT_VALUES_EQUAL(lookups->Val(), 5);
}
Y_UNIT_TEST(FindCounters) {
TDynamicCounterPtr rootGroup(new TDynamicCounters());
auto counter = rootGroup->FindCounter("counter1");
UNIT_ASSERT(!counter);
rootGroup->GetCounter("counter1");
counter = rootGroup->FindCounter("counter1");
UNIT_ASSERT(counter);
counter = rootGroup->FindNamedCounter("name", "counter2");
UNIT_ASSERT(!counter);
rootGroup->GetNamedCounter("name", "counter2");
counter = rootGroup->FindNamedCounter("name", "counter2");
UNIT_ASSERT(counter);
auto histogram = rootGroup->FindHistogram("histogram1");
UNIT_ASSERT(!histogram);
rootGroup->GetHistogram("histogram1", ExponentialHistogram(4, 2));
histogram = rootGroup->FindHistogram("histogram1");
UNIT_ASSERT(histogram);
histogram = rootGroup->FindNamedHistogram("name", "histogram2");
UNIT_ASSERT(!histogram);
rootGroup->GetNamedHistogram("name", "histogram2", ExponentialHistogram(4, 2));
histogram = rootGroup->FindNamedHistogram("name", "histogram2");
UNIT_ASSERT(histogram);
}
}
|