histogram_test.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. #include <cmath>
  7. #include "monitoring/histogram.h"
  8. #include "monitoring/histogram_windowing.h"
  9. #include "test_util/testharness.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. class HistogramTest : public testing::Test {};
  12. namespace {
  13. const double kIota = 0.1;
  14. const HistogramBucketMapper bucketMapper;
  15. Env* env = Env::Default();
  16. }
  17. void PopulateHistogram(Histogram& histogram,
  18. uint64_t low, uint64_t high, uint64_t loop = 1) {
  19. for (; loop > 0; loop--) {
  20. for (uint64_t i = low; i <= high; i++) {
  21. histogram.Add(i);
  22. }
  23. }
  24. }
  25. void BasicOperation(Histogram& histogram) {
  26. PopulateHistogram(histogram, 1, 110, 10); // fill up to bucket [70, 110)
  27. HistogramData data;
  28. histogram.Data(&data);
  29. ASSERT_LE(fabs(histogram.Percentile(100.0) - 110.0), kIota);
  30. ASSERT_LE(fabs(data.percentile99 - 108.9), kIota); // 99 * 110 / 100
  31. ASSERT_LE(fabs(data.percentile95 - 104.5), kIota); // 95 * 110 / 100
  32. ASSERT_LE(fabs(data.median - 55.0), kIota); // 50 * 110 / 100
  33. ASSERT_EQ(data.average, 55.5); // (1 + 110) / 2
  34. }
  35. void MergeHistogram(Histogram& histogram, Histogram& other) {
  36. PopulateHistogram(histogram, 1, 100);
  37. PopulateHistogram(other, 101, 250);
  38. histogram.Merge(other);
  39. HistogramData data;
  40. histogram.Data(&data);
  41. ASSERT_LE(fabs(histogram.Percentile(100.0) - 250.0), kIota);
  42. ASSERT_LE(fabs(data.percentile99 - 247.5), kIota); // 99 * 250 / 100
  43. ASSERT_LE(fabs(data.percentile95 - 237.5), kIota); // 95 * 250 / 100
  44. ASSERT_LE(fabs(data.median - 125.0), kIota); // 50 * 250 / 100
  45. ASSERT_EQ(data.average, 125.5); // (1 + 250) / 2
  46. }
  47. void EmptyHistogram(Histogram& histogram) {
  48. ASSERT_EQ(histogram.min(), bucketMapper.LastValue());
  49. ASSERT_EQ(histogram.max(), 0);
  50. ASSERT_EQ(histogram.num(), 0);
  51. ASSERT_EQ(histogram.Median(), 0.0);
  52. ASSERT_EQ(histogram.Percentile(85.0), 0.0);
  53. ASSERT_EQ(histogram.Average(), 0.0);
  54. ASSERT_EQ(histogram.StandardDeviation(), 0.0);
  55. }
  56. void ClearHistogram(Histogram& histogram) {
  57. for (uint64_t i = 1; i <= 100; i++) {
  58. histogram.Add(i);
  59. }
  60. histogram.Clear();
  61. ASSERT_TRUE(histogram.Empty());
  62. ASSERT_EQ(histogram.Median(), 0);
  63. ASSERT_EQ(histogram.Percentile(85.0), 0);
  64. ASSERT_EQ(histogram.Average(), 0);
  65. }
  66. TEST_F(HistogramTest, BasicOperation) {
  67. HistogramImpl histogram;
  68. BasicOperation(histogram);
  69. HistogramWindowingImpl histogramWindowing;
  70. BasicOperation(histogramWindowing);
  71. }
  72. TEST_F(HistogramTest, BoundaryValue) {
  73. HistogramImpl histogram;
  74. // - both should be in [0, 1] bucket because we place values on bucket
  75. // boundaries in the lower bucket.
  76. // - all points are in [0, 1] bucket, so p50 will be 0.5
  77. // - the test cannot be written with a single point since histogram won't
  78. // report percentiles lower than the min or greater than the max.
  79. histogram.Add(0);
  80. histogram.Add(1);
  81. ASSERT_LE(fabs(histogram.Percentile(50.0) - 0.5), kIota);
  82. }
  83. TEST_F(HistogramTest, MergeHistogram) {
  84. HistogramImpl histogram;
  85. HistogramImpl other;
  86. MergeHistogram(histogram, other);
  87. HistogramWindowingImpl histogramWindowing;
  88. HistogramWindowingImpl otherWindowing;
  89. MergeHistogram(histogramWindowing, otherWindowing);
  90. }
  91. TEST_F(HistogramTest, EmptyHistogram) {
  92. HistogramImpl histogram;
  93. EmptyHistogram(histogram);
  94. HistogramWindowingImpl histogramWindowing;
  95. EmptyHistogram(histogramWindowing);
  96. }
  97. TEST_F(HistogramTest, ClearHistogram) {
  98. HistogramImpl histogram;
  99. ClearHistogram(histogram);
  100. HistogramWindowingImpl histogramWindowing;
  101. ClearHistogram(histogramWindowing);
  102. }
  103. TEST_F(HistogramTest, HistogramWindowingExpire) {
  104. uint64_t num_windows = 3;
  105. int micros_per_window = 1000000;
  106. uint64_t min_num_per_window = 0;
  107. HistogramWindowingImpl
  108. histogramWindowing(num_windows, micros_per_window, min_num_per_window);
  109. PopulateHistogram(histogramWindowing, 1, 1, 100);
  110. env->SleepForMicroseconds(micros_per_window);
  111. ASSERT_EQ(histogramWindowing.num(), 100);
  112. ASSERT_EQ(histogramWindowing.min(), 1);
  113. ASSERT_EQ(histogramWindowing.max(), 1);
  114. ASSERT_EQ(histogramWindowing.Average(), 1);
  115. PopulateHistogram(histogramWindowing, 2, 2, 100);
  116. env->SleepForMicroseconds(micros_per_window);
  117. ASSERT_EQ(histogramWindowing.num(), 200);
  118. ASSERT_EQ(histogramWindowing.min(), 1);
  119. ASSERT_EQ(histogramWindowing.max(), 2);
  120. ASSERT_EQ(histogramWindowing.Average(), 1.5);
  121. PopulateHistogram(histogramWindowing, 3, 3, 100);
  122. env->SleepForMicroseconds(micros_per_window);
  123. ASSERT_EQ(histogramWindowing.num(), 300);
  124. ASSERT_EQ(histogramWindowing.min(), 1);
  125. ASSERT_EQ(histogramWindowing.max(), 3);
  126. ASSERT_EQ(histogramWindowing.Average(), 2.0);
  127. // dropping oldest window with value 1, remaining 2 ~ 4
  128. PopulateHistogram(histogramWindowing, 4, 4, 100);
  129. env->SleepForMicroseconds(micros_per_window);
  130. ASSERT_EQ(histogramWindowing.num(), 300);
  131. ASSERT_EQ(histogramWindowing.min(), 2);
  132. ASSERT_EQ(histogramWindowing.max(), 4);
  133. ASSERT_EQ(histogramWindowing.Average(), 3.0);
  134. // dropping oldest window with value 2, remaining 3 ~ 5
  135. PopulateHistogram(histogramWindowing, 5, 5, 100);
  136. env->SleepForMicroseconds(micros_per_window);
  137. ASSERT_EQ(histogramWindowing.num(), 300);
  138. ASSERT_EQ(histogramWindowing.min(), 3);
  139. ASSERT_EQ(histogramWindowing.max(), 5);
  140. ASSERT_EQ(histogramWindowing.Average(), 4.0);
  141. }
  142. TEST_F(HistogramTest, HistogramWindowingMerge) {
  143. uint64_t num_windows = 3;
  144. int micros_per_window = 1000000;
  145. uint64_t min_num_per_window = 0;
  146. HistogramWindowingImpl
  147. histogramWindowing(num_windows, micros_per_window, min_num_per_window);
  148. HistogramWindowingImpl
  149. otherWindowing(num_windows, micros_per_window, min_num_per_window);
  150. PopulateHistogram(histogramWindowing, 1, 1, 100);
  151. PopulateHistogram(otherWindowing, 1, 1, 100);
  152. env->SleepForMicroseconds(micros_per_window);
  153. PopulateHistogram(histogramWindowing, 2, 2, 100);
  154. PopulateHistogram(otherWindowing, 2, 2, 100);
  155. env->SleepForMicroseconds(micros_per_window);
  156. PopulateHistogram(histogramWindowing, 3, 3, 100);
  157. PopulateHistogram(otherWindowing, 3, 3, 100);
  158. env->SleepForMicroseconds(micros_per_window);
  159. histogramWindowing.Merge(otherWindowing);
  160. ASSERT_EQ(histogramWindowing.num(), 600);
  161. ASSERT_EQ(histogramWindowing.min(), 1);
  162. ASSERT_EQ(histogramWindowing.max(), 3);
  163. ASSERT_EQ(histogramWindowing.Average(), 2.0);
  164. // dropping oldest window with value 1, remaining 2 ~ 4
  165. PopulateHistogram(histogramWindowing, 4, 4, 100);
  166. env->SleepForMicroseconds(micros_per_window);
  167. ASSERT_EQ(histogramWindowing.num(), 500);
  168. ASSERT_EQ(histogramWindowing.min(), 2);
  169. ASSERT_EQ(histogramWindowing.max(), 4);
  170. // dropping oldest window with value 2, remaining 3 ~ 5
  171. PopulateHistogram(histogramWindowing, 5, 5, 100);
  172. env->SleepForMicroseconds(micros_per_window);
  173. ASSERT_EQ(histogramWindowing.num(), 400);
  174. ASSERT_EQ(histogramWindowing.min(), 3);
  175. ASSERT_EQ(histogramWindowing.max(), 5);
  176. }
  177. } // namespace ROCKSDB_NAMESPACE
  178. int main(int argc, char** argv) {
  179. ::testing::InitGoogleTest(&argc, argv);
  180. return RUN_ALL_TESTS();
  181. }