encode_ut.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "encode.h"
  2. #include <library/cpp/monlib/encode/json/json.h>
  3. #include <library/cpp/monlib/encode/spack/spack_v1.h>
  4. #include <library/cpp/monlib/encode/protobuf/protobuf.h>
  5. #include <library/cpp/monlib/encode/protobuf/protos/samples.pb.h>
  6. #include <library/cpp/testing/unittest/registar.h>
  7. #include <util/generic/buffer.h>
  8. #include <util/stream/buffer.h>
  9. namespace NMonitoring {
  10. struct TTestData: public TDynamicCounters {
  11. TTestData() {
  12. auto hostGroup = GetSubgroup("counters", "resources");
  13. {
  14. auto cpuCounter = hostGroup->GetNamedCounter("resource", "cpu");
  15. *cpuCounter = 30;
  16. auto memGroup = hostGroup->GetSubgroup("resource", "mem");
  17. auto usedCounter = memGroup->GetCounter("used");
  18. auto freeCounter = memGroup->GetCounter("free");
  19. *usedCounter = 100;
  20. *freeCounter = 28;
  21. auto netGroup = hostGroup->GetSubgroup("resource", "net");
  22. auto rxCounter = netGroup->GetCounter("rx", true);
  23. auto txCounter = netGroup->GetCounter("tx", true);
  24. *rxCounter = 8;
  25. *txCounter = 9;
  26. }
  27. auto usersCounter = GetNamedCounter("users", "count");
  28. *usersCounter = 7;
  29. auto responseTimeMillis = GetHistogram("responseTimeMillis", ExplicitHistogram({1, 5, 10, 15, 20, 100, 200}));
  30. for (i64 i = 0; i < 400; i++) {
  31. responseTimeMillis->Collect(i);
  32. }
  33. }
  34. };
  35. void AssertLabelsEqual(const NProto::TLabel& l, TStringBuf name, TStringBuf value) {
  36. UNIT_ASSERT_STRINGS_EQUAL(l.GetName(), name);
  37. UNIT_ASSERT_STRINGS_EQUAL(l.GetValue(), value);
  38. }
  39. void AssertResult(const NProto::TSingleSamplesList& samples) {
  40. UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 7);
  41. {
  42. auto s = samples.GetSamples(0);
  43. UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2);
  44. AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
  45. AssertLabelsEqual(s.GetLabels(1), "resource", "cpu");
  46. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
  47. UNIT_ASSERT_DOUBLES_EQUAL(s.GetFloat64(), 30.0, Min<double>());
  48. }
  49. {
  50. auto s = samples.GetSamples(1);
  51. UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 3);
  52. AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
  53. AssertLabelsEqual(s.GetLabels(1), "resource", "mem");
  54. AssertLabelsEqual(s.GetLabels(2), "sensor", "free");
  55. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
  56. UNIT_ASSERT_DOUBLES_EQUAL(s.GetFloat64(), 28.0, Min<double>());
  57. }
  58. {
  59. auto s = samples.GetSamples(2);
  60. UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 3);
  61. AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
  62. AssertLabelsEqual(s.GetLabels(1), "resource", "mem");
  63. AssertLabelsEqual(s.GetLabels(2), "sensor", "used");
  64. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
  65. UNIT_ASSERT_DOUBLES_EQUAL(s.GetFloat64(), 100.0, Min<double>());
  66. }
  67. {
  68. auto s = samples.GetSamples(3);
  69. UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 3);
  70. AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
  71. AssertLabelsEqual(s.GetLabels(1), "resource", "net");
  72. AssertLabelsEqual(s.GetLabels(2), "sensor", "rx");
  73. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE);
  74. UNIT_ASSERT_VALUES_EQUAL(s.GetUint64(), 8);
  75. }
  76. {
  77. auto s = samples.GetSamples(4);
  78. UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 3);
  79. AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
  80. AssertLabelsEqual(s.GetLabels(1), "resource", "net");
  81. AssertLabelsEqual(s.GetLabels(2), "sensor", "tx");
  82. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE);
  83. UNIT_ASSERT_VALUES_EQUAL(s.GetUint64(), 9);
  84. }
  85. {
  86. auto s = samples.GetSamples(5);
  87. UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1);
  88. AssertLabelsEqual(s.GetLabels(0), "sensor", "responseTimeMillis");
  89. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::HIST_RATE);
  90. const NProto::THistogram& h = s.GetHistogram();
  91. UNIT_ASSERT_EQUAL(h.BoundsSize(), 8);
  92. UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(0), 1, Min<double>());
  93. UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(1), 5, Min<double>());
  94. UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(2), 10, Min<double>());
  95. UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(3), 15, Min<double>());
  96. UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(4), 20, Min<double>());
  97. UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(5), 100, Min<double>());
  98. UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(6), 200, Min<double>());
  99. UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(7), Max<double>(), Min<double>());
  100. UNIT_ASSERT_EQUAL(h.ValuesSize(), 8);
  101. UNIT_ASSERT_EQUAL(h.GetValues(0), 2);
  102. UNIT_ASSERT_EQUAL(h.GetValues(1), 4);
  103. UNIT_ASSERT_EQUAL(h.GetValues(2), 5);
  104. UNIT_ASSERT_EQUAL(h.GetValues(3), 5);
  105. UNIT_ASSERT_EQUAL(h.GetValues(4), 5);
  106. UNIT_ASSERT_EQUAL(h.GetValues(5), 80);
  107. UNIT_ASSERT_EQUAL(h.GetValues(6), 100);
  108. UNIT_ASSERT_EQUAL(h.GetValues(7), 199);
  109. }
  110. {
  111. auto s = samples.GetSamples(6);
  112. UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1);
  113. AssertLabelsEqual(s.GetLabels(0), "users", "count");
  114. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
  115. UNIT_ASSERT_DOUBLES_EQUAL(s.GetFloat64(), 7, Min<double>());
  116. }
  117. }
  118. Y_UNIT_TEST_SUITE(TDynamicCountersEncodeTest) {
  119. TTestData Data;
  120. Y_UNIT_TEST(Json) {
  121. TString result;
  122. {
  123. TStringOutput out(result);
  124. auto encoder = CreateEncoder(&out, EFormat::JSON);
  125. Data.Accept(TString(), TString(), *encoder);
  126. }
  127. NProto::TSingleSamplesList samples;
  128. {
  129. auto e = EncoderProtobuf(&samples);
  130. DecodeJson(result, e.Get());
  131. }
  132. AssertResult(samples);
  133. }
  134. Y_UNIT_TEST(Spack) {
  135. TBuffer result;
  136. {
  137. TBufferOutput out(result);
  138. auto encoder = CreateEncoder(&out, EFormat::SPACK);
  139. Data.Accept(TString(), TString(), *encoder);
  140. }
  141. NProto::TSingleSamplesList samples;
  142. {
  143. auto e = EncoderProtobuf(&samples);
  144. TBufferInput in(result);
  145. DecodeSpackV1(&in, e.Get());
  146. }
  147. AssertResult(samples);
  148. }
  149. Y_UNIT_TEST(PrivateSubgroupIsNotSerialized) {
  150. TBuffer result;
  151. auto subGroup = MakeIntrusive<TDynamicCounters>(TCountableBase::EVisibility::Private);
  152. subGroup->GetCounter("hello");
  153. Data.RegisterSubgroup("foo", "bar", subGroup);
  154. {
  155. TBufferOutput out(result);
  156. auto encoder = CreateEncoder(&out, EFormat::SPACK);
  157. Data.Accept(TString(), TString(), *encoder);
  158. }
  159. NProto::TSingleSamplesList samples;
  160. {
  161. auto e = EncoderProtobuf(&samples);
  162. TBufferInput in(result);
  163. DecodeSpackV1(&in, e.Get());
  164. }
  165. AssertResult(samples);
  166. }
  167. Y_UNIT_TEST(PrivateCounterIsNotSerialized) {
  168. TBuffer result;
  169. Data.GetCounter("foo", false, TCountableBase::EVisibility::Private);
  170. {
  171. TBufferOutput out(result);
  172. auto encoder = CreateEncoder(&out, EFormat::SPACK);
  173. Data.Accept(TString(), TString(), *encoder);
  174. }
  175. NProto::TSingleSamplesList samples;
  176. {
  177. auto e = EncoderProtobuf(&samples);
  178. TBufferInput in(result);
  179. DecodeSpackV1(&in, e.Get());
  180. }
  181. AssertResult(samples);
  182. }
  183. Y_UNIT_TEST(ToJson) {
  184. TString result = ToJson(Data);
  185. NProto::TSingleSamplesList samples;
  186. {
  187. auto e = EncoderProtobuf(&samples);
  188. DecodeJson(result, e.Get());
  189. }
  190. AssertResult(samples);
  191. }
  192. }
  193. }