histogram_collector_ut.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "histogram_collector.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NMonitoring;
  4. Y_UNIT_TEST_SUITE(THistogramCollectorTest) {
  5. void CheckSnapshot(
  6. const IHistogramSnapshot& s,
  7. const TBucketBounds& bounds,
  8. const TBucketValues& values) {
  9. UNIT_ASSERT_VALUES_EQUAL(bounds.size(), values.size());
  10. UNIT_ASSERT_VALUES_EQUAL(bounds.size(), s.Count());
  11. double epsilon = std::numeric_limits<double>::epsilon();
  12. for (ui32 i = 0; i < s.Count(); i++) {
  13. UNIT_ASSERT_DOUBLES_EQUAL(bounds[i], s.UpperBound(i), epsilon);
  14. UNIT_ASSERT_VALUES_EQUAL(values[i], s.Value(i));
  15. }
  16. }
  17. Y_UNIT_TEST(Explicit) {
  18. auto histogram = ExplicitHistogram({0, 1, 2, 5, 10, 20});
  19. histogram->Collect(-2);
  20. histogram->Collect(-1);
  21. histogram->Collect(0);
  22. histogram->Collect(1);
  23. histogram->Collect(20);
  24. histogram->Collect(21);
  25. histogram->Collect(1000);
  26. TBucketBounds expectedBounds = {0, 1, 2, 5, 10, 20, Max<TBucketBound>()};
  27. TBucketValues expectedValues = {3, 1, 0, 0, 0, 1, 2};
  28. CheckSnapshot(*histogram->Snapshot(), expectedBounds, expectedValues);
  29. }
  30. Y_UNIT_TEST(ExplicitWithFloadBounds) {
  31. auto histogram = ExplicitHistogram({0.1, 0.5, 1, 1.7, 10});
  32. histogram->Collect(0.3, 2);
  33. histogram->Collect(0.01);
  34. histogram->Collect(0.9);
  35. histogram->Collect(0.6);
  36. histogram->Collect(1.1);
  37. histogram->Collect(0.7);
  38. histogram->Collect(2.71);
  39. TBucketBounds expectedBounds = {0.1, 0.5, 1, 1.7, 10, Max<TBucketBound>()};
  40. TBucketValues expectedValues = {1, 2, 3, 1, 1, 0};
  41. CheckSnapshot(*histogram->Snapshot(), expectedBounds, expectedValues);
  42. }
  43. Y_UNIT_TEST(Exponential) {
  44. auto histogram = ExponentialHistogram(6, 2.0, 3.0);
  45. histogram->Collect(-1);
  46. histogram->Collect(0);
  47. histogram->Collect(1);
  48. histogram->Collect(3);
  49. histogram->Collect(4);
  50. histogram->Collect(5);
  51. histogram->Collect(22);
  52. histogram->Collect(23);
  53. histogram->Collect(24);
  54. histogram->Collect(50);
  55. histogram->Collect(100);
  56. histogram->Collect(1000);
  57. TBucketBounds expectedBounds = {3, 6, 12, 24, 48, Max<TBucketBound>()};
  58. TBucketValues expectedValues = {4, 2, 0, 3, 0, 3};
  59. CheckSnapshot(*histogram->Snapshot(), expectedBounds, expectedValues);
  60. }
  61. Y_UNIT_TEST(Linear) {
  62. auto histogram = LinearHistogram(6, 5, 15);
  63. histogram->Collect(-1);
  64. histogram->Collect(0);
  65. histogram->Collect(1);
  66. histogram->Collect(4);
  67. histogram->Collect(5);
  68. histogram->Collect(6);
  69. histogram->Collect(64);
  70. histogram->Collect(65);
  71. histogram->Collect(66);
  72. histogram->Collect(100);
  73. histogram->Collect(1000);
  74. TBucketBounds expectedBounds = {5, 20, 35, 50, 65, Max<TBucketBound>()};
  75. TBucketValues expectedValues = {5, 1, 0, 0, 2, 3};
  76. CheckSnapshot(*histogram->Snapshot(), expectedBounds, expectedValues);
  77. }
  78. Y_UNIT_TEST(SnapshotOutput) {
  79. auto histogram = ExplicitHistogram({0, 1, 2, 5, 10, 20});
  80. histogram->Collect(-2);
  81. histogram->Collect(-1);
  82. histogram->Collect(0);
  83. histogram->Collect(1);
  84. histogram->Collect(20);
  85. histogram->Collect(21);
  86. histogram->Collect(1000);
  87. auto snapshot = histogram->Snapshot();
  88. TStringStream ss;
  89. ss << *snapshot;
  90. UNIT_ASSERT_STRINGS_EQUAL(
  91. "{0: 3, 1: 1, 2: 0, 5: 0, 10: 0, 20: 1, inf: 2}",
  92. ss.Str());
  93. }
  94. }