terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/congestion_controller/trendline_estimator.h" |
| 12 | #include "rtc_base/random.h" |
| 13 | #include "test/gtest.h" |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
| 16 | |
| 17 | namespace { |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 18 | constexpr size_t kWindowSize = 20; |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 19 | constexpr double kSmoothing = 0.0; |
| 20 | constexpr double kGain = 1; |
| 21 | constexpr int64_t kAvgTimeBetweenPackets = 10; |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 22 | constexpr size_t kPacketCount = 2 * kWindowSize + 1; |
| 23 | |
| 24 | void TestEstimator(double slope, double jitter_stddev, double tolerance) { |
| 25 | TrendlineEstimator estimator(kWindowSize, kSmoothing, kGain); |
| 26 | Random random(0x1234567); |
| 27 | int64_t send_times[kPacketCount]; |
| 28 | int64_t recv_times[kPacketCount]; |
| 29 | int64_t send_start_time = random.Rand(1000000); |
| 30 | int64_t recv_start_time = random.Rand(1000000); |
| 31 | for (size_t i = 0; i < kPacketCount; ++i) { |
| 32 | send_times[i] = send_start_time + i * kAvgTimeBetweenPackets; |
| 33 | double latency = i * kAvgTimeBetweenPackets / (1 - slope); |
| 34 | double jitter = random.Gaussian(0, jitter_stddev); |
| 35 | recv_times[i] = recv_start_time + latency + jitter; |
| 36 | } |
| 37 | for (size_t i = 1; i < kPacketCount; ++i) { |
| 38 | double recv_delta = recv_times[i] - recv_times[i - 1]; |
| 39 | double send_delta = send_times[i] - send_times[i - 1]; |
| 40 | estimator.Update(recv_delta, send_delta, recv_times[i]); |
| 41 | if (i < kWindowSize) |
| 42 | EXPECT_NEAR(estimator.trendline_slope(), 0, 0.001); |
| 43 | else |
| 44 | EXPECT_NEAR(estimator.trendline_slope(), slope, tolerance); |
| 45 | } |
| 46 | } |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 47 | } // namespace |
| 48 | |
terelius | 43c3821 | 2016-12-15 06:42:44 -0800 | [diff] [blame] | 49 | TEST(TrendlineEstimator, PerfectLineSlopeOneHalf) { |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 50 | TestEstimator(0.5, 0, 0.001); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | TEST(TrendlineEstimator, PerfectLineSlopeMinusOne) { |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 54 | TestEstimator(-1, 0, 0.001); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | TEST(TrendlineEstimator, PerfectLineSlopeZero) { |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 58 | TestEstimator(0, 0, 0.001); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TEST(TrendlineEstimator, JitteryLineSlopeOneHalf) { |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 62 | TestEstimator(0.5, kAvgTimeBetweenPackets / 3.0, 0.01); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | TEST(TrendlineEstimator, JitteryLineSlopeMinusOne) { |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 66 | TestEstimator(-1, kAvgTimeBetweenPackets / 3.0, 0.075); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | TEST(TrendlineEstimator, JitteryLineSlopeZero) { |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 70 | TestEstimator(0, kAvgTimeBetweenPackets / 3.0, 0.02); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | } // namespace webrtc |