samples_stats_counter.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef API_NUMERICS_SAMPLES_STATS_COUNTER_H_
  11. #define API_NUMERICS_SAMPLES_STATS_COUNTER_H_
  12. #include <vector>
  13. #include "api/array_view.h"
  14. #include "api/units/timestamp.h"
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/numerics/running_statistics.h"
  17. namespace webrtc {
  18. // This class extends RunningStatistics by providing GetPercentile() method,
  19. // while slightly adapting the interface.
  20. class SamplesStatsCounter {
  21. public:
  22. struct StatsSample {
  23. double value;
  24. Timestamp time;
  25. };
  26. SamplesStatsCounter();
  27. ~SamplesStatsCounter();
  28. SamplesStatsCounter(const SamplesStatsCounter&);
  29. SamplesStatsCounter& operator=(const SamplesStatsCounter&);
  30. SamplesStatsCounter(SamplesStatsCounter&&);
  31. SamplesStatsCounter& operator=(SamplesStatsCounter&&);
  32. // Adds sample to the stats in amortized O(1) time.
  33. void AddSample(double value);
  34. void AddSample(StatsSample sample);
  35. // Adds samples from another counter.
  36. void AddSamples(const SamplesStatsCounter& other);
  37. // Returns if there are any values in O(1) time.
  38. bool IsEmpty() const { return samples_.empty(); }
  39. // Returns the amount of samples added into counter in O(1) time.
  40. int64_t NumSamples() const { return stats_.Size(); }
  41. // Returns min in O(1) time. This function may not be called if there are no
  42. // samples.
  43. double GetMin() const {
  44. RTC_DCHECK(!IsEmpty());
  45. return *stats_.GetMin();
  46. }
  47. // Returns max in O(1) time. This function may not be called if there are no
  48. // samples.
  49. double GetMax() const {
  50. RTC_DCHECK(!IsEmpty());
  51. return *stats_.GetMax();
  52. }
  53. // Returns average in O(1) time. This function may not be called if there are
  54. // no samples.
  55. double GetAverage() const {
  56. RTC_DCHECK(!IsEmpty());
  57. return *stats_.GetMean();
  58. }
  59. // Returns variance in O(1) time. This function may not be called if there are
  60. // no samples.
  61. double GetVariance() const {
  62. RTC_DCHECK(!IsEmpty());
  63. return *stats_.GetVariance();
  64. }
  65. // Returns standard deviation in O(1) time. This function may not be called if
  66. // there are no samples.
  67. double GetStandardDeviation() const {
  68. RTC_DCHECK(!IsEmpty());
  69. return *stats_.GetStandardDeviation();
  70. }
  71. // Returns percentile in O(nlogn) on first call and in O(1) after, if no
  72. // additions were done. This function may not be called if there are no
  73. // samples.
  74. //
  75. // |percentile| has to be in [0; 1]. 0 percentile is the min in the array and
  76. // 1 percentile is the max in the array.
  77. double GetPercentile(double percentile);
  78. // Returns array view with all samples added into counter. There are no
  79. // guarantees of order, so samples can be in different order comparing to in
  80. // which they were added into counter. Also return value will be invalidate
  81. // after call to any non const method.
  82. rtc::ArrayView<const StatsSample> GetTimedSamples() const { return samples_; }
  83. std::vector<double> GetSamples() const {
  84. std::vector<double> out;
  85. out.reserve(samples_.size());
  86. for (const auto& sample : samples_) {
  87. out.push_back(sample.value);
  88. }
  89. return out;
  90. }
  91. private:
  92. webrtc_impl::RunningStatistics<double> stats_;
  93. std::vector<StatsSample> samples_;
  94. bool sorted_ = false;
  95. };
  96. // Multiply all sample values on |value| and return new SamplesStatsCounter
  97. // with resulted samples. Doesn't change origin SamplesStatsCounter.
  98. SamplesStatsCounter operator*(const SamplesStatsCounter& counter, double value);
  99. inline SamplesStatsCounter operator*(double value,
  100. const SamplesStatsCounter& counter) {
  101. return counter * value;
  102. }
  103. // Divide all sample values on |value| and return new SamplesStatsCounter with
  104. // resulted samples. Doesn't change origin SamplesStatsCounter.
  105. SamplesStatsCounter operator/(const SamplesStatsCounter& counter, double value);
  106. } // namespace webrtc
  107. #endif // API_NUMERICS_SAMPLES_STATS_COUNTER_H_