percentile_ut.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "percentile.h"
  2. #include "percentile_lg.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/datetime/cputimer.h>
  5. using namespace NMonitoring;
  6. Y_UNIT_TEST_SUITE(PercentileTest) {
  7. template<size_t A, size_t B, size_t B_BEGIN>
  8. void PrintSizeAndLimit() {
  9. using TPerc = TPercentileTrackerLg<A, B, 15>;
  10. Cout << "TPercentileTrackerLg<" << A << ", " << B << ", 15>"
  11. << "; sizeof# " << LeftPad(HumanReadableSize(sizeof(TPerc), SF_BYTES), 7)
  12. << "; max_granularity# " << LeftPad(HumanReadableSize(TPerc::MAX_GRANULARITY, SF_QUANTITY), 5)
  13. << "; limit# " << LeftPad(HumanReadableSize(TPerc::TRACKER_LIMIT , SF_QUANTITY), 5) << Endl;
  14. if constexpr (B > 1) {
  15. PrintSizeAndLimit<A, B - 1, B_BEGIN>();
  16. } else if constexpr (A > 1) {
  17. Cout << Endl;
  18. PrintSizeAndLimit<A - 1, B_BEGIN, B_BEGIN>();
  19. }
  20. }
  21. Y_UNIT_TEST(PrintTrackerLgSizeAndLimits) {
  22. PrintSizeAndLimit<10, 5, 5>();
  23. }
  24. template<class T>
  25. void RunPerf() {
  26. TTimer t(TypeName<T>() + "\n");
  27. T tracker;
  28. for (size_t i = 0; i < 1000000; ++i) {
  29. for (size_t i = 0; i < 10; ++i) {
  30. tracker.Increment(i * 6451);
  31. }
  32. tracker.Update();
  33. }
  34. }
  35. Y_UNIT_TEST(TrackerPerf) {
  36. RunPerf<TPercentileTracker<4, 512, 15>>();
  37. RunPerf<TPercentileTrackerLg<4, 3, 15>>();
  38. }
  39. Y_UNIT_TEST(TrackerLimitTest) {
  40. {
  41. using TPerc = TPercentileTrackerLg<1, 0, 1>;
  42. TPerc tracker;
  43. tracker.Increment(Max<size_t>());
  44. UNIT_ASSERT_EQUAL(TPerc::TRACKER_LIMIT, tracker.GetPercentile(1.0));
  45. }
  46. {
  47. using TPerc = TPercentileTrackerLg<1, 1, 1>;
  48. TPerc tracker;
  49. tracker.Increment(Max<size_t>());
  50. UNIT_ASSERT_EQUAL(TPerc::TRACKER_LIMIT, tracker.GetPercentile(1.0));
  51. }
  52. {
  53. using TPerc = TPercentileTrackerLg<1, 5, 1>;
  54. TPerc tracker;
  55. tracker.Increment(Max<size_t>());
  56. UNIT_ASSERT_EQUAL(TPerc::TRACKER_LIMIT, tracker.GetPercentile(1.0));
  57. }
  58. {
  59. using TPerc = TPercentileTrackerLg<2, 1, 1>;
  60. TPerc tracker;
  61. tracker.Increment(Max<size_t>());
  62. UNIT_ASSERT_EQUAL(TPerc::TRACKER_LIMIT, tracker.GetPercentile(1.0));
  63. }
  64. {
  65. using TPerc = TPercentileTrackerLg<5, 4, 1>;
  66. TPerc tracker;
  67. tracker.Increment(Max<size_t>());
  68. UNIT_ASSERT_EQUAL(TPerc::TRACKER_LIMIT, tracker.GetPercentile(1.0));
  69. }
  70. }
  71. Y_UNIT_TEST(BucketIdxIfvsBucketIdxBinarySearch) {
  72. for (size_t var = 0; var < 5; var++) {
  73. if (var == 0) {
  74. TPercentileTrackerLg<3, 2, 15> tracker;
  75. for (size_t i = 0; i < 3000000; i += 1) {
  76. size_t num1 = tracker.BucketIdxMostSignificantBit(i);
  77. size_t num2 = tracker.BucketIdxBinarySearch(i);
  78. UNIT_ASSERT_EQUAL(num1, num2);
  79. }
  80. } else if (var == 1) {
  81. TPercentileTrackerLg<4, 4, 15> tracker;
  82. for (size_t i = 0; i < 3000000; i += 1) {
  83. size_t num1 = tracker.BucketIdxMostSignificantBit(i);
  84. size_t num2 = tracker.BucketIdxBinarySearch(i);
  85. UNIT_ASSERT_EQUAL(num1, num2);
  86. }
  87. } else if (var == 2) {
  88. TPercentileTrackerLg<5, 3, 15> tracker;
  89. for (size_t i = 0; i < 3000000; i += 1) {
  90. size_t num1 = tracker.BucketIdxMostSignificantBit(i);
  91. size_t num2 = tracker.BucketIdxBinarySearch(i);
  92. size_t num3 = tracker.BucketIdxIf(i);
  93. UNIT_ASSERT_EQUAL(num1, num2);
  94. UNIT_ASSERT_EQUAL(num2, num3);
  95. }
  96. } else if (var == 3) {
  97. TPercentileTrackerLg<5, 4, 15> tracker;
  98. for (size_t i = 0; i < 3000000; i += 1) {
  99. size_t num1 = tracker.BucketIdxMostSignificantBit(i);
  100. size_t num2 = tracker.BucketIdxBinarySearch(i);
  101. size_t num3 = tracker.BucketIdxIf(i);
  102. UNIT_ASSERT_EQUAL(num1, num2);
  103. UNIT_ASSERT_EQUAL(num2, num3);
  104. }
  105. } else if (var == 4) {
  106. TPercentileTrackerLg<6, 5, 15> tracker;
  107. for (size_t i = 0; i < 3000000; i += 1) {
  108. size_t num1 = tracker.BucketIdxMostSignificantBit(i);
  109. size_t num2 = tracker.BucketIdxBinarySearch(i);
  110. UNIT_ASSERT_EQUAL(num1, num2);
  111. }
  112. for (size_t i = 0; i < 400000000000ul; i += 1303) {
  113. size_t num1 = tracker.BucketIdxMostSignificantBit(i);
  114. size_t num2 = tracker.BucketIdxBinarySearch(i);
  115. UNIT_ASSERT_EQUAL(num1, num2);
  116. }
  117. }
  118. }
  119. }
  120. Y_UNIT_TEST(DifferentPercentiles) {
  121. TPercentileTrackerLg<5, 4, 15> tracker;
  122. TVector<size_t> values({0, 115, 1216, 15, 3234567, 1234567, 216546, 263421, 751654, 96, 224, 223, 225});
  123. TVector<size_t> percentiles50({0, 0, 116, 15, 116, 116, 1216, 1216, 217056, 1216, 1216, 224, 232});
  124. TVector<size_t> percentiles75({0, 116, 116, 116, 1216, 1245152, 217056, 270304, 753632, 753632,
  125. 270304, 270304, 270304});
  126. TVector<size_t> percentiles90({ 0, 116, 1216, 1216, 2064352, 1245152, 1245152, 1245152, 1245152,
  127. 1245152, 1245152, 1245152, 1245152});
  128. TVector<size_t> percentiles100({ 0, 116, 1216, 1216, 2064352, 2064352, 2064352, 2064352, 2064352,
  129. 2064352, 2064352, 2064352, 2064352 });
  130. for (size_t i = 0; i < values.size(); ++i) {
  131. tracker.Increment(values[i]);
  132. UNIT_ASSERT_EQUAL(tracker.GetPercentile(0.5), percentiles50[i]);
  133. UNIT_ASSERT_EQUAL(tracker.GetPercentile(0.75), percentiles75[i]);
  134. UNIT_ASSERT_EQUAL(tracker.GetPercentile(0.90), percentiles90[i]);
  135. UNIT_ASSERT_EQUAL(tracker.GetPercentile(1.0), percentiles100[i]);
  136. }
  137. }
  138. }