OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2015 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 | |
11 #include "webrtc/base/gunit.h" | |
12 #include "webrtc/base/ratetracker.h" | |
13 | |
14 namespace rtc { | |
15 namespace { | |
16 const uint32_t kBucketIntervalMs = 100; | |
17 } // namespace | |
18 | |
19 class RateTrackerForTest : public RateTracker { | |
20 public: | |
21 RateTrackerForTest() : RateTracker(kBucketIntervalMs, 10u), time_(0) {} | |
22 int64_t Time() const override { return time_; } | |
23 void AdvanceTime(int delta) { time_ += delta; } | |
24 | |
25 private: | |
26 int64_t time_; | |
27 }; | |
28 | |
29 TEST(RateTrackerTest, Test30FPS) { | |
30 RateTrackerForTest tracker; | |
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 } | |
39 EXPECT_DOUBLE_EQ(30.0, tracker.ComputeRateForInterval(50000)); | |
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 } | |
52 EXPECT_DOUBLE_EQ(60.0, tracker.ComputeRateForInterval(1000)); | |
53 } | |
54 | |
55 TEST(RateTrackerTest, TestRateTrackerBasics) { | |
56 RateTrackerForTest tracker; | |
57 EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRateForInterval(1000)); | |
58 | |
59 // Add a sample. | |
60 tracker.AddSamples(1234); | |
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); | |
66 EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRateForInterval(1000)); | |
67 EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRate()); | |
68 EXPECT_EQ(1234U, tracker.TotalSampleCount()); | |
69 EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeTotalRate()); | |
70 | |
71 // Repeat. | |
72 tracker.AddSamples(1234); | |
73 tracker.AdvanceTime(100); | |
74 EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRateForInterval(1000)); | |
75 EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeRate()); | |
76 EXPECT_EQ(1234U * 2, tracker.TotalSampleCount()); | |
77 EXPECT_DOUBLE_EQ(12340.0, tracker.ComputeTotalRate()); | |
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); | |
82 EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeRateForInterval(1000)); | |
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()); | |
86 | |
87 // Poll the tracker again immediately. The reported rate should stay the same. | |
88 EXPECT_DOUBLE_EQ(1234.0 * 2.0, tracker.ComputeRateForInterval(1000)); | |
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()); | |
92 | |
93 // Do nothing and advance by a second. We should drop down to zero. | |
94 tracker.AdvanceTime(1000); | |
95 EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRateForInterval(1000)); | |
96 EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRate()); | |
97 EXPECT_EQ(1234U * 2, tracker.TotalSampleCount()); | |
98 EXPECT_DOUBLE_EQ(1234.0, tracker.ComputeTotalRate()); | |
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) { | |
103 tracker.AddSamples(9876U); | |
104 tracker.AdvanceTime(100); | |
105 } | |
106 EXPECT_DOUBLE_EQ(9876.0 * 10.0, tracker.ComputeRateForInterval(1000)); | |
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()); | |
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); | |
115 EXPECT_DOUBLE_EQ(9876.0 * 5.0, tracker.ComputeRateForInterval(1000)); | |
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. | |
122 EXPECT_DOUBLE_EQ(0.0, tracker.ComputeRateForInterval(500)); | |
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()); | |
152 EXPECT_DOUBLE_EQ(20.0, tracker.ComputeRateForInterval(500)); | |
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()); | |
159 } | |
160 | |
161 TEST(RateTrackerTest, TestGetUnitSecondsAfterInitialValue) { | |
162 RateTrackerForTest tracker; | |
163 tracker.AddSamples(1234); | |
164 tracker.AdvanceTime(1000); | |
165 EXPECT_DOUBLE_EQ(1234.0, tracker.ComputeRateForInterval(1000)); | |
166 } | |
167 | |
168 } // namespace rtc | |
OLD | NEW |