blob: 4eb0cde299f7ffcddaaeb6a9fba4b42d9258d427 [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
Artem Titov9d777622020-09-18 18:23:08 +020011#include "api/numerics/samples_stats_counter.h"
Artem Titove4ed6ea2019-01-11 11:02:19 +010012
Artem Titov9d777622020-09-18 18:23:08 +020013#include <algorithm>
Artem Titove4ed6ea2019-01-11 11:02:19 +010014#include <cmath>
15
Steve Anton2acd1632019-03-25 13:48:30 -070016#include "absl/algorithm/container.h"
Artem Titov6fcdbc12019-09-11 11:45:40 +020017#include "rtc_base/time_utils.h"
Steve Anton2acd1632019-03-25 13:48:30 -070018
Artem Titove4ed6ea2019-01-11 11:02:19 +010019namespace webrtc {
20
21SamplesStatsCounter::SamplesStatsCounter() = default;
Artem Titov7fee2f72022-09-26 16:29:48 +020022SamplesStatsCounter::SamplesStatsCounter(size_t expected_samples_count) {
23 samples_.reserve(expected_samples_count);
24}
25
Artem Titove4ed6ea2019-01-11 11:02:19 +010026SamplesStatsCounter::~SamplesStatsCounter() = default;
Artem Titove6f6a0c2019-02-07 12:14:35 +010027SamplesStatsCounter::SamplesStatsCounter(const SamplesStatsCounter&) = default;
28SamplesStatsCounter& SamplesStatsCounter::operator=(
29 const SamplesStatsCounter&) = default;
Artem Titove4ed6ea2019-01-11 11:02:19 +010030SamplesStatsCounter::SamplesStatsCounter(SamplesStatsCounter&&) = default;
Artem Titove6f6a0c2019-02-07 12:14:35 +010031SamplesStatsCounter& SamplesStatsCounter::operator=(SamplesStatsCounter&&) =
32 default;
Artem Titove4ed6ea2019-01-11 11:02:19 +010033
34void SamplesStatsCounter::AddSample(double value) {
Danil Chapovalov0c626af2020-02-10 11:16:00 +010035 AddSample(StatsSample{value, Timestamp::Micros(rtc::TimeMicros())});
Artem Titov6fcdbc12019-09-11 11:45:40 +020036}
37
38void SamplesStatsCounter::AddSample(StatsSample sample) {
39 stats_.AddSample(sample.value);
40 samples_.push_back(sample);
Artem Titove4ed6ea2019-01-11 11:02:19 +010041 sorted_ = false;
Artem Titove4ed6ea2019-01-11 11:02:19 +010042}
43
Sebastian Janssond93a0042019-04-09 15:38:07 +020044void SamplesStatsCounter::AddSamples(const SamplesStatsCounter& other) {
Yves Gerey890f62b2019-04-10 17:18:48 +020045 stats_.MergeStatistics(other.stats_);
46 samples_.insert(samples_.end(), other.samples_.begin(), other.samples_.end());
Sebastian Janssond93a0042019-04-09 15:38:07 +020047 sorted_ = false;
Sebastian Janssond93a0042019-04-09 15:38:07 +020048}
49
Artem Titove4ed6ea2019-01-11 11:02:19 +010050double SamplesStatsCounter::GetPercentile(double percentile) {
51 RTC_DCHECK(!IsEmpty());
52 RTC_CHECK_GE(percentile, 0);
53 RTC_CHECK_LE(percentile, 1);
54 if (!sorted_) {
Artem Titov6fcdbc12019-09-11 11:45:40 +020055 absl::c_sort(samples_, [](const StatsSample& a, const StatsSample& b) {
56 return a.value < b.value;
57 });
Artem Titove4ed6ea2019-01-11 11:02:19 +010058 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
Artem Titov6fcdbc12019-09-11 11:45:40 +020076 const double low = samples_[rank].value;
77 const double high = samples_[std::min(rank + 1, samples_.size() - 1)].value;
Artem Titove4ed6ea2019-01-11 11:02:19 +010078 return low + fract_part * (high - low);
79}
80
Artem Titov2c5af4f2019-07-03 10:40:16 +020081SamplesStatsCounter operator*(const SamplesStatsCounter& counter,
82 double value) {
83 SamplesStatsCounter out;
Artem Titov6fcdbc12019-09-11 11:45:40 +020084 for (const auto& sample : counter.GetTimedSamples()) {
85 out.AddSample(
86 SamplesStatsCounter::StatsSample{sample.value * value, sample.time});
Artem Titov2c5af4f2019-07-03 10:40:16 +020087 }
88 return out;
89}
90
91SamplesStatsCounter operator/(const SamplesStatsCounter& counter,
92 double value) {
93 SamplesStatsCounter out;
Artem Titov6fcdbc12019-09-11 11:45:40 +020094 for (const auto& sample : counter.GetTimedSamples()) {
95 out.AddSample(
96 SamplesStatsCounter::StatsSample{sample.value / value, sample.time});
Artem Titov2c5af4f2019-07-03 10:40:16 +020097 }
98 return out;
99}
100
Artem Titove4ed6ea2019-01-11 11:02:19 +0100101} // namespace webrtc