Add support for min value in SampleCounter
Required logic to query the min value of a SampleCounter along with some additions to the existing test cases Bug: webrtc:15580 Change-Id: I46afb30ad130f17f9e68ebc794b6935187bb2479 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/323900 Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Harald Alvestrand <hta@webrtc.org> Cr-Commit-Position: refs/heads/main@{#40956}
This commit is contained in:
parent
de7f17d421
commit
3723433d2f
@ -31,6 +31,9 @@ void SampleCounter::Add(int sample) {
|
||||
if (!max_ || sample > *max_) {
|
||||
max_ = sample;
|
||||
}
|
||||
if (!min_ || sample < *min_) {
|
||||
min_ = sample;
|
||||
}
|
||||
}
|
||||
|
||||
void SampleCounter::Add(const SampleCounter& other) {
|
||||
@ -45,6 +48,8 @@ void SampleCounter::Add(const SampleCounter& other) {
|
||||
num_samples_ += other.num_samples_;
|
||||
if (other.max_ && (!max_ || *max_ < *other.max_))
|
||||
max_ = other.max_;
|
||||
if (other.min_ && (!min_ || *min_ > *other.min_))
|
||||
min_ = other.min_;
|
||||
}
|
||||
|
||||
absl::optional<int> SampleCounter::Avg(int64_t min_required_samples) const {
|
||||
@ -58,6 +63,10 @@ absl::optional<int> SampleCounter::Max() const {
|
||||
return max_;
|
||||
}
|
||||
|
||||
absl::optional<int> SampleCounter::Min() const {
|
||||
return min_;
|
||||
}
|
||||
|
||||
absl::optional<int64_t> SampleCounter::Sum(int64_t min_required_samples) const {
|
||||
RTC_DCHECK_GT(min_required_samples, 0);
|
||||
if (num_samples_ < min_required_samples)
|
||||
|
||||
@ -26,6 +26,7 @@ class SampleCounter {
|
||||
void Add(int sample);
|
||||
absl::optional<int> Avg(int64_t min_required_samples) const;
|
||||
absl::optional<int> Max() const;
|
||||
absl::optional<int> Min() const;
|
||||
absl::optional<int64_t> Sum(int64_t min_required_samples) const;
|
||||
int64_t NumSamples() const;
|
||||
void Reset();
|
||||
@ -37,6 +38,7 @@ class SampleCounter {
|
||||
int64_t sum_ = 0;
|
||||
int64_t num_samples_ = 0;
|
||||
absl::optional<int> max_;
|
||||
absl::optional<int> min_;
|
||||
};
|
||||
|
||||
class SampleCounterWithVariance : public SampleCounter {
|
||||
|
||||
@ -24,6 +24,7 @@ TEST(SampleCounterTest, ProcessesNoSamples) {
|
||||
SampleCounter counter;
|
||||
EXPECT_THAT(counter.Avg(kMinSamples), Eq(absl::nullopt));
|
||||
EXPECT_THAT(counter.Max(), Eq(absl::nullopt));
|
||||
EXPECT_THAT(counter.Min(), Eq(absl::nullopt));
|
||||
}
|
||||
|
||||
TEST(SampleCounterTest, NotEnoughSamples) {
|
||||
@ -35,6 +36,7 @@ TEST(SampleCounterTest, NotEnoughSamples) {
|
||||
EXPECT_THAT(counter.Avg(kMinSamples), Eq(absl::nullopt));
|
||||
EXPECT_THAT(counter.Sum(kMinSamples), Eq(absl::nullopt));
|
||||
EXPECT_THAT(counter.Max(), Eq(5));
|
||||
EXPECT_THAT(counter.Min(), Eq(1));
|
||||
}
|
||||
|
||||
TEST(SampleCounterTest, EnoughSamples) {
|
||||
@ -46,6 +48,7 @@ TEST(SampleCounterTest, EnoughSamples) {
|
||||
EXPECT_THAT(counter.Avg(kMinSamples), Eq(3));
|
||||
EXPECT_THAT(counter.Sum(kMinSamples), Eq(15));
|
||||
EXPECT_THAT(counter.Max(), Eq(5));
|
||||
EXPECT_THAT(counter.Min(), Eq(1));
|
||||
}
|
||||
|
||||
TEST(SampleCounterTest, ComputesVariance) {
|
||||
@ -74,6 +77,7 @@ TEST(SampleCounterTest, AggregatesTwoCounters) {
|
||||
counter1.Add(counter2);
|
||||
EXPECT_THAT(counter1.Avg(kMinSamples), Eq(3));
|
||||
EXPECT_THAT(counter1.Max(), Eq(5));
|
||||
EXPECT_THAT(counter1.Min(), Eq(1));
|
||||
EXPECT_THAT(counter1.Variance(kMinSamples), Eq(2));
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user