histogram_percentile_counter.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2017 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 RTC_BASE_NUMERICS_HISTOGRAM_PERCENTILE_COUNTER_H_
  11. #define RTC_BASE_NUMERICS_HISTOGRAM_PERCENTILE_COUNTER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <map>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. namespace rtc {
  18. // Calculates percentiles on the stream of data. Use |Add| methods to add new
  19. // values. Use |GetPercentile| to get percentile of the currently added values.
  20. class HistogramPercentileCounter {
  21. public:
  22. // Values below |long_tail_boundary| are stored as the histogram in an array.
  23. // Values above - in a map.
  24. explicit HistogramPercentileCounter(uint32_t long_tail_boundary);
  25. ~HistogramPercentileCounter();
  26. void Add(uint32_t value);
  27. void Add(uint32_t value, size_t count);
  28. void Add(const HistogramPercentileCounter& other);
  29. // Argument should be from 0 to 1.
  30. absl::optional<uint32_t> GetPercentile(float fraction);
  31. private:
  32. std::vector<size_t> histogram_low_;
  33. std::map<uint32_t, size_t> histogram_high_;
  34. const uint32_t long_tail_boundary_;
  35. size_t total_elements_;
  36. size_t total_elements_low_;
  37. };
  38. } // namespace rtc
  39. #endif // RTC_BASE_NUMERICS_HISTOGRAM_PERCENTILE_COUNTER_H_