blob: 36871a671354ceb6a7eca9ccc82d3fcc5447f455 [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;
22SamplesStatsCounter::~SamplesStatsCounter() = default;
Artem Titove6f6a0c2019-02-07 12:14:35 +010023SamplesStatsCounter::SamplesStatsCounter(const SamplesStatsCounter&) = default;
24SamplesStatsCounter& SamplesStatsCounter::operator=(
25 const SamplesStatsCounter&) = default;
Artem Titove4ed6ea2019-01-11 11:02:19 +010026SamplesStatsCounter::SamplesStatsCounter(SamplesStatsCounter&&) = default;
Artem Titove6f6a0c2019-02-07 12:14:35 +010027SamplesStatsCounter& SamplesStatsCounter::operator=(SamplesStatsCounter&&) =
28 default;
Artem Titove4ed6ea2019-01-11 11:02:19 +010029
30void SamplesStatsCounter::AddSample(double value) {
Danil Chapovalov0c626af2020-02-10 11:16:00 +010031 AddSample(StatsSample{value, Timestamp::Micros(rtc::TimeMicros())});
Artem Titov6fcdbc12019-09-11 11:45:40 +020032}
33
34void SamplesStatsCounter::AddSample(StatsSample sample) {
35 stats_.AddSample(sample.value);
36 samples_.push_back(sample);
Artem Titove4ed6ea2019-01-11 11:02:19 +010037 sorted_ = false;
Artem Titove4ed6ea2019-01-11 11:02:19 +010038}
39
Sebastian Janssond93a0042019-04-09 15:38:07 +020040void SamplesStatsCounter::AddSamples(const SamplesStatsCounter& other) {
Yves Gerey890f62b2019-04-10 17:18:48 +020041 stats_.MergeStatistics(other.stats_);
42 samples_.insert(samples_.end(), other.samples_.begin(), other.samples_.end());
Sebastian Janssond93a0042019-04-09 15:38:07 +020043 sorted_ = false;
Sebastian Janssond93a0042019-04-09 15:38:07 +020044}
45
Artem Titove4ed6ea2019-01-11 11:02:19 +010046double SamplesStatsCounter::GetPercentile(double percentile) {
47 RTC_DCHECK(!IsEmpty());
48 RTC_CHECK_GE(percentile, 0);
49 RTC_CHECK_LE(percentile, 1);
50 if (!sorted_) {
Artem Titov6fcdbc12019-09-11 11:45:40 +020051 absl::c_sort(samples_, [](const StatsSample& a, const StatsSample& b) {
52 return a.value < b.value;
53 });
Artem Titove4ed6ea2019-01-11 11:02:19 +010054 sorted_ = true;
55 }
56 const double raw_rank = percentile * (samples_.size() - 1);
57 double int_part;
58 double fract_part = std::modf(raw_rank, &int_part);
59 size_t rank = static_cast<size_t>(int_part);
60 if (fract_part >= 1.0) {
61 // It can happen due to floating point calculation error.
62 rank++;
63 fract_part -= 1.0;
64 }
65
66 RTC_DCHECK_GE(rank, 0);
67 RTC_DCHECK_LT(rank, samples_.size());
68 RTC_DCHECK_GE(fract_part, 0);
69 RTC_DCHECK_LT(fract_part, 1);
70 RTC_DCHECK(rank + fract_part == raw_rank);
71
Artem Titov6fcdbc12019-09-11 11:45:40 +020072 const double low = samples_[rank].value;
73 const double high = samples_[std::min(rank + 1, samples_.size() - 1)].value;
Artem Titove4ed6ea2019-01-11 11:02:19 +010074 return low + fract_part * (high - low);
75}
76
Artem Titov2c5af4f2019-07-03 10:40:16 +020077SamplesStatsCounter operator*(const SamplesStatsCounter& counter,
78 double value) {
79 SamplesStatsCounter out;
Artem Titov6fcdbc12019-09-11 11:45:40 +020080 for (const auto& sample : counter.GetTimedSamples()) {
81 out.AddSample(
82 SamplesStatsCounter::StatsSample{sample.value * value, sample.time});
Artem Titov2c5af4f2019-07-03 10:40:16 +020083 }
84 return out;
85}
86
87SamplesStatsCounter operator/(const SamplesStatsCounter& counter,
88 double value) {
89 SamplesStatsCounter out;
Artem Titov6fcdbc12019-09-11 11:45:40 +020090 for (const auto& sample : counter.GetTimedSamples()) {
91 out.AddSample(
92 SamplesStatsCounter::StatsSample{sample.value / value, sample.time});
Artem Titov2c5af4f2019-07-03 10:40:16 +020093 }
94 return out;
95}
96
Artem Titove4ed6ea2019-01-11 11:02:19 +010097} // namespace webrtc