You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
5.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  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.
  15. *
  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.
  26. */
  27. #include <limits.h>
  28. #include "talk/base/bandwidthsmoother.h"
  29. #include "talk/base/gunit.h"
  30. namespace talk_base {
  31. static const int kTimeBetweenIncrease = 10;
  32. static const double kPercentIncrease = 1.1;
  33. static const size_t kSamplesCountToAverage = 2;
  34. static const double kMinSampleCountPercent = 1.0;
  35. TEST(BandwidthSmootherTest, TestSampleIncrease) {
  36. BandwidthSmoother mon(1000, // initial_bandwidth_guess
  37. kTimeBetweenIncrease,
  38. kPercentIncrease,
  39. kSamplesCountToAverage,
  40. kMinSampleCountPercent);
  41. int bandwidth_sample = 1000;
  42. EXPECT_EQ(bandwidth_sample, mon.get_bandwidth_estimation());
  43. bandwidth_sample =
  44. static_cast<int>(bandwidth_sample * kPercentIncrease);
  45. EXPECT_FALSE(mon.Sample(9, bandwidth_sample));
  46. EXPECT_TRUE(mon.Sample(10, bandwidth_sample));
  47. EXPECT_EQ(bandwidth_sample, mon.get_bandwidth_estimation());
  48. int next_expected_est =
  49. static_cast<int>(bandwidth_sample * kPercentIncrease);
  50. bandwidth_sample *= 2;
  51. EXPECT_TRUE(mon.Sample(20, bandwidth_sample));
  52. EXPECT_EQ(next_expected_est, mon.get_bandwidth_estimation());
  53. }
  54. TEST(BandwidthSmootherTest, TestSampleIncreaseFromZero) {
  55. BandwidthSmoother mon(0, // initial_bandwidth_guess
  56. kTimeBetweenIncrease,
  57. kPercentIncrease,
  58. kSamplesCountToAverage,
  59. kMinSampleCountPercent);
  60. const int kBandwidthSample = 1000;
  61. EXPECT_EQ(0, mon.get_bandwidth_estimation());
  62. EXPECT_FALSE(mon.Sample(9, kBandwidthSample));
  63. EXPECT_TRUE(mon.Sample(10, kBandwidthSample));
  64. EXPECT_EQ(kBandwidthSample, mon.get_bandwidth_estimation());
  65. }
  66. TEST(BandwidthSmootherTest, TestSampleDecrease) {
  67. BandwidthSmoother mon(1000, // initial_bandwidth_guess
  68. kTimeBetweenIncrease,
  69. kPercentIncrease,
  70. kSamplesCountToAverage,
  71. kMinSampleCountPercent);
  72. const int kBandwidthSample = 999;
  73. EXPECT_EQ(1000, mon.get_bandwidth_estimation());
  74. EXPECT_FALSE(mon.Sample(1, kBandwidthSample));
  75. EXPECT_EQ(1000, mon.get_bandwidth_estimation());
  76. EXPECT_TRUE(mon.Sample(2, kBandwidthSample));
  77. EXPECT_EQ(kBandwidthSample, mon.get_bandwidth_estimation());
  78. }
  79. TEST(BandwidthSmootherTest, TestSampleTooFewSamples) {
  80. BandwidthSmoother mon(1000, // initial_bandwidth_guess
  81. kTimeBetweenIncrease,
  82. kPercentIncrease,
  83. 10, // 10 samples.
  84. 0.5); // 5 min samples.
  85. const int kBandwidthSample = 500;
  86. EXPECT_EQ(1000, mon.get_bandwidth_estimation());
  87. EXPECT_FALSE(mon.Sample(1, kBandwidthSample));
  88. EXPECT_FALSE(mon.Sample(2, kBandwidthSample));
  89. EXPECT_FALSE(mon.Sample(3, kBandwidthSample));
  90. EXPECT_FALSE(mon.Sample(4, kBandwidthSample));
  91. EXPECT_EQ(1000, mon.get_bandwidth_estimation());
  92. EXPECT_TRUE(mon.Sample(5, kBandwidthSample));
  93. EXPECT_EQ(kBandwidthSample, mon.get_bandwidth_estimation());
  94. }
  95. TEST(BandwidthSmootherTest, TestSampleRollover) {
  96. const int kHugeBandwidth = 2000000000; // > INT_MAX/1.1
  97. BandwidthSmoother mon(kHugeBandwidth,
  98. kTimeBetweenIncrease,
  99. kPercentIncrease,
  100. kSamplesCountToAverage,
  101. kMinSampleCountPercent);
  102. EXPECT_FALSE(mon.Sample(10, INT_MAX));
  103. EXPECT_FALSE(mon.Sample(11, INT_MAX));
  104. EXPECT_EQ(kHugeBandwidth, mon.get_bandwidth_estimation());
  105. }
  106. TEST(BandwidthSmootherTest, TestSampleNegative) {
  107. BandwidthSmoother mon(1000, // initial_bandwidth_guess
  108. kTimeBetweenIncrease,
  109. kPercentIncrease,
  110. kSamplesCountToAverage,
  111. kMinSampleCountPercent);
  112. EXPECT_FALSE(mon.Sample(10, -1));
  113. EXPECT_FALSE(mon.Sample(11, -1));
  114. EXPECT_EQ(1000, mon.get_bandwidth_estimation());
  115. }
  116. } // namespace talk_base