blob: 27aa3a9299d744ff5851cff3a7fcb91e0cc36dac [file] [log] [blame]
henrike@webrtc.org47be73b2014-05-13 18:00:261/*
Tim Psiaki3e255232015-09-14 17:38:082 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
henrike@webrtc.org47be73b2014-05-13 18:00:263 *
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
kjellander19796962017-06-30 17:45:2111#include "webrtc/rtc_base/ratetracker.h"
12#include "webrtc/rtc_base/gunit.h"
henrike@webrtc.org47be73b2014-05-13 18:00:2613
14namespace rtc {
asapersson7ac0a742016-02-02 09:46:5315namespace {
16 const uint32_t kBucketIntervalMs = 100;
17} // namespace
henrike@webrtc.org47be73b2014-05-13 18:00:2618
19class RateTrackerForTest : public RateTracker {
20 public:
asapersson7ac0a742016-02-02 09:46:5321 RateTrackerForTest() : RateTracker(kBucketIntervalMs, 10u), time_(0) {}
ehmaldonadocff7edc2017-01-04 15:11:2322 int64_t Time() const override { return time_; }
Honghai Zhang3f20ddb2016-05-06 18:29:1523 void AdvanceTime(int delta) { time_ += delta; }
henrike@webrtc.org47be73b2014-05-13 18:00:2624
25 private:
Honghai Zhang3f20ddb2016-05-06 18:29:1526 int64_t time_;
henrike@webrtc.org47be73b2014-05-13 18:00:2627};
28
Tim Psiaki3e255232015-09-14 17:38:0829TEST(RateTrackerTest, Test30FPS) {
henrike@webrtc.org47be73b2014-05-13 18:00:2630 RateTrackerForTest tracker;
Tim Psiaki3e255232015-09-14 17:38:0831
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 Zhang3f20ddb2016-05-06 18:29:1539 EXPECT_DOUBLE_EQ(30.0, tracker.ComputeRateForInterval(50000));
Tim Psiaki3e255232015-09-14 17:38:0840}
41
42TEST(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 Zhang3f20ddb2016-05-06 18:29:1552 EXPECT_DOUBLE_EQ(60.0, tracker.ComputeRateForInterval(1000));
Tim Psiaki3e255232015-09-14 17:38:0853}
54
55TEST(RateTrackerTest, TestRateTrackerBasics) {
56 RateTrackerForTest tracker;
Honghai Zhang3f20ddb2016-05-06 18:29:1557 EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRateForInterval(1000));
henrike@webrtc.org47be73b2014-05-13 18:00:2658
59 // Add a sample.
Tim Psiaki3e255232015-09-14 17:38:0860 tracker.AddSamples(1234);
asapersson7ac0a742016-02-02 09:46:5361 // 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 Zhang3f20ddb2016-05-06 18:29:1566 EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRateForInterval(1000));
Tim Psiaki3e255232015-09-14 17:38:0867 EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRate());
68 EXPECT_EQ(1234U, tracker.TotalSampleCount());
69 EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeTotalRate());
henrike@webrtc.org47be73b2014-05-13 18:00:2670
71 // Repeat.
Tim Psiaki3e255232015-09-14 17:38:0872 tracker.AddSamples(1234);
henrike@webrtc.org47be73b2014-05-13 18:00:2673 tracker.AdvanceTime(100);
Honghai Zhang3f20ddb2016-05-06 18:29:1574 EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRateForInterval(1000));
Tim Psiaki3e255232015-09-14 17:38:0875 EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRate());
76 EXPECT_EQ(1234U * 2, tracker.TotalSampleCount());
77 EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeTotalRate());
henrike@webrtc.org47be73b2014-05-13 18:00:2678
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 Zhang3f20ddb2016-05-06 18:29:1582 EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeRateForInterval(1000));
Tim Psiaki3e255232015-09-14 17:38:0883 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.org47be73b2014-05-13 18:00:2686
87 // Poll the tracker again immediately. The reported rate should stay the same.
Honghai Zhang3f20ddb2016-05-06 18:29:1588 EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeRateForInterval(1000));
Tim Psiaki3e255232015-09-14 17:38:0889 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.org47be73b2014-05-13 18:00:2692
93 // Do nothing and advance by a second. We should drop down to zero.
94 tracker.AdvanceTime(1000);
Honghai Zhang3f20ddb2016-05-06 18:29:1595 EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRateForInterval(1000));
Tim Psiaki3e255232015-09-14 17:38:0896 EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRate());
97 EXPECT_EQ(1234U * 2, tracker.TotalSampleCount());
98 EXPECT_DOUBLE_EQ(1234.0, tracker.ComputeTotalRate());
henrike@webrtc.org47be73b2014-05-13 18:00:2699
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 Psiaki3e255232015-09-14 17:38:08103 tracker.AddSamples(9876U);
henrike@webrtc.org47be73b2014-05-13 18:00:26104 tracker.AdvanceTime(100);
105 }
Honghai Zhang3f20ddb2016-05-06 18:29:15106 EXPECT_DOUBLE_EQ(9876.0 * 10.0, tracker.ComputeRateForInterval(1000));
Tim Psiaki3e255232015-09-14 17:38:08107 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.org47be73b2014-05-13 18:00:26111
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 Zhang3f20ddb2016-05-06 18:29:15115 EXPECT_DOUBLE_EQ(9876.0 * 5.0, tracker.ComputeRateForInterval(1000));
Tim Psiaki3e255232015-09-14 17:38:08116 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 Zhang3f20ddb2016-05-06 18:29:15122 EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRateForInterval(500));
Tim Psiaki3e255232015-09-14 17:38:08123}
124
125TEST(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
139TEST(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 Zhang3f20ddb2016-05-06 18:29:15152 EXPECT_DOUBLE_EQ(20.0, tracker.ComputeRateForInterval(500));
Tim Psiaki3e255232015-09-14 17:38:08153
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.org47be73b2014-05-13 18:00:26159}
160
perkj@webrtc.orga08308c2015-03-17 12:45:15161TEST(RateTrackerTest, TestGetUnitSecondsAfterInitialValue) {
162 RateTrackerForTest tracker;
Tim Psiaki3e255232015-09-14 17:38:08163 tracker.AddSamples(1234);
perkj@webrtc.orga08308c2015-03-17 12:45:15164 tracker.AdvanceTime(1000);
Honghai Zhang3f20ddb2016-05-06 18:29:15165 EXPECT_DOUBLE_EQ(1234.0, tracker.ComputeRateForInterval(1000));
perkj@webrtc.orga08308c2015-03-17 12:45:15166}
167
henrike@webrtc.org47be73b2014-05-13 18:00:26168} // namespace rtc