histogram_iter_ut.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "histogram_iter.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NHdr;
  4. Y_UNIT_TEST_SUITE(THistogramIterTest) {
  5. Y_UNIT_TEST(RecordedValues) {
  6. THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
  7. UNIT_ASSERT(h.RecordValues(1000, 1000));
  8. UNIT_ASSERT(h.RecordValue(1000 * 1000));
  9. int index = 0;
  10. TRecordedValuesIterator it(h);
  11. while (it.Next()) {
  12. i64 countInBucket = it.GetCount();
  13. i64 countInStep = it.GetCountAddedInThisIterationStep();
  14. if (index == 0) {
  15. UNIT_ASSERT_EQUAL(countInBucket, 1000);
  16. UNIT_ASSERT_EQUAL(countInStep, 1000);
  17. } else if (index == 1) {
  18. UNIT_ASSERT_EQUAL(countInBucket, 1);
  19. UNIT_ASSERT_EQUAL(countInStep, 1);
  20. } else {
  21. UNIT_FAIL("unexpected index value: " << index);
  22. }
  23. index++;
  24. }
  25. UNIT_ASSERT_EQUAL(index, 2);
  26. }
  27. Y_UNIT_TEST(CorrectedRecordedValues) {
  28. THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
  29. UNIT_ASSERT(h.RecordValuesWithExpectedInterval(1000, 1000, 1000));
  30. UNIT_ASSERT(h.RecordValueWithExpectedInterval(1000 * 1000, 1000));
  31. int index = 0;
  32. i64 totalCount = 0;
  33. TRecordedValuesIterator it(h);
  34. while (it.Next()) {
  35. i64 countInBucket = it.GetCount();
  36. i64 countInStep = it.GetCountAddedInThisIterationStep();
  37. if (index == 0) {
  38. UNIT_ASSERT_EQUAL(countInBucket, 1001);
  39. UNIT_ASSERT_EQUAL(countInStep, 1001);
  40. } else {
  41. UNIT_ASSERT(countInBucket >= 1);
  42. UNIT_ASSERT(countInStep >= 1);
  43. }
  44. index++;
  45. totalCount += countInStep;
  46. }
  47. UNIT_ASSERT_EQUAL(index, 1000);
  48. UNIT_ASSERT_EQUAL(totalCount, 2000);
  49. }
  50. Y_UNIT_TEST(LinearValues) {
  51. THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
  52. UNIT_ASSERT(h.RecordValues(1000, 1000));
  53. UNIT_ASSERT(h.RecordValue(1000 * 1000));
  54. int index = 0;
  55. TLinearIterator it(h, 1000);
  56. while (it.Next()) {
  57. i64 countInBucket = it.GetCount();
  58. i64 countInStep = it.GetCountAddedInThisIterationStep();
  59. if (index == 0) {
  60. UNIT_ASSERT_EQUAL(countInBucket, 1000);
  61. UNIT_ASSERT_EQUAL(countInStep, 1000);
  62. } else if (index == 999) {
  63. UNIT_ASSERT_EQUAL(countInBucket, 1);
  64. UNIT_ASSERT_EQUAL(countInStep, 1);
  65. } else {
  66. UNIT_ASSERT_EQUAL(countInBucket, 0);
  67. UNIT_ASSERT_EQUAL(countInStep, 0);
  68. }
  69. index++;
  70. }
  71. UNIT_ASSERT_EQUAL(index, 1000);
  72. }
  73. Y_UNIT_TEST(CorrectLinearValues) {
  74. THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
  75. UNIT_ASSERT(h.RecordValuesWithExpectedInterval(1000, 1000, 1000));
  76. UNIT_ASSERT(h.RecordValueWithExpectedInterval(1000 * 1000, 1000));
  77. int index = 0;
  78. i64 totalCount = 0;
  79. TLinearIterator it(h, 1000);
  80. while (it.Next()) {
  81. i64 countInBucket = it.GetCount();
  82. i64 countInStep = it.GetCountAddedInThisIterationStep();
  83. if (index == 0) {
  84. UNIT_ASSERT_EQUAL(countInBucket, 1001);
  85. UNIT_ASSERT_EQUAL(countInStep, 1001);
  86. } else {
  87. UNIT_ASSERT_EQUAL(countInBucket, 1);
  88. UNIT_ASSERT_EQUAL(countInStep, 1);
  89. }
  90. index++;
  91. totalCount += countInStep;
  92. }
  93. UNIT_ASSERT_EQUAL(index, 1000);
  94. UNIT_ASSERT_EQUAL(totalCount, 2000);
  95. }
  96. Y_UNIT_TEST(LogarithmicValues) {
  97. THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
  98. UNIT_ASSERT(h.RecordValues(1000, 1000));
  99. UNIT_ASSERT(h.RecordValue(1000 * 1000));
  100. int index = 0;
  101. i64 expectedValue = 1000;
  102. TLogarithmicIterator it(h, 1000, 2.0);
  103. while (it.Next()) {
  104. i64 value = it.GetValue();
  105. i64 countInBucket = it.GetCount();
  106. i64 countInStep = it.GetCountAddedInThisIterationStep();
  107. UNIT_ASSERT_EQUAL(value, expectedValue);
  108. if (index == 0) {
  109. UNIT_ASSERT_EQUAL(countInBucket, 1000);
  110. UNIT_ASSERT_EQUAL(countInStep, 1000);
  111. } else if (index == 10) {
  112. UNIT_ASSERT_EQUAL(countInBucket, 0);
  113. UNIT_ASSERT_EQUAL(countInStep, 1);
  114. } else {
  115. UNIT_ASSERT_EQUAL(countInBucket, 0);
  116. UNIT_ASSERT_EQUAL(countInStep, 0);
  117. }
  118. index++;
  119. expectedValue *= 2;
  120. }
  121. UNIT_ASSERT_EQUAL(index, 11);
  122. }
  123. Y_UNIT_TEST(CorrectedLogarithmicValues) {
  124. THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
  125. UNIT_ASSERT(h.RecordValuesWithExpectedInterval(1000, 1000, 1000));
  126. UNIT_ASSERT(h.RecordValueWithExpectedInterval(1000 * 1000, 1000));
  127. int index = 0;
  128. i64 totalCount = 0;
  129. i64 expectedValue = 1000;
  130. TLogarithmicIterator it(h, 1000, 2.0);
  131. while (it.Next()) {
  132. i64 value = it.GetValue();
  133. i64 countInBucket = it.GetCount();
  134. i64 countInStep = it.GetCountAddedInThisIterationStep();
  135. UNIT_ASSERT_EQUAL(value, expectedValue);
  136. if (index == 0) {
  137. UNIT_ASSERT_EQUAL(countInBucket, 1001);
  138. UNIT_ASSERT_EQUAL(countInStep, 1001);
  139. }
  140. index++;
  141. totalCount += countInStep;
  142. expectedValue *= 2;
  143. }
  144. UNIT_ASSERT_EQUAL(index, 11);
  145. UNIT_ASSERT_EQUAL(totalCount, 2000);
  146. }
  147. Y_UNIT_TEST(LinearIterBucketsCorrectly) {
  148. THistogram h(255, 2);
  149. UNIT_ASSERT(h.RecordValue(193));
  150. UNIT_ASSERT(h.RecordValue(255));
  151. UNIT_ASSERT(h.RecordValue(0));
  152. UNIT_ASSERT(h.RecordValue(1));
  153. UNIT_ASSERT(h.RecordValue(64));
  154. UNIT_ASSERT(h.RecordValue(128));
  155. int index = 0;
  156. i64 totalCount = 0;
  157. TLinearIterator it(h, 64);
  158. while (it.Next()) {
  159. if (index == 0) {
  160. // change after iterator was created
  161. UNIT_ASSERT(h.RecordValue(2));
  162. }
  163. index++;
  164. totalCount += it.GetCountAddedInThisIterationStep();
  165. }
  166. UNIT_ASSERT_EQUAL(index, 4);
  167. UNIT_ASSERT_EQUAL(totalCount, 6);
  168. }
  169. }