blob: 5769fb0c16be5d9cc33ac06a4a76fbd72e20ac34 [file] [log] [blame]
Ilya Nikolaevskiyed23be92017-10-12 12:38:01 +02001/*
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
11#ifndef RTC_BASE_HISTOGRAM_PERCENTILE_COUNTER_H_
12#define RTC_BASE_HISTOGRAM_PERCENTILE_COUNTER_H_
13
14#include <stdint.h>
15#include <map>
16#include <vector>
17
18#include "api/optional.h"
19
20namespace rtc {
21// Calculates percentiles on the stream of data. Use |Add| methods to add new
22// values. Use |GetPercentile| to get percentile of the currently added values.
23class HistogramPercentileCounter {
24 public:
25 // Values below |long_tail_boundary| are stored as the histogram in an array.
26 // Values above - in a map.
27 explicit HistogramPercentileCounter(uint32_t long_tail_boundary);
28 ~HistogramPercentileCounter();
29 void Add(uint32_t value);
30 void Add(uint32_t value, size_t count);
31 void Add(const HistogramPercentileCounter& other);
32 // Argument should be from 0 to 1.
33 rtc::Optional<uint32_t> GetPercentile(float fraction);
34
35 private:
36 std::vector<size_t> histogram_low_;
37 std::map<uint32_t, size_t> histogram_high_;
38 const uint32_t long_tail_boundary_;
39 size_t total_elements_;
40 size_t total_elements_low_;
41};
42} // namespace rtc
43#endif // RTC_BASE_HISTOGRAM_PERCENTILE_COUNTER_H_