1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
|