terelius | 5a38836 | 2016-12-09 13:50:01 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Danil Chapovalov | bda5068 | 2018-02-14 09:08:28 | [diff] [blame] | 11 | #include "modules/congestion_controller/median_slope_estimator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 12 | #include "rtc_base/random.h" |
| 13 | #include "test/gtest.h" |
terelius | 5a38836 | 2016-12-09 13:50:01 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
| 16 | |
| 17 | namespace { |
| 18 | constexpr size_t kWindowSize = 20; |
| 19 | constexpr double kGain = 1; |
| 20 | constexpr int64_t kAvgTimeBetweenPackets = 10; |
terelius | eb538fd | 2016-12-16 10:37:10 | [diff] [blame] | 21 | constexpr size_t kPacketCount = 2 * kWindowSize + 1; |
| 22 | |
| 23 | void TestEstimator(double slope, double jitter_stddev, double tolerance) { |
| 24 | MedianSlopeEstimator estimator(kWindowSize, kGain); |
| 25 | Random random(0x1234567); |
| 26 | int64_t send_times[kPacketCount]; |
| 27 | int64_t recv_times[kPacketCount]; |
| 28 | int64_t send_start_time = random.Rand(1000000); |
| 29 | int64_t recv_start_time = random.Rand(1000000); |
| 30 | for (size_t i = 0; i < kPacketCount; ++i) { |
| 31 | send_times[i] = send_start_time + i * kAvgTimeBetweenPackets; |
| 32 | double latency = i * kAvgTimeBetweenPackets / (1 - slope); |
| 33 | double jitter = random.Gaussian(0, jitter_stddev); |
| 34 | recv_times[i] = recv_start_time + latency + jitter; |
| 35 | } |
| 36 | for (size_t i = 1; i < kPacketCount; ++i) { |
| 37 | double recv_delta = recv_times[i] - recv_times[i - 1]; |
| 38 | double send_delta = send_times[i] - send_times[i - 1]; |
| 39 | estimator.Update(recv_delta, send_delta, recv_times[i]); |
| 40 | if (i < kWindowSize) |
| 41 | EXPECT_NEAR(estimator.trendline_slope(), 0, 0.001); |
| 42 | else |
| 43 | EXPECT_NEAR(estimator.trendline_slope(), slope, tolerance); |
| 44 | } |
| 45 | } |
terelius | 5a38836 | 2016-12-09 13:50:01 | [diff] [blame] | 46 | } // namespace |
| 47 | |
Sebastian Jansson | 56da2f7 | 2018-02-28 12:07:28 | [diff] [blame] | 48 | TEST(DeprecatedMedianSlopeEstimator, PerfectLineSlopeOneHalf) { |
terelius | eb538fd | 2016-12-16 10:37:10 | [diff] [blame] | 49 | TestEstimator(0.5, 0, 0.001); |
terelius | 5a38836 | 2016-12-09 13:50:01 | [diff] [blame] | 50 | } |
| 51 | |
Sebastian Jansson | 56da2f7 | 2018-02-28 12:07:28 | [diff] [blame] | 52 | TEST(DeprecatedMedianSlopeEstimator, PerfectLineSlopeMinusOne) { |
terelius | eb538fd | 2016-12-16 10:37:10 | [diff] [blame] | 53 | TestEstimator(-1, 0, 0.001); |
terelius | 5a38836 | 2016-12-09 13:50:01 | [diff] [blame] | 54 | } |
| 55 | |
Sebastian Jansson | 56da2f7 | 2018-02-28 12:07:28 | [diff] [blame] | 56 | TEST(DeprecatedMedianSlopeEstimator, PerfectLineSlopeZero) { |
terelius | eb538fd | 2016-12-16 10:37:10 | [diff] [blame] | 57 | TestEstimator(0, 0, 0.001); |
terelius | 5a38836 | 2016-12-09 13:50:01 | [diff] [blame] | 58 | } |
| 59 | |
Sebastian Jansson | 56da2f7 | 2018-02-28 12:07:28 | [diff] [blame] | 60 | TEST(DeprecatedMedianSlopeEstimator, JitteryLineSlopeOneHalf) { |
terelius | eb538fd | 2016-12-16 10:37:10 | [diff] [blame] | 61 | TestEstimator(0.5, kAvgTimeBetweenPackets / 3.0, 0.01); |
terelius | 5a38836 | 2016-12-09 13:50:01 | [diff] [blame] | 62 | } |
| 63 | |
Sebastian Jansson | 56da2f7 | 2018-02-28 12:07:28 | [diff] [blame] | 64 | TEST(DeprecatedMedianSlopeEstimator, JitteryLineSlopeMinusOne) { |
terelius | eb538fd | 2016-12-16 10:37:10 | [diff] [blame] | 65 | TestEstimator(-1, kAvgTimeBetweenPackets / 3.0, 0.05); |
terelius | 5a38836 | 2016-12-09 13:50:01 | [diff] [blame] | 66 | } |
| 67 | |
Sebastian Jansson | 56da2f7 | 2018-02-28 12:07:28 | [diff] [blame] | 68 | TEST(DeprecatedMedianSlopeEstimator, JitteryLineSlopeZero) { |
terelius | eb538fd | 2016-12-16 10:37:10 | [diff] [blame] | 69 | TestEstimator(0, kAvgTimeBetweenPackets / 3.0, 0.02); |
terelius | 5a38836 | 2016-12-09 13:50:01 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | } // namespace webrtc |