histogram_collector.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #include "histogram_snapshot.h"
  3. namespace NMonitoring {
  4. ///////////////////////////////////////////////////////////////////////////
  5. // IHistogramCollector
  6. ///////////////////////////////////////////////////////////////////////////
  7. class IHistogramCollector {
  8. public:
  9. virtual ~IHistogramCollector() = default;
  10. /**
  11. * Store {@code count} times given {@code value} in this collector.
  12. */
  13. virtual void Collect(double value, ui64 count) = 0;
  14. /**
  15. * Store given {@code value} in this collector.
  16. */
  17. void Collect(double value) {
  18. Collect(value, 1);
  19. }
  20. /**
  21. * Add counts from snapshot into this collector
  22. */
  23. void Collect(const IHistogramSnapshot& snapshot) {
  24. for (ui32 i = 0; i < snapshot.Count(); i++) {
  25. Collect(snapshot.UpperBound(i), snapshot.Value(i));
  26. }
  27. }
  28. /**
  29. * Reset collector values
  30. */
  31. virtual void Reset() = 0;
  32. /**
  33. * @return snapshot of the state of this collector.
  34. */
  35. virtual IHistogramSnapshotPtr Snapshot() const = 0;
  36. };
  37. using IHistogramCollectorPtr = THolder<IHistogramCollector>;
  38. ///////////////////////////////////////////////////////////////////////////
  39. // free functions
  40. ///////////////////////////////////////////////////////////////////////////
  41. /**
  42. * <p>Creates histogram collector for a set of buckets with arbitrary
  43. * bounds.</p>
  44. *
  45. * <p>Defines {@code bounds.size() + 1} buckets with these boundaries for
  46. * bucket i:</p>
  47. * <ul>
  48. * <li>Upper bound (0 <= i < N-1): {@code bounds[i]}</li>
  49. * <li>Lower bound (1 <= i < N): {@code bounds[i - 1]}</li>
  50. * </ul>
  51. *
  52. * <p>For example, if the list of boundaries is:</p>
  53. * <pre>0, 1, 2, 5, 10, 20</pre>
  54. *
  55. * <p>then there are five finite buckets with the following ranges:</p>
  56. * <pre>(-INF, 0], (0, 1], (1, 2], (2, 5], (5, 10], (10, 20], (20, +INF)</pre>
  57. *
  58. * @param bounds array of upper bounds for buckets. Values must be sorted.
  59. */
  60. IHistogramCollectorPtr ExplicitHistogram(TBucketBounds bounds);
  61. /**
  62. * <p>Creates histogram collector for a sequence of buckets that have a
  63. * width proportional to the value of the lower bound.</p>
  64. *
  65. * <p>Defines {@code bucketsCount} buckets with these boundaries for bucket i:</p>
  66. * <ul>
  67. * <li>Upper bound (0 <= i < N-1): {@code scale * (base ^ i)}</li>
  68. * <li>Lower bound (1 <= i < N): {@code scale * (base ^ (i - 1))}</li>
  69. * </ul>
  70. *
  71. * <p>For example, if {@code bucketsCount=6}, {@code base=2}, and {@code scale=3},
  72. * then the bucket ranges are as follows:</p>
  73. *
  74. * <pre>(-INF, 3], (3, 6], (6, 12], (12, 24], (24, 48], (48, +INF)</pre>
  75. *
  76. * @param bucketsCount the total number of buckets. The value must be >= 2.
  77. * @param base the exponential growth factor for the buckets width.
  78. * The value must be >= 1.0.
  79. * @param scale the linear scale for the buckets. The value must be >= 1.0.
  80. */
  81. IHistogramCollectorPtr ExponentialHistogram(
  82. ui32 bucketsCount, double base, double scale = 1.0);
  83. /**
  84. * <p>Creates histogram collector for a sequence of buckets that all have
  85. * the same width (except overflow and underflow).</p>
  86. *
  87. * <p>Defines {@code bucketsCount} buckets with these boundaries for bucket i:</p>
  88. * <ul>
  89. * <li>Upper bound (0 <= i < N-1): {@code startValue + bucketWidth * i}</li>
  90. * <li>Lower bound (1 <= i < N): {@code startValue + bucketWidth * (i - 1)}</li>
  91. * </ul>
  92. *
  93. * <p>For example, if {@code bucketsCount=6}, {@code startValue=5}, and
  94. * {@code bucketWidth=15}, then the bucket ranges are as follows:</p>
  95. *
  96. * <pre>(-INF, 5], (5, 20], (20, 35], (35, 50], (50, 65], (65, +INF)</pre>
  97. *
  98. * @param bucketsCount the total number of buckets. The value must be >= 2.
  99. * @param startValue the upper boundary of the first bucket.
  100. * @param bucketWidth the difference between the upper and lower bounds for
  101. * each bucket. The value must be >= 1.
  102. */
  103. IHistogramCollectorPtr LinearHistogram(
  104. ui32 bucketsCount, TBucketBound startValue, TBucketBound bucketWidth);
  105. } // namespace NMonitoring