3 * Copyright 2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "talk/base/bandwidthsmoother.h"
31 #include "talk/base/gunit.h"
35 static const int kTimeBetweenIncrease = 10;
36 static const double kPercentIncrease = 1.1;
37 static const size_t kSamplesCountToAverage = 2;
38 static const double kMinSampleCountPercent = 1.0;
40 TEST(BandwidthSmootherTest, TestSampleIncrease) {
41 BandwidthSmoother mon(1000, // initial_bandwidth_guess
44 kSamplesCountToAverage,
45 kMinSampleCountPercent);
47 int bandwidth_sample = 1000;
48 EXPECT_EQ(bandwidth_sample, mon.get_bandwidth_estimation());
50 static_cast<int>(bandwidth_sample * kPercentIncrease);
51 EXPECT_FALSE(mon.Sample(9, bandwidth_sample));
52 EXPECT_TRUE(mon.Sample(10, bandwidth_sample));
53 EXPECT_EQ(bandwidth_sample, mon.get_bandwidth_estimation());
54 int next_expected_est =
55 static_cast<int>(bandwidth_sample * kPercentIncrease);
56 bandwidth_sample *= 2;
57 EXPECT_TRUE(mon.Sample(20, bandwidth_sample));
58 EXPECT_EQ(next_expected_est, mon.get_bandwidth_estimation());
61 TEST(BandwidthSmootherTest, TestSampleIncreaseFromZero) {
62 BandwidthSmoother mon(0, // initial_bandwidth_guess
65 kSamplesCountToAverage,
66 kMinSampleCountPercent);
68 const int kBandwidthSample = 1000;
69 EXPECT_EQ(0, mon.get_bandwidth_estimation());
70 EXPECT_FALSE(mon.Sample(9, kBandwidthSample));
71 EXPECT_TRUE(mon.Sample(10, kBandwidthSample));
72 EXPECT_EQ(kBandwidthSample, mon.get_bandwidth_estimation());
75 TEST(BandwidthSmootherTest, TestSampleDecrease) {
76 BandwidthSmoother mon(1000, // initial_bandwidth_guess
79 kSamplesCountToAverage,
80 kMinSampleCountPercent);
82 const int kBandwidthSample = 999;
83 EXPECT_EQ(1000, mon.get_bandwidth_estimation());
84 EXPECT_FALSE(mon.Sample(1, kBandwidthSample));
85 EXPECT_EQ(1000, mon.get_bandwidth_estimation());
86 EXPECT_TRUE(mon.Sample(2, kBandwidthSample));
87 EXPECT_EQ(kBandwidthSample, mon.get_bandwidth_estimation());
90 TEST(BandwidthSmootherTest, TestSampleTooFewSamples) {
91 BandwidthSmoother mon(1000, // initial_bandwidth_guess
95 0.5); // 5 min samples.
97 const int kBandwidthSample = 500;
98 EXPECT_EQ(1000, mon.get_bandwidth_estimation());
99 EXPECT_FALSE(mon.Sample(1, kBandwidthSample));
100 EXPECT_FALSE(mon.Sample(2, kBandwidthSample));
101 EXPECT_FALSE(mon.Sample(3, kBandwidthSample));
102 EXPECT_FALSE(mon.Sample(4, kBandwidthSample));
103 EXPECT_EQ(1000, mon.get_bandwidth_estimation());
104 EXPECT_TRUE(mon.Sample(5, kBandwidthSample));
105 EXPECT_EQ(kBandwidthSample, mon.get_bandwidth_estimation());
108 TEST(BandwidthSmootherTest, TestSampleRollover) {
109 const int kHugeBandwidth = 2000000000; // > INT_MAX/1.1
110 BandwidthSmoother mon(kHugeBandwidth,
111 kTimeBetweenIncrease,
113 kSamplesCountToAverage,
114 kMinSampleCountPercent);
116 EXPECT_FALSE(mon.Sample(10, INT_MAX));
117 EXPECT_FALSE(mon.Sample(11, INT_MAX));
118 EXPECT_EQ(kHugeBandwidth, mon.get_bandwidth_estimation());
121 TEST(BandwidthSmootherTest, TestSampleNegative) {
122 BandwidthSmoother mon(1000, // initial_bandwidth_guess
123 kTimeBetweenIncrease,
125 kSamplesCountToAverage,
126 kMinSampleCountPercent);
128 EXPECT_FALSE(mon.Sample(10, -1));
129 EXPECT_FALSE(mon.Sample(11, -1));
130 EXPECT_EQ(1000, mon.get_bandwidth_estimation());
133 } // namespace talk_base