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

Side by Side Diff: webrtc/base/ratetracker_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: Win64 fix Created 5 years, 3 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/ratetracker.h"
13 13
14 namespace rtc { 14 namespace rtc {
15 15
16 class RateTrackerForTest : public RateTracker { 16 class RateTrackerForTest : public RateTracker {
17 public: 17 public:
18 RateTrackerForTest() : time_(0) {} 18 RateTrackerForTest() : RateTracker(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(RateTrackerTest, Test30FPS) {
27 RateTrackerForTest tracker; 27 RateTrackerForTest tracker;
28 EXPECT_EQ(0U, tracker.total_units()); 28
29 EXPECT_EQ(0U, tracker.units_second()); 29 for (int i = 0; i < 300; ++i) {
30 tracker.AddSamples(1);
31 tracker.AdvanceTime(33);
32 if (i % 3 == 0) {
33 tracker.AdvanceTime(1);
34 }
35 }
36 EXPECT_EQ(30u, tracker.ComputeCurrentRate(50000u));
noahric 2015/09/03 21:42:24 Is there a version of this for doubles/floats that
tpsiaki 2015/09/03 23:28:59 Done.
37 }
38
39 TEST(RateTrackerTest, Test60FPS) {
40 RateTrackerForTest tracker;
41
42 for (int i = 0; i < 300; ++i) {
43 tracker.AddSamples(1);
44 tracker.AdvanceTime(16);
45 if (i % 3 != 0) {
46 tracker.AdvanceTime(1);
47 }
48 }
49 EXPECT_EQ(60u, tracker.ComputeCurrentRate(1000u));
50 }
51
52 TEST(RateTrackerTest, TestRateTrackerBasics) {
53 RateTrackerForTest tracker;
54 EXPECT_EQ(0U, tracker.ComputeCurrentRate(1000u));
30 55
31 // Add a sample. 56 // Add a sample.
32 tracker.Update(1234); 57 tracker.AddSamples(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 EXPECT_EQ(12340U, tracker.ComputeCurrentRate(1000u));
36 EXPECT_EQ(1234U, tracker.total_units()); 61 EXPECT_EQ(12340U, tracker.ComputeCurrentRate());
37 EXPECT_EQ(0U, tracker.units_second()); 62 EXPECT_EQ(1234U, tracker.TotalSampleCount());
63 EXPECT_EQ(12340U, tracker.ComputeTotalRate());
38 64
39 // Repeat. 65 // Repeat.
40 tracker.Update(1234); 66 tracker.AddSamples(1234);
41 tracker.AdvanceTime(100); 67 tracker.AdvanceTime(100);
42 EXPECT_EQ(1234U * 2, tracker.total_units()); 68 EXPECT_EQ(12340U, tracker.ComputeCurrentRate(1000u));
43 EXPECT_EQ(0U, tracker.units_second()); 69 EXPECT_EQ(12340U, tracker.ComputeCurrentRate());
70 EXPECT_EQ(1234U * 2, tracker.TotalSampleCount());
71 EXPECT_EQ(12340U, tracker.ComputeTotalRate());
44 72
45 // Advance the clock by 800 ms, so we've elapsed a full second. 73 // Advance the clock by 800 ms, so we've elapsed a full second.
46 // units_second should now be filled in properly. 74 // units_second should now be filled in properly.
47 tracker.AdvanceTime(800); 75 tracker.AdvanceTime(800);
48 EXPECT_EQ(1234U * 2, tracker.total_units()); 76 EXPECT_EQ(1234U * 2, tracker.ComputeCurrentRate(1000u));
49 EXPECT_EQ(1234U * 2, tracker.units_second()); 77 EXPECT_EQ(1234U * 2, tracker.ComputeCurrentRate());
78 EXPECT_EQ(1234U * 2, tracker.TotalSampleCount());
79 EXPECT_EQ(1234U * 2, tracker.ComputeTotalRate());
50 80
51 // Poll the tracker again immediately. The reported rate should stay the same. 81 // Poll the tracker again immediately. The reported rate should stay the same.
52 EXPECT_EQ(1234U * 2, tracker.total_units()); 82 EXPECT_EQ(1234U * 2, tracker.ComputeCurrentRate(1000u));
53 EXPECT_EQ(1234U * 2, tracker.units_second()); 83 EXPECT_EQ(1234U * 2, tracker.ComputeCurrentRate());
84 EXPECT_EQ(1234U * 2, tracker.TotalSampleCount());
85 EXPECT_EQ(1234U * 2, tracker.ComputeTotalRate());
54 86
55 // Do nothing and advance by a second. We should drop down to zero. 87 // Do nothing and advance by a second. We should drop down to zero.
56 tracker.AdvanceTime(1000); 88 tracker.AdvanceTime(1000);
57 EXPECT_EQ(1234U * 2, tracker.total_units()); 89 EXPECT_EQ(0U, tracker.ComputeCurrentRate(1000u));
58 EXPECT_EQ(0U, tracker.units_second()); 90 EXPECT_EQ(0U, tracker.ComputeCurrentRate());
91 EXPECT_EQ(1234U * 2, tracker.TotalSampleCount());
92 EXPECT_EQ(1234U, tracker.ComputeTotalRate());
59 93
60 // Send a bunch of data at a constant rate for 5.5 "seconds". 94 // Send a bunch of data at a constant rate for 5.5 "seconds".
61 // We should report the rate properly. 95 // We should report the rate properly.
62 for (int i = 0; i < 5500; i += 100) { 96 for (int i = 0; i < 5500; i += 100) {
63 tracker.Update(9876U); 97 tracker.AddSamples(9876U);
64 tracker.AdvanceTime(100); 98 tracker.AdvanceTime(100);
65 } 99 }
66 EXPECT_EQ(9876U * 10, tracker.units_second()); 100 EXPECT_EQ(9876U * 10, tracker.ComputeCurrentRate(1000u));
101 EXPECT_EQ(9876U * 10, tracker.ComputeCurrentRate());
102 EXPECT_EQ(1234U * 2 + 9876U * 55, tracker.TotalSampleCount());
103 EXPECT_EQ((1234U * 2 + 9876U * 55) / 7.5, tracker.ComputeTotalRate());
67 104
68 // Advance the clock by 500 ms. Since we sent nothing over this half-second, 105 // Advance the clock by 500 ms. Since we sent nothing over this half-second,
69 // the reported rate should be reduced by half. 106 // the reported rate should be reduced by half.
70 tracker.AdvanceTime(500); 107 tracker.AdvanceTime(500);
71 EXPECT_EQ(9876U * 5, tracker.units_second()); 108 EXPECT_EQ(9876U * 5, tracker.ComputeCurrentRate(1000u));
109 EXPECT_EQ(9876U * 5, tracker.ComputeCurrentRate());
110 EXPECT_EQ(1234U * 2 + 9876U * 55, tracker.TotalSampleCount());
111 EXPECT_EQ((1234U * 2 + 9876U * 55) / 8.0, tracker.ComputeTotalRate());
112
113 // Rate over the last half second should be zero.
114 EXPECT_EQ(0u, tracker.ComputeCurrentRate(500u));
noahric 2015/09/03 21:42:24 Can you also test (1) elapsed time > number of buc
tpsiaki 2015/09/03 23:28:59 Done.
72 } 115 }
73 116
74 TEST(RateTrackerTest, TestGetUnitSecondsAfterInitialValue) { 117 TEST(RateTrackerTest, TestGetUnitSecondsAfterInitialValue) {
75 RateTrackerForTest tracker; 118 RateTrackerForTest tracker;
76 tracker.Update(1234); 119 tracker.AddSamples(1234);
77 tracker.AdvanceTime(1000); 120 tracker.AdvanceTime(1000);
78 EXPECT_EQ(1234u, tracker.units_second()); 121 EXPECT_EQ(1234u, tracker.ComputeCurrentRate(1000u));
79 } 122 }
80 123
81 } // namespace rtc 124 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698