diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/libwebrtc/rtc_base/rate_tracker_unittest.cc | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/libwebrtc/rtc_base/rate_tracker_unittest.cc')
-rw-r--r-- | third_party/libwebrtc/rtc_base/rate_tracker_unittest.cc | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/third_party/libwebrtc/rtc_base/rate_tracker_unittest.cc b/third_party/libwebrtc/rtc_base/rate_tracker_unittest.cc new file mode 100644 index 0000000000..22ae2c07e7 --- /dev/null +++ b/third_party/libwebrtc/rtc_base/rate_tracker_unittest.cc @@ -0,0 +1,178 @@ +/* + * Copyright 2015 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "rtc_base/rate_tracker.h" + +#include "test/gtest.h" + +namespace rtc { +namespace { +const uint32_t kBucketIntervalMs = 100; +} // namespace + +class RateTrackerForTest : public RateTracker { + public: + RateTrackerForTest() : RateTracker(kBucketIntervalMs, 10u), time_(0) {} + int64_t Time() const override { return time_; } + void AdvanceTime(int delta) { time_ += delta; } + + private: + int64_t time_; +}; + +TEST(RateTrackerTest, Test30FPS) { + RateTrackerForTest tracker; + + for (int i = 0; i < 300; ++i) { + tracker.AddSamples(1); + tracker.AdvanceTime(33); + if (i % 3 == 0) { + tracker.AdvanceTime(1); + } + } + EXPECT_DOUBLE_EQ(30.0, tracker.ComputeRateForInterval(50000)); +} + +TEST(RateTrackerTest, Test60FPS) { + RateTrackerForTest tracker; + + for (int i = 0; i < 300; ++i) { + tracker.AddSamples(1); + tracker.AdvanceTime(16); + if (i % 3 != 0) { + tracker.AdvanceTime(1); + } + } + EXPECT_DOUBLE_EQ(60.0, tracker.ComputeRateForInterval(1000)); +} + +TEST(RateTrackerTest, TestRateTrackerBasics) { + RateTrackerForTest tracker; + EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRateForInterval(1000)); + + // Add a sample. + tracker.AddSamples(1234); + // Advance the clock by less than one bucket interval (no rate returned). + tracker.AdvanceTime(kBucketIntervalMs - 1); + EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRate()); + // Advance the clock by 100 ms (one bucket interval). + tracker.AdvanceTime(1); + EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRateForInterval(1000)); + EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRate()); + EXPECT_EQ(1234U, tracker.TotalSampleCount()); + EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeTotalRate()); + + // Repeat. + tracker.AddSamples(1234); + tracker.AdvanceTime(100); + EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRateForInterval(1000)); + EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRate()); + EXPECT_EQ(1234U * 2, tracker.TotalSampleCount()); + EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeTotalRate()); + + // Advance the clock by 800 ms, so we've elapsed a full second. + // units_second should now be filled in properly. + tracker.AdvanceTime(800); + EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeRateForInterval(1000)); + EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeRate()); + EXPECT_EQ(1234U * 2, tracker.TotalSampleCount()); + EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeTotalRate()); + + // Poll the tracker again immediately. The reported rate should stay the same. + EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeRateForInterval(1000)); + EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeRate()); + EXPECT_EQ(1234U * 2, tracker.TotalSampleCount()); + EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeTotalRate()); + + // Do nothing and advance by a second. We should drop down to zero. + tracker.AdvanceTime(1000); + EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRateForInterval(1000)); + EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRate()); + EXPECT_EQ(1234U * 2, tracker.TotalSampleCount()); + EXPECT_DOUBLE_EQ(1234.0, tracker.ComputeTotalRate()); + + // Send a bunch of data at a constant rate for 5.5 "seconds". + // We should report the rate properly. + for (int i = 0; i < 5500; i += 100) { + tracker.AddSamples(9876U); + tracker.AdvanceTime(100); + } + EXPECT_DOUBLE_EQ(9876.0 * 10.0, tracker.ComputeRateForInterval(1000)); + EXPECT_DOUBLE_EQ(9876.0 * 10.0, tracker.ComputeRate()); + EXPECT_EQ(1234U * 2 + 9876U * 55, tracker.TotalSampleCount()); + EXPECT_DOUBLE_EQ((1234.0 * 2.0 + 9876.0 * 55.0) / 7.5, + tracker.ComputeTotalRate()); + + // Advance the clock by 500 ms. Since we sent nothing over this half-second, + // the reported rate should be reduced by half. + tracker.AdvanceTime(500); + EXPECT_DOUBLE_EQ(9876.0 * 5.0, tracker.ComputeRateForInterval(1000)); + EXPECT_DOUBLE_EQ(9876.0 * 5.0, tracker.ComputeRate()); + EXPECT_EQ(1234U * 2 + 9876U * 55, tracker.TotalSampleCount()); + EXPECT_DOUBLE_EQ((1234.0 * 2.0 + 9876.0 * 55.0) / 8.0, + tracker.ComputeTotalRate()); + + // Rate over the last half second should be zero. + EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRateForInterval(500)); +} + +TEST(RateTrackerTest, TestLongPeriodBetweenSamples) { + RateTrackerForTest tracker; + tracker.AddSamples(1); + tracker.AdvanceTime(1000); + EXPECT_DOUBLE_EQ(1.0, tracker.ComputeRate()); + + tracker.AdvanceTime(2000); + EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRate()); + + tracker.AdvanceTime(2000); + tracker.AddSamples(1); + EXPECT_DOUBLE_EQ(1.0, tracker.ComputeRate()); +} + +TEST(RateTrackerTest, TestRolloff) { + RateTrackerForTest tracker; + for (int i = 0; i < 10; ++i) { + tracker.AddSamples(1U); + tracker.AdvanceTime(100); + } + EXPECT_DOUBLE_EQ(10.0, tracker.ComputeRate()); + + for (int i = 0; i < 10; ++i) { + tracker.AddSamples(1U); + tracker.AdvanceTime(50); + } + EXPECT_DOUBLE_EQ(15.0, tracker.ComputeRate()); + EXPECT_DOUBLE_EQ(20.0, tracker.ComputeRateForInterval(500)); + + for (int i = 0; i < 10; ++i) { + tracker.AddSamples(1U); + tracker.AdvanceTime(50); + } + EXPECT_DOUBLE_EQ(20.0, tracker.ComputeRate()); +} + +TEST(RateTrackerTest, TestGetUnitSecondsAfterInitialValue) { + RateTrackerForTest tracker; + tracker.AddSamples(1234); + tracker.AdvanceTime(1000); + EXPECT_DOUBLE_EQ(1234.0, tracker.ComputeRateForInterval(1000)); +} + +TEST(RateTrackerTest, TestLargeNumbers) { + RateTrackerForTest tracker; + const uint64_t large_number = 0x100000000; + tracker.AddSamples(large_number); + tracker.AdvanceTime(1000); + tracker.AddSamples(large_number); + EXPECT_DOUBLE_EQ(large_number * 2, tracker.ComputeRate()); +} + +} // namespace rtc |