counters_ut.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "counters.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NMonitoring;
  4. class TCountersPrinter: public ICountableConsumer {
  5. public:
  6. TCountersPrinter(IOutputStream* out)
  7. : Out_(out)
  8. , Level_(0)
  9. {
  10. }
  11. private:
  12. void OnCounter(
  13. const TString& labelName, const TString& labelValue,
  14. const TCounterForPtr* counter) override {
  15. Indent(Out_, Level_)
  16. << labelName << ':' << labelValue
  17. << " = " << counter->Val() << '\n';
  18. }
  19. void OnHistogram(
  20. const TString& labelName, const TString& labelValue,
  21. IHistogramSnapshotPtr snapshot, bool /*derivative*/) override {
  22. Indent(Out_, Level_)
  23. << labelName << ':' << labelValue
  24. << " = " << *snapshot << '\n';
  25. }
  26. void OnGroupBegin(
  27. const TString& labelName, const TString& labelValue,
  28. const TDynamicCounters*) override {
  29. Indent(Out_, Level_++) << labelName << ':' << labelValue << " {\n";
  30. }
  31. void OnGroupEnd(
  32. const TString&, const TString&,
  33. const TDynamicCounters*) override {
  34. Indent(Out_, --Level_) << "}\n";
  35. }
  36. static IOutputStream& Indent(IOutputStream* out, int level) {
  37. for (int i = 0; i < level; i++) {
  38. out->Write(" ");
  39. }
  40. return *out;
  41. }
  42. private:
  43. IOutputStream* Out_;
  44. int Level_ = 0;
  45. };
  46. Y_UNIT_TEST_SUITE(TDynamicCountersTest) {
  47. Y_UNIT_TEST(CountersConsumer) {
  48. TDynamicCounterPtr rootGroup(new TDynamicCounters());
  49. auto usersCounter = rootGroup->GetNamedCounter("users", "count");
  50. *usersCounter = 7;
  51. auto hostGroup = rootGroup->GetSubgroup("counters", "resources");
  52. auto cpuCounter = hostGroup->GetNamedCounter("resource", "cpu");
  53. *cpuCounter = 30;
  54. auto memGroup = hostGroup->GetSubgroup("resource", "mem");
  55. auto usedCounter = memGroup->GetCounter("used");
  56. auto freeCounter = memGroup->GetCounter("free");
  57. *usedCounter = 100;
  58. *freeCounter = 28;
  59. auto netGroup = hostGroup->GetSubgroup("resource", "net");
  60. auto rxCounter = netGroup->GetCounter("rx", true);
  61. auto txCounter = netGroup->GetCounter("tx", true);
  62. *rxCounter = 8;
  63. *txCounter = 9;
  64. TStringStream ss;
  65. TCountersPrinter printer(&ss);
  66. rootGroup->Accept("root", "counters", printer);
  67. UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
  68. "root:counters {\n"
  69. " counters:resources {\n"
  70. " resource:cpu = 30\n"
  71. " resource:mem {\n"
  72. " sensor:free = 28\n"
  73. " sensor:used = 100\n"
  74. " }\n"
  75. " resource:net {\n"
  76. " sensor:rx = 8\n"
  77. " sensor:tx = 9\n"
  78. " }\n"
  79. " }\n"
  80. " users:count = 7\n"
  81. "}\n");
  82. }
  83. Y_UNIT_TEST(MergeSubgroup) {
  84. TDynamicCounterPtr rootGroup(new TDynamicCounters());
  85. auto sensor1 = rootGroup->GetNamedCounter("sensor", "1");
  86. *sensor1 = 1;
  87. auto group1 = rootGroup->GetSubgroup("group", "1");
  88. auto sensor2 = group1->GetNamedCounter("sensor", "2");
  89. *sensor2 = 2;
  90. auto group2 = group1->GetSubgroup("group", "2");
  91. auto sensor3 = group2->GetNamedCounter("sensor", "3");
  92. *sensor3 = 3;
  93. rootGroup->MergeWithSubgroup("group", "1");
  94. TStringStream ss;
  95. TCountersPrinter printer(&ss);
  96. rootGroup->Accept("root", "counters", printer);
  97. UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
  98. "root:counters {\n"
  99. " group:2 {\n"
  100. " sensor:3 = 3\n"
  101. " }\n"
  102. " sensor:1 = 1\n"
  103. " sensor:2 = 2\n"
  104. "}\n");
  105. }
  106. Y_UNIT_TEST(ResetCounters) {
  107. TDynamicCounterPtr rootGroup(new TDynamicCounters());
  108. auto sensor1 = rootGroup->GetNamedCounter("sensor", "1");
  109. *sensor1 = 1;
  110. auto group1 = rootGroup->GetSubgroup("group", "1");
  111. auto sensor2 = group1->GetNamedCounter("sensor", "2");
  112. *sensor2 = 2;
  113. auto group2 = group1->GetSubgroup("group", "2");
  114. auto sensor3 = group2->GetNamedCounter("sensor", "3", true);
  115. *sensor3 = 3;
  116. rootGroup->ResetCounters(true);
  117. TStringStream ss1;
  118. TCountersPrinter printer1(&ss1);
  119. rootGroup->Accept("root", "counters", printer1);
  120. UNIT_ASSERT_STRINGS_EQUAL(ss1.Str(),
  121. "root:counters {\n"
  122. " group:1 {\n"
  123. " group:2 {\n"
  124. " sensor:3 = 0\n"
  125. " }\n"
  126. " sensor:2 = 2\n"
  127. " }\n"
  128. " sensor:1 = 1\n"
  129. "}\n");
  130. rootGroup->ResetCounters();
  131. TStringStream ss2;
  132. TCountersPrinter printer2(&ss2);
  133. rootGroup->Accept("root", "counters", printer2);
  134. UNIT_ASSERT_STRINGS_EQUAL(ss2.Str(),
  135. "root:counters {\n"
  136. " group:1 {\n"
  137. " group:2 {\n"
  138. " sensor:3 = 0\n"
  139. " }\n"
  140. " sensor:2 = 0\n"
  141. " }\n"
  142. " sensor:1 = 0\n"
  143. "}\n");
  144. }
  145. Y_UNIT_TEST(RemoveCounter) {
  146. TDynamicCounterPtr rootGroup(new TDynamicCounters());
  147. rootGroup->GetNamedCounter("label", "1");
  148. rootGroup->GetCounter("2");
  149. rootGroup->GetCounter("3");
  150. rootGroup->GetSubgroup("group", "1");
  151. rootGroup->RemoveNamedCounter("label", "1");
  152. rootGroup->RemoveNamedCounter("label", "5");
  153. rootGroup->RemoveNamedCounter("group", "1");
  154. rootGroup->RemoveCounter("2");
  155. rootGroup->RemoveCounter("5");
  156. TStringStream ss;
  157. TCountersPrinter printer(&ss);
  158. rootGroup->Accept("root", "counters", printer);
  159. UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
  160. "root:counters {\n"
  161. " group:1 {\n"
  162. " }\n"
  163. " sensor:3 = 0\n"
  164. "}\n");
  165. }
  166. Y_UNIT_TEST(RemoveSubgroup) {
  167. TDynamicCounterPtr rootGroup(new TDynamicCounters());
  168. rootGroup->GetSubgroup("group", "1");
  169. rootGroup->GetSubgroup("group", "2");
  170. rootGroup->GetCounter("2");
  171. rootGroup->RemoveSubgroup("group", "1");
  172. rootGroup->RemoveSubgroup("group", "3");
  173. rootGroup->RemoveSubgroup("sensor", "2");
  174. TStringStream ss;
  175. TCountersPrinter printer(&ss);
  176. rootGroup->Accept("root", "counters", printer);
  177. UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
  178. "root:counters {\n"
  179. " group:2 {\n"
  180. " }\n"
  181. " sensor:2 = 0\n"
  182. "}\n");
  183. }
  184. Y_UNIT_TEST(ExpiringCounters) {
  185. TDynamicCounterPtr rootGroup{new TDynamicCounters()};
  186. {
  187. auto c = rootGroup->GetExpiringCounter("foo");
  188. auto h = rootGroup->GetExpiringHistogram("bar", ExplicitHistogram({1, 42}));
  189. h->Collect(15);
  190. TStringStream ss;
  191. TCountersPrinter printer(&ss);
  192. rootGroup->Accept("root", "counters", printer);
  193. UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
  194. "root:counters {\n"
  195. " sensor:bar = {1: 0, 42: 1, inf: 0}\n"
  196. " sensor:foo = 0\n"
  197. "}\n");
  198. }
  199. TStringStream ss;
  200. TCountersPrinter printer(&ss);
  201. rootGroup->Accept("root", "counters", printer);
  202. UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
  203. "root:counters {\n"
  204. "}\n");
  205. }
  206. Y_UNIT_TEST(ExpiringCountersDiesAfterRegistry) {
  207. TDynamicCounters::TCounterPtr ptr;
  208. {
  209. TDynamicCounterPtr rootGroup{new TDynamicCounters()};
  210. ptr = rootGroup->GetExpiringCounter("foo");
  211. TStringStream ss;
  212. TCountersPrinter printer(&ss);
  213. rootGroup->Accept("root", "counters", printer);
  214. UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
  215. "root:counters {\n"
  216. " sensor:foo = 0\n"
  217. "}\n");
  218. }
  219. }
  220. Y_UNIT_TEST(HistogramCounter) {
  221. TDynamicCounterPtr rootGroup(new TDynamicCounters());
  222. auto h = rootGroup->GetHistogram("timeMillis", ExponentialHistogram(4, 2));
  223. for (i64 i = 1; i < 100; i++) {
  224. h->Collect(i);
  225. }
  226. TStringStream ss;
  227. TCountersPrinter printer(&ss);
  228. rootGroup->Accept("root", "counters", printer);
  229. UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
  230. "root:counters {\n"
  231. " sensor:timeMillis = {1: 1, 2: 1, 4: 2, inf: 95}\n"
  232. "}\n");
  233. }
  234. Y_UNIT_TEST(CounterLookupCounter) {
  235. TDynamicCounterPtr rootGroup(new TDynamicCounters());
  236. TDynamicCounters::TCounterPtr lookups = rootGroup->GetCounter("Lookups", true);
  237. rootGroup->SetLookupCounter(lookups);
  238. // Create subtree and check that counter is inherited
  239. TDynamicCounterPtr serviceGroup = rootGroup->GetSubgroup("service", "MyService");
  240. UNIT_ASSERT_VALUES_EQUAL(lookups->Val(), 1);
  241. TDynamicCounterPtr subGroup = serviceGroup->GetSubgroup("component", "MyComponent");
  242. UNIT_ASSERT_VALUES_EQUAL(lookups->Val(), 2);
  243. auto counter = subGroup->GetNamedCounter("range", "20 msec", true);
  244. UNIT_ASSERT_VALUES_EQUAL(lookups->Val(), 3);
  245. auto hist = subGroup->GetHistogram("timeMsec", ExponentialHistogram(4, 2));
  246. UNIT_ASSERT_VALUES_EQUAL(lookups->Val(), 4);
  247. // Replace the counter for subGroup
  248. auto subGroupLookups = rootGroup->GetCounter("LookupsInMyComponent", true);
  249. UNIT_ASSERT_VALUES_EQUAL(lookups->Val(), 5);
  250. subGroup->SetLookupCounter(subGroupLookups);
  251. auto counter2 = subGroup->GetNamedCounter("range", "30 msec", true);
  252. UNIT_ASSERT_VALUES_EQUAL(subGroupLookups->Val(), 1);
  253. UNIT_ASSERT_VALUES_EQUAL(lookups->Val(), 5);
  254. }
  255. Y_UNIT_TEST(FindCounters) {
  256. TDynamicCounterPtr rootGroup(new TDynamicCounters());
  257. auto counter = rootGroup->FindCounter("counter1");
  258. UNIT_ASSERT(!counter);
  259. rootGroup->GetCounter("counter1");
  260. counter = rootGroup->FindCounter("counter1");
  261. UNIT_ASSERT(counter);
  262. counter = rootGroup->FindNamedCounter("name", "counter2");
  263. UNIT_ASSERT(!counter);
  264. rootGroup->GetNamedCounter("name", "counter2");
  265. counter = rootGroup->FindNamedCounter("name", "counter2");
  266. UNIT_ASSERT(counter);
  267. auto histogram = rootGroup->FindHistogram("histogram1");
  268. UNIT_ASSERT(!histogram);
  269. rootGroup->GetHistogram("histogram1", ExponentialHistogram(4, 2));
  270. histogram = rootGroup->FindHistogram("histogram1");
  271. UNIT_ASSERT(histogram);
  272. histogram = rootGroup->FindNamedHistogram("name", "histogram2");
  273. UNIT_ASSERT(!histogram);
  274. rootGroup->GetNamedHistogram("name", "histogram2", ExponentialHistogram(4, 2));
  275. histogram = rootGroup->FindNamedHistogram("name", "histogram2");
  276. UNIT_ASSERT(histogram);
  277. }
  278. }