henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 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 | |
kjellander | 1979696 | 2017-06-30 17:45:21 | [diff] [blame] | 11 | #include "webrtc/rtc_base/ratetracker.h" |
| 12 | #include "webrtc/rtc_base/gunit.h" |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
| 14 | namespace rtc { |
asapersson | 7ac0a74 | 2016-02-02 09:46:53 | [diff] [blame] | 15 | namespace { |
| 16 | const uint32_t kBucketIntervalMs = 100; |
| 17 | } // namespace |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 18 | |
| 19 | class RateTrackerForTest : public RateTracker { |
| 20 | public: |
asapersson | 7ac0a74 | 2016-02-02 09:46:53 | [diff] [blame] | 21 | RateTrackerForTest() : RateTracker(kBucketIntervalMs, 10u), time_(0) {} |
ehmaldonado | cff7edc | 2017-01-04 15:11:23 | [diff] [blame] | 22 | int64_t Time() const override { return time_; } |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 23 | void AdvanceTime(int delta) { time_ += delta; } |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 24 | |
| 25 | private: |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 26 | int64_t time_; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 27 | }; |
| 28 | |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 29 | TEST(RateTrackerTest, Test30FPS) { |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 30 | RateTrackerForTest tracker; |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 31 | |
| 32 | for (int i = 0; i < 300; ++i) { |
| 33 | tracker.AddSamples(1); |
| 34 | tracker.AdvanceTime(33); |
| 35 | if (i % 3 == 0) { |
| 36 | tracker.AdvanceTime(1); |
| 37 | } |
| 38 | } |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 39 | EXPECT_DOUBLE_EQ(30.0, tracker.ComputeRateForInterval(50000)); |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | TEST(RateTrackerTest, Test60FPS) { |
| 43 | RateTrackerForTest tracker; |
| 44 | |
| 45 | for (int i = 0; i < 300; ++i) { |
| 46 | tracker.AddSamples(1); |
| 47 | tracker.AdvanceTime(16); |
| 48 | if (i % 3 != 0) { |
| 49 | tracker.AdvanceTime(1); |
| 50 | } |
| 51 | } |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 52 | EXPECT_DOUBLE_EQ(60.0, tracker.ComputeRateForInterval(1000)); |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | TEST(RateTrackerTest, TestRateTrackerBasics) { |
| 56 | RateTrackerForTest tracker; |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 57 | EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRateForInterval(1000)); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 58 | |
| 59 | // Add a sample. |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 60 | tracker.AddSamples(1234); |
asapersson | 7ac0a74 | 2016-02-02 09:46:53 | [diff] [blame] | 61 | // Advance the clock by less than one bucket interval (no rate returned). |
| 62 | tracker.AdvanceTime(kBucketIntervalMs - 1); |
| 63 | EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRate()); |
| 64 | // Advance the clock by 100 ms (one bucket interval). |
| 65 | tracker.AdvanceTime(1); |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 66 | EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRateForInterval(1000)); |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 67 | EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRate()); |
| 68 | EXPECT_EQ(1234U, tracker.TotalSampleCount()); |
| 69 | EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeTotalRate()); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 70 | |
| 71 | // Repeat. |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 72 | tracker.AddSamples(1234); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 73 | tracker.AdvanceTime(100); |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 74 | EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRateForInterval(1000)); |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 75 | EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRate()); |
| 76 | EXPECT_EQ(1234U * 2, tracker.TotalSampleCount()); |
| 77 | EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeTotalRate()); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 78 | |
| 79 | // Advance the clock by 800 ms, so we've elapsed a full second. |
| 80 | // units_second should now be filled in properly. |
| 81 | tracker.AdvanceTime(800); |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 82 | EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeRateForInterval(1000)); |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 83 | EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeRate()); |
| 84 | EXPECT_EQ(1234U * 2, tracker.TotalSampleCount()); |
| 85 | EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeTotalRate()); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 86 | |
| 87 | // Poll the tracker again immediately. The reported rate should stay the same. |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 88 | EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeRateForInterval(1000)); |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 89 | EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeRate()); |
| 90 | EXPECT_EQ(1234U * 2, tracker.TotalSampleCount()); |
| 91 | EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeTotalRate()); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 92 | |
| 93 | // Do nothing and advance by a second. We should drop down to zero. |
| 94 | tracker.AdvanceTime(1000); |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 95 | EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRateForInterval(1000)); |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 96 | EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRate()); |
| 97 | EXPECT_EQ(1234U * 2, tracker.TotalSampleCount()); |
| 98 | EXPECT_DOUBLE_EQ(1234.0, tracker.ComputeTotalRate()); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 99 | |
| 100 | // Send a bunch of data at a constant rate for 5.5 "seconds". |
| 101 | // We should report the rate properly. |
| 102 | for (int i = 0; i < 5500; i += 100) { |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 103 | tracker.AddSamples(9876U); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 104 | tracker.AdvanceTime(100); |
| 105 | } |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 106 | EXPECT_DOUBLE_EQ(9876.0 * 10.0, tracker.ComputeRateForInterval(1000)); |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 107 | EXPECT_DOUBLE_EQ(9876.0 * 10.0, tracker.ComputeRate()); |
| 108 | EXPECT_EQ(1234U * 2 + 9876U * 55, tracker.TotalSampleCount()); |
| 109 | EXPECT_DOUBLE_EQ((1234.0 * 2.0 + 9876.0 * 55.0) / 7.5, |
| 110 | tracker.ComputeTotalRate()); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 111 | |
| 112 | // Advance the clock by 500 ms. Since we sent nothing over this half-second, |
| 113 | // the reported rate should be reduced by half. |
| 114 | tracker.AdvanceTime(500); |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 115 | EXPECT_DOUBLE_EQ(9876.0 * 5.0, tracker.ComputeRateForInterval(1000)); |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 116 | EXPECT_DOUBLE_EQ(9876.0 * 5.0, tracker.ComputeRate()); |
| 117 | EXPECT_EQ(1234U * 2 + 9876U * 55, tracker.TotalSampleCount()); |
| 118 | EXPECT_DOUBLE_EQ((1234.0 * 2.0 + 9876.0 * 55.0) / 8.0, |
| 119 | tracker.ComputeTotalRate()); |
| 120 | |
| 121 | // Rate over the last half second should be zero. |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 122 | EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRateForInterval(500)); |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | TEST(RateTrackerTest, TestLongPeriodBetweenSamples) { |
| 126 | RateTrackerForTest tracker; |
| 127 | tracker.AddSamples(1); |
| 128 | tracker.AdvanceTime(1000); |
| 129 | EXPECT_DOUBLE_EQ(1.0, tracker.ComputeRate()); |
| 130 | |
| 131 | tracker.AdvanceTime(2000); |
| 132 | EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRate()); |
| 133 | |
| 134 | tracker.AdvanceTime(2000); |
| 135 | tracker.AddSamples(1); |
| 136 | EXPECT_DOUBLE_EQ(1.0, tracker.ComputeRate()); |
| 137 | } |
| 138 | |
| 139 | TEST(RateTrackerTest, TestRolloff) { |
| 140 | RateTrackerForTest tracker; |
| 141 | for (int i = 0; i < 10; ++i) { |
| 142 | tracker.AddSamples(1U); |
| 143 | tracker.AdvanceTime(100); |
| 144 | } |
| 145 | EXPECT_DOUBLE_EQ(10.0, tracker.ComputeRate()); |
| 146 | |
| 147 | for (int i = 0; i < 10; ++i) { |
| 148 | tracker.AddSamples(1U); |
| 149 | tracker.AdvanceTime(50); |
| 150 | } |
| 151 | EXPECT_DOUBLE_EQ(15.0, tracker.ComputeRate()); |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 152 | EXPECT_DOUBLE_EQ(20.0, tracker.ComputeRateForInterval(500)); |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 153 | |
| 154 | for (int i = 0; i < 10; ++i) { |
| 155 | tracker.AddSamples(1U); |
| 156 | tracker.AdvanceTime(50); |
| 157 | } |
| 158 | EXPECT_DOUBLE_EQ(20.0, tracker.ComputeRate()); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 159 | } |
| 160 | |
perkj@webrtc.org | a08308c | 2015-03-17 12:45:15 | [diff] [blame] | 161 | TEST(RateTrackerTest, TestGetUnitSecondsAfterInitialValue) { |
| 162 | RateTrackerForTest tracker; |
Tim Psiaki | 3e25523 | 2015-09-14 17:38:08 | [diff] [blame] | 163 | tracker.AddSamples(1234); |
perkj@webrtc.org | a08308c | 2015-03-17 12:45:15 | [diff] [blame] | 164 | tracker.AdvanceTime(1000); |
Honghai Zhang | 3f20ddb | 2016-05-06 18:29:15 | [diff] [blame] | 165 | EXPECT_DOUBLE_EQ(1234.0, tracker.ComputeRateForInterval(1000)); |
perkj@webrtc.org | a08308c | 2015-03-17 12:45:15 | [diff] [blame] | 166 | } |
| 167 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 168 | } // namespace rtc |