blob: c262d48be9de25fab95cef190a93fdb4deaae076 [file] [log] [blame]
Artem Titove4ed6ea2019-01-11 11:02:19 +01001/*
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 Titove4ed6ea2019-01-11 11:02:19 +010013#include <cmath>
14
Steve Anton2acd1632019-03-25 13:48:30 -070015#include "absl/algorithm/container.h"
Artem Titov6fcdbc12019-09-11 11:45:40 +020016#include "rtc_base/time_utils.h"
Steve Anton2acd1632019-03-25 13:48:30 -070017
Artem Titove4ed6ea2019-01-11 11:02:19 +010018namespace webrtc {
19
20SamplesStatsCounter::SamplesStatsCounter() = default;
21SamplesStatsCounter::~SamplesStatsCounter() = default;
Artem Titove6f6a0c2019-02-07 12:14:35 +010022SamplesStatsCounter::SamplesStatsCounter(const SamplesStatsCounter&) = default;
23SamplesStatsCounter& SamplesStatsCounter::operator=(
24 const SamplesStatsCounter&) = default;
Artem Titove4ed6ea2019-01-11 11:02:19 +010025SamplesStatsCounter::SamplesStatsCounter(SamplesStatsCounter&&) = default;
Artem Titove6f6a0c2019-02-07 12:14:35 +010026SamplesStatsCounter& SamplesStatsCounter::operator=(SamplesStatsCounter&&) =
27 default;
Artem Titove4ed6ea2019-01-11 11:02:19 +010028
29void SamplesStatsCounter::AddSample(double value) {
Artem Titov6fcdbc12019-09-11 11:45:40 +020030 AddSample(StatsSample{value, Timestamp::us(rtc::TimeMicros())});
31}
32
33void SamplesStatsCounter::AddSample(StatsSample sample) {
34 stats_.AddSample(sample.value);
35 samples_.push_back(sample);
Artem Titove4ed6ea2019-01-11 11:02:19 +010036 sorted_ = false;
Artem Titove4ed6ea2019-01-11 11:02:19 +010037}
38
Sebastian Janssond93a0042019-04-09 15:38:07 +020039void SamplesStatsCounter::AddSamples(const SamplesStatsCounter& other) {
Yves Gerey890f62b2019-04-10 17:18:48 +020040 stats_.MergeStatistics(other.stats_);
41 samples_.insert(samples_.end(), other.samples_.begin(), other.samples_.end());
Sebastian Janssond93a0042019-04-09 15:38:07 +020042 sorted_ = false;
Sebastian Janssond93a0042019-04-09 15:38:07 +020043}
44
Artem Titove4ed6ea2019-01-11 11:02:19 +010045double SamplesStatsCounter::GetPercentile(double percentile) {
46 RTC_DCHECK(!IsEmpty());
47 RTC_CHECK_GE(percentile, 0);
48 RTC_CHECK_LE(percentile, 1);
49 if (!sorted_) {
Artem Titov6fcdbc12019-09-11 11:45:40 +020050 absl::c_sort(samples_, [](const StatsSample& a, const StatsSample& b) {
51 return a.value < b.value;
52 });
Artem Titove4ed6ea2019-01-11 11:02:19 +010053 sorted_ = true;
54 }
55 const double raw_rank = percentile * (samples_.size() - 1);
56 double int_part;
57 double fract_part = std::modf(raw_rank, &int_part);
58 size_t rank = static_cast<size_t>(int_part);
59 if (fract_part >= 1.0) {
60 // It can happen due to floating point calculation error.
61 rank++;
62 fract_part -= 1.0;
63 }
64
65 RTC_DCHECK_GE(rank, 0);
66 RTC_DCHECK_LT(rank, samples_.size());
67 RTC_DCHECK_GE(fract_part, 0);
68 RTC_DCHECK_LT(fract_part, 1);
69 RTC_DCHECK(rank + fract_part == raw_rank);
70
Artem Titov6fcdbc12019-09-11 11:45:40 +020071 const double low = samples_[rank].value;
72 const double high = samples_[std::min(rank + 1, samples_.size() - 1)].value;
Artem Titove4ed6ea2019-01-11 11:02:19 +010073 return low + fract_part * (high - low);
74}
75
Artem Titov2c5af4f2019-07-03 10:40:16 +020076SamplesStatsCounter operator*(const SamplesStatsCounter& counter,
77 double value) {
78 SamplesStatsCounter out;
Artem Titov6fcdbc12019-09-11 11:45:40 +020079 for (const auto& sample : counter.GetTimedSamples()) {
80 out.AddSample(
81 SamplesStatsCounter::StatsSample{sample.value * value, sample.time});
Artem Titov2c5af4f2019-07-03 10:40:16 +020082 }
83 return out;
84}
85
86SamplesStatsCounter operator/(const SamplesStatsCounter& counter,
87 double value) {
88 SamplesStatsCounter out;
Artem Titov6fcdbc12019-09-11 11:45:40 +020089 for (const auto& sample : counter.GetTimedSamples()) {
90 out.AddSample(
91 SamplesStatsCounter::StatsSample{sample.value / value, sample.time});
Artem Titov2c5af4f2019-07-03 10:40:16 +020092 }
93 return out;
94}
95
Artem Titove4ed6ea2019-01-11 11:02:19 +010096} // namespace webrtc