Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: webrtc/base/bucketratetracker_unittest.cc

Issue 1279433006: Add a rate tracker that tracks rate over a given interval split up into buckets that accumulate uni… (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix type issues on win64. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2010 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/base/gunit.h" 11 #include "webrtc/base/gunit.h"
12 #include "webrtc/base/ratetracker.h" 12 #include "webrtc/base/bucketratetracker.h"
13 13
14 namespace rtc { 14 namespace rtc {
15 15
16 class RateTrackerForTest : public RateTracker { 16 class BucketRateTrackerForTest : public BucketRateTracker {
17 public: 17 public:
18 RateTrackerForTest() : time_(0) {} 18 BucketRateTrackerForTest() : BucketRateTracker(100u, 10u), time_(0) {}
19 virtual uint32 Time() const { return time_; } 19 virtual uint32 Time() const { return time_; }
20 void AdvanceTime(uint32 delta) { time_ += delta; } 20 void AdvanceTime(uint32 delta) { time_ += delta; }
21 21
22 private: 22 private:
23 uint32 time_; 23 uint32 time_;
24 }; 24 };
25 25
26 TEST(RateTrackerTest, TestBasics) { 26 TEST(BucketRateTrackerTest, Test30FPS) {
27 RateTrackerForTest tracker; 27 BucketRateTrackerForTest tracker;
28 EXPECT_EQ(0U, tracker.total_units()); 28
29 for (int i = 0; i < 100; ++i) {
30 tracker.AdvanceTime(33);
31 if (i % 3 == 0) {
32 tracker.AdvanceTime(1);
33 }
34 tracker.Update(1);
35 }
36 EXPECT_EQ(30u, tracker.units_second());
37 }
38
39 TEST(BucketRateTrackerTest, Test60FPS) {
40 BucketRateTrackerForTest tracker;
41
42 for (int i = 0; i < 100; ++i) {
43 tracker.AdvanceTime(16);
44 if (i % 3 != 0) {
45 tracker.AdvanceTime(1);
46 }
47 tracker.Update(1);
48 }
49 EXPECT_EQ(60u, tracker.units_second());
50 }
51
52 TEST(BucketRateTrackerTest, TestRateTrackerBasics) {
53 BucketRateTrackerForTest tracker;
29 EXPECT_EQ(0U, tracker.units_second()); 54 EXPECT_EQ(0U, tracker.units_second());
30 55
31 // Add a sample. 56 // Add a sample.
32 tracker.Update(1234); 57 tracker.Update(1234);
33 // Advance the clock by 100 ms. 58 // Advance the clock by 100 ms.
34 tracker.AdvanceTime(100); 59 tracker.AdvanceTime(100);
35 // total_units should advance, but units_second should stay 0. 60 // total_units should advance, but units_second should stay 0.
36 EXPECT_EQ(1234U, tracker.total_units());
37 EXPECT_EQ(0U, tracker.units_second()); 61 EXPECT_EQ(0U, tracker.units_second());
38 62
39 // Repeat. 63 // Repeat.
40 tracker.Update(1234); 64 tracker.Update(1234);
41 tracker.AdvanceTime(100); 65 tracker.AdvanceTime(100);
42 EXPECT_EQ(1234U * 2, tracker.total_units());
43 EXPECT_EQ(0U, tracker.units_second()); 66 EXPECT_EQ(0U, tracker.units_second());
44 67
45 // Advance the clock by 800 ms, so we've elapsed a full second. 68 // Advance the clock by 800 ms, so we've elapsed a full second.
46 // units_second should now be filled in properly. 69 // units_second should now be filled in properly.
47 tracker.AdvanceTime(800); 70 tracker.AdvanceTime(800);
48 EXPECT_EQ(1234U * 2, tracker.total_units());
49 EXPECT_EQ(1234U * 2, tracker.units_second()); 71 EXPECT_EQ(1234U * 2, tracker.units_second());
50 72
51 // Poll the tracker again immediately. The reported rate should stay the same. 73 // Poll the tracker again immediately. The reported rate should stay the same.
52 EXPECT_EQ(1234U * 2, tracker.total_units());
53 EXPECT_EQ(1234U * 2, tracker.units_second()); 74 EXPECT_EQ(1234U * 2, tracker.units_second());
54 75
55 // Do nothing and advance by a second. We should drop down to zero. 76 // Do nothing and advance by a second. We should drop down to zero.
56 tracker.AdvanceTime(1000); 77 tracker.AdvanceTime(1000);
57 EXPECT_EQ(1234U * 2, tracker.total_units());
58 EXPECT_EQ(0U, tracker.units_second()); 78 EXPECT_EQ(0U, tracker.units_second());
59 79
60 // Send a bunch of data at a constant rate for 5.5 "seconds". 80 // Send a bunch of data at a constant rate for 5.5 "seconds".
61 // We should report the rate properly. 81 // We should report the rate properly.
62 for (int i = 0; i < 5500; i += 100) { 82 for (int i = 0; i < 5500; i += 100) {
83 tracker.AdvanceTime(100);
63 tracker.Update(9876U); 84 tracker.Update(9876U);
64 tracker.AdvanceTime(100);
65 } 85 }
66 EXPECT_EQ(9876U * 10, tracker.units_second()); 86 EXPECT_EQ(9876U * 10, tracker.units_second());
67 87
68 // Advance the clock by 500 ms. Since we sent nothing over this half-second, 88 // Advance the clock by 500 ms. Since we sent nothing over this half-second,
69 // the reported rate should be reduced by half. 89 // the reported rate should be reduced by half.
70 tracker.AdvanceTime(500); 90 tracker.AdvanceTime(500);
71 EXPECT_EQ(9876U * 5, tracker.units_second()); 91 EXPECT_EQ(9876U * 5, tracker.units_second());
72 } 92 }
73 93
74 TEST(RateTrackerTest, TestGetUnitSecondsAfterInitialValue) { 94 TEST(BucketRateTrackerTest, TestGetUnitSecondsAfterInitialValue) {
75 RateTrackerForTest tracker; 95 BucketRateTrackerForTest tracker;
76 tracker.Update(1234); 96 tracker.Update(1234);
77 tracker.AdvanceTime(1000); 97 tracker.AdvanceTime(1000);
78 EXPECT_EQ(1234u, tracker.units_second()); 98 EXPECT_EQ(1234u, tracker.units_second());
79 } 99 }
80 100
101 class IntervalRateTrackerForTest : public IntervalRateTracker {
102 public:
103 IntervalRateTrackerForTest() : IntervalRateTracker(15u), time_(0) {}
104 virtual uint32 Time() const { return time_; }
105 void AdvanceTime(uint32 delta) { time_ += delta; }
106
107 private:
108 uint32 time_;
109 };
110
111 TEST(IntervalRateTrackerTest, Test30FPS) {
112 IntervalRateTrackerForTest tracker;
113
114 for (int i = 0; i < 30*30; ++i) {
115 tracker.AdvanceTime(33);
116 if (i % 3 == 0) {
117 tracker.AdvanceTime(1);
118 }
119 tracker.Update(1);
120 }
121 EXPECT_EQ(30u, tracker.units_second());
122 }
123
81 } // namespace rtc 124 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698