Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 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 | |
| 11 | #include "rtc_base/numerics/samples_stats_counter.h" |
| 12 | |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 13 | #include <cmath> |
| 14 | |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame] | 15 | #include "absl/algorithm/container.h" |
| 16 | |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 17 | namespace webrtc { |
| 18 | |
| 19 | SamplesStatsCounter::SamplesStatsCounter() = default; |
| 20 | SamplesStatsCounter::~SamplesStatsCounter() = default; |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 21 | SamplesStatsCounter::SamplesStatsCounter(const SamplesStatsCounter&) = default; |
| 22 | SamplesStatsCounter& SamplesStatsCounter::operator=( |
| 23 | const SamplesStatsCounter&) = default; |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 24 | SamplesStatsCounter::SamplesStatsCounter(SamplesStatsCounter&&) = default; |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 25 | SamplesStatsCounter& SamplesStatsCounter::operator=(SamplesStatsCounter&&) = |
| 26 | default; |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 27 | |
| 28 | void SamplesStatsCounter::AddSample(double value) { |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 29 | stats_.AddSample(value); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 30 | samples_.push_back(value); |
| 31 | sorted_ = false; |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Sebastian Jansson | d93a004 | 2019-04-09 15:38:07 +0200 | [diff] [blame] | 34 | void SamplesStatsCounter::AddSamples(const SamplesStatsCounter& other) { |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 35 | stats_.MergeStatistics(other.stats_); |
| 36 | samples_.insert(samples_.end(), other.samples_.begin(), other.samples_.end()); |
Sebastian Jansson | d93a004 | 2019-04-09 15:38:07 +0200 | [diff] [blame] | 37 | sorted_ = false; |
Sebastian Jansson | d93a004 | 2019-04-09 15:38:07 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 40 | double SamplesStatsCounter::GetPercentile(double percentile) { |
| 41 | RTC_DCHECK(!IsEmpty()); |
| 42 | RTC_CHECK_GE(percentile, 0); |
| 43 | RTC_CHECK_LE(percentile, 1); |
| 44 | if (!sorted_) { |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame] | 45 | absl::c_sort(samples_); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 46 | sorted_ = true; |
| 47 | } |
| 48 | const double raw_rank = percentile * (samples_.size() - 1); |
| 49 | double int_part; |
| 50 | double fract_part = std::modf(raw_rank, &int_part); |
| 51 | size_t rank = static_cast<size_t>(int_part); |
| 52 | if (fract_part >= 1.0) { |
| 53 | // It can happen due to floating point calculation error. |
| 54 | rank++; |
| 55 | fract_part -= 1.0; |
| 56 | } |
| 57 | |
| 58 | RTC_DCHECK_GE(rank, 0); |
| 59 | RTC_DCHECK_LT(rank, samples_.size()); |
| 60 | RTC_DCHECK_GE(fract_part, 0); |
| 61 | RTC_DCHECK_LT(fract_part, 1); |
| 62 | RTC_DCHECK(rank + fract_part == raw_rank); |
| 63 | |
| 64 | const double low = samples_[rank]; |
| 65 | const double high = samples_[std::min(rank + 1, samples_.size() - 1)]; |
| 66 | return low + fract_part * (high - low); |
| 67 | } |
| 68 | |
Artem Titov | 2c5af4f | 2019-07-03 10:40:16 +0200 | [diff] [blame] | 69 | SamplesStatsCounter operator*(const SamplesStatsCounter& counter, |
| 70 | double value) { |
| 71 | SamplesStatsCounter out; |
| 72 | for (auto& sample : counter.GetSamples()) { |
| 73 | out.AddSample(sample * value); |
| 74 | } |
| 75 | return out; |
| 76 | } |
| 77 | |
| 78 | SamplesStatsCounter operator/(const SamplesStatsCounter& counter, |
| 79 | double value) { |
| 80 | SamplesStatsCounter out; |
| 81 | for (auto& sample : counter.GetSamples()) { |
| 82 | out.AddSample(sample / value); |
| 83 | } |
| 84 | return out; |
| 85 | } |
| 86 | |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 87 | } // namespace webrtc |