xref: /aosp_15_r20/external/webrtc/api/numerics/samples_stats_counter.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "api/numerics/samples_stats_counter.h"
12 
13 #include <algorithm>
14 #include <cmath>
15 
16 #include "absl/algorithm/container.h"
17 #include "rtc_base/time_utils.h"
18 
19 namespace webrtc {
20 
21 SamplesStatsCounter::SamplesStatsCounter() = default;
SamplesStatsCounter(size_t expected_samples_count)22 SamplesStatsCounter::SamplesStatsCounter(size_t expected_samples_count) {
23   samples_.reserve(expected_samples_count);
24 }
25 
26 SamplesStatsCounter::~SamplesStatsCounter() = default;
27 SamplesStatsCounter::SamplesStatsCounter(const SamplesStatsCounter&) = default;
28 SamplesStatsCounter& SamplesStatsCounter::operator=(
29     const SamplesStatsCounter&) = default;
30 SamplesStatsCounter::SamplesStatsCounter(SamplesStatsCounter&&) = default;
31 SamplesStatsCounter& SamplesStatsCounter::operator=(SamplesStatsCounter&&) =
32     default;
33 
AddSample(double value)34 void SamplesStatsCounter::AddSample(double value) {
35   AddSample(StatsSample{value, Timestamp::Micros(rtc::TimeMicros())});
36 }
37 
AddSample(StatsSample sample)38 void SamplesStatsCounter::AddSample(StatsSample sample) {
39   stats_.AddSample(sample.value);
40   samples_.push_back(sample);
41   sorted_ = false;
42 }
43 
AddSamples(const SamplesStatsCounter & other)44 void SamplesStatsCounter::AddSamples(const SamplesStatsCounter& other) {
45   stats_.MergeStatistics(other.stats_);
46   samples_.insert(samples_.end(), other.samples_.begin(), other.samples_.end());
47   sorted_ = false;
48 }
49 
GetPercentile(double percentile)50 double SamplesStatsCounter::GetPercentile(double percentile) {
51   RTC_DCHECK(!IsEmpty());
52   RTC_CHECK_GE(percentile, 0);
53   RTC_CHECK_LE(percentile, 1);
54   if (!sorted_) {
55     absl::c_sort(samples_, [](const StatsSample& a, const StatsSample& b) {
56       return a.value < b.value;
57     });
58     sorted_ = true;
59   }
60   const double raw_rank = percentile * (samples_.size() - 1);
61   double int_part;
62   double fract_part = std::modf(raw_rank, &int_part);
63   size_t rank = static_cast<size_t>(int_part);
64   if (fract_part >= 1.0) {
65     // It can happen due to floating point calculation error.
66     rank++;
67     fract_part -= 1.0;
68   }
69 
70   RTC_DCHECK_GE(rank, 0);
71   RTC_DCHECK_LT(rank, samples_.size());
72   RTC_DCHECK_GE(fract_part, 0);
73   RTC_DCHECK_LT(fract_part, 1);
74   RTC_DCHECK(rank + fract_part == raw_rank);
75 
76   const double low = samples_[rank].value;
77   const double high = samples_[std::min(rank + 1, samples_.size() - 1)].value;
78   return low + fract_part * (high - low);
79 }
80 
operator *(const SamplesStatsCounter & counter,double value)81 SamplesStatsCounter operator*(const SamplesStatsCounter& counter,
82                               double value) {
83   SamplesStatsCounter out;
84   for (const auto& sample : counter.GetTimedSamples()) {
85     out.AddSample(
86         SamplesStatsCounter::StatsSample{sample.value * value, sample.time});
87   }
88   return out;
89 }
90 
operator /(const SamplesStatsCounter & counter,double value)91 SamplesStatsCounter operator/(const SamplesStatsCounter& counter,
92                               double value) {
93   SamplesStatsCounter out;
94   for (const auto& sample : counter.GetTimedSamples()) {
95     out.AddSample(
96         SamplesStatsCounter::StatsSample{sample.value / value, sample.time});
97   }
98   return out;
99 }
100 
101 }  // namespace webrtc
102