OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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/test/gtest.h" | 11 #include "webrtc/test/gtest.h" |
12 #include "webrtc/base/random.h" | 12 #include "webrtc/base/random.h" |
13 #include "webrtc/modules/congestion_controller/median_slope_estimator.h" | 13 #include "webrtc/modules/congestion_controller/median_slope_estimator.h" |
14 | 14 |
15 namespace webrtc { | 15 namespace webrtc { |
16 | 16 |
17 namespace { | 17 namespace { |
18 constexpr size_t kWindowSize = 20; | 18 constexpr size_t kWindowSize = 20; |
19 constexpr double kGain = 1; | 19 constexpr double kGain = 1; |
20 constexpr int64_t kAvgTimeBetweenPackets = 10; | 20 constexpr int64_t kAvgTimeBetweenPackets = 10; |
21 constexpr size_t kPacketCount = 2 * kWindowSize + 1; | |
22 } // namespace | 21 } // namespace |
23 | 22 |
24 void TestEstimator(double slope, double jitter_stddev, double tolerance) { | 23 TEST(MedianSlopeEstimator, PerfectLineSlopeOneHalf) { |
25 MedianSlopeEstimator estimator(kWindowSize, kGain); | 24 MedianSlopeEstimator estimator(kWindowSize, kGain); |
26 Random random(0x1234567); | 25 Random rand(0x1234567); |
27 int64_t send_times[kPacketCount]; | 26 double now_ms = rand.Rand<double>() * 10000; |
28 int64_t recv_times[kPacketCount]; | 27 for (size_t i = 1; i < 2 * kWindowSize; i++) { |
29 int64_t send_start_time = random.Rand(1000000); | 28 double send_delta = rand.Rand<double>() * 2 * kAvgTimeBetweenPackets; |
30 int64_t recv_start_time = random.Rand(1000000); | 29 double recv_delta = 2 * send_delta; |
31 for (size_t i = 0; i < kPacketCount; ++i) { | 30 now_ms += recv_delta; |
32 send_times[i] = send_start_time + i * kAvgTimeBetweenPackets; | 31 estimator.Update(recv_delta, send_delta, now_ms); |
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) | 32 if (i < kWindowSize) |
42 EXPECT_NEAR(estimator.trendline_slope(), 0, 0.001); | 33 EXPECT_NEAR(estimator.trendline_slope(), 0, 0.001); |
43 else | 34 else |
44 EXPECT_NEAR(estimator.trendline_slope(), slope, tolerance); | 35 EXPECT_NEAR(estimator.trendline_slope(), 0.5, 0.001); |
45 } | 36 } |
46 } | 37 } |
47 | 38 |
48 TEST(MedianSlopeEstimator, PerfectLineSlopeOneHalf) { | |
49 TestEstimator(0.5, 0, 0.001); | |
50 } | |
51 | |
52 TEST(MedianSlopeEstimator, PerfectLineSlopeMinusOne) { | 39 TEST(MedianSlopeEstimator, PerfectLineSlopeMinusOne) { |
53 TestEstimator(-1, 0, 0.001); | 40 MedianSlopeEstimator estimator(kWindowSize, kGain); |
| 41 Random rand(0x1234567); |
| 42 double now_ms = rand.Rand<double>() * 10000; |
| 43 for (size_t i = 1; i < 2 * kWindowSize; i++) { |
| 44 double send_delta = rand.Rand<double>() * 2 * kAvgTimeBetweenPackets; |
| 45 double recv_delta = 0.5 * send_delta; |
| 46 now_ms += recv_delta; |
| 47 estimator.Update(recv_delta, send_delta, now_ms); |
| 48 if (i < kWindowSize) |
| 49 EXPECT_NEAR(estimator.trendline_slope(), 0, 0.001); |
| 50 else |
| 51 EXPECT_NEAR(estimator.trendline_slope(), -1, 0.001); |
| 52 } |
54 } | 53 } |
55 | 54 |
56 TEST(MedianSlopeEstimator, PerfectLineSlopeZero) { | 55 TEST(MedianSlopeEstimator, PerfectLineSlopeZero) { |
57 TestEstimator(0, 0, 0.001); | 56 MedianSlopeEstimator estimator(kWindowSize, kGain); |
| 57 Random rand(0x1234567); |
| 58 double now_ms = rand.Rand<double>() * 10000; |
| 59 for (size_t i = 1; i < 2 * kWindowSize; i++) { |
| 60 double send_delta = rand.Rand<double>() * 2 * kAvgTimeBetweenPackets; |
| 61 double recv_delta = send_delta; |
| 62 now_ms += recv_delta; |
| 63 estimator.Update(recv_delta, send_delta, now_ms); |
| 64 EXPECT_NEAR(estimator.trendline_slope(), 0, 0.001); |
| 65 } |
58 } | 66 } |
59 | 67 |
60 TEST(MedianSlopeEstimator, JitteryLineSlopeOneHalf) { | 68 TEST(MedianSlopeEstimator, JitteryLineSlopeOneHalf) { |
61 TestEstimator(0.5, kAvgTimeBetweenPackets / 3.0, 0.01); | 69 MedianSlopeEstimator estimator(kWindowSize, kGain); |
| 70 Random rand(0x1234567); |
| 71 double now_ms = rand.Rand<double>() * 10000; |
| 72 for (size_t i = 1; i < 2 * kWindowSize; i++) { |
| 73 double send_delta = rand.Rand<double>() * 2 * kAvgTimeBetweenPackets; |
| 74 double recv_delta = 2 * send_delta + rand.Gaussian(0, send_delta / 3); |
| 75 now_ms += recv_delta; |
| 76 estimator.Update(recv_delta, send_delta, now_ms); |
| 77 if (i < kWindowSize) |
| 78 EXPECT_NEAR(estimator.trendline_slope(), 0, 0.001); |
| 79 else |
| 80 EXPECT_NEAR(estimator.trendline_slope(), 0.5, 0.1); |
| 81 } |
62 } | 82 } |
63 | 83 |
64 TEST(MedianSlopeEstimator, JitteryLineSlopeMinusOne) { | 84 TEST(MedianSlopeEstimator, JitteryLineSlopeMinusOne) { |
65 TestEstimator(-1, kAvgTimeBetweenPackets / 3.0, 0.05); | 85 MedianSlopeEstimator estimator(kWindowSize, kGain); |
| 86 Random rand(0x1234567); |
| 87 double now_ms = rand.Rand<double>() * 10000; |
| 88 for (size_t i = 1; i < 2 * kWindowSize; i++) { |
| 89 double send_delta = rand.Rand<double>() * 2 * kAvgTimeBetweenPackets; |
| 90 double recv_delta = 0.5 * send_delta + rand.Gaussian(0, send_delta / 20); |
| 91 now_ms += recv_delta; |
| 92 estimator.Update(recv_delta, send_delta, now_ms); |
| 93 if (i < kWindowSize) |
| 94 EXPECT_NEAR(estimator.trendline_slope(), 0, 0.001); |
| 95 else |
| 96 EXPECT_NEAR(estimator.trendline_slope(), -1, 0.1); |
| 97 } |
66 } | 98 } |
67 | 99 |
68 TEST(MedianSlopeEstimator, JitteryLineSlopeZero) { | 100 TEST(MedianSlopeEstimator, JitteryLineSlopeZero) { |
69 TestEstimator(0, kAvgTimeBetweenPackets / 3.0, 0.02); | 101 MedianSlopeEstimator estimator(kWindowSize, kGain); |
| 102 Random rand(0x1234567); |
| 103 double now_ms = rand.Rand<double>() * 10000; |
| 104 for (size_t i = 1; i < 2 * kWindowSize; i++) { |
| 105 double send_delta = rand.Rand<double>() * 2 * kAvgTimeBetweenPackets; |
| 106 double recv_delta = send_delta + rand.Gaussian(0, send_delta / 5); |
| 107 now_ms += recv_delta; |
| 108 estimator.Update(recv_delta, send_delta, now_ms); |
| 109 EXPECT_NEAR(estimator.trendline_slope(), 0, 0.1); |
| 110 } |
70 } | 111 } |
71 | 112 |
72 } // namespace webrtc | 113 } // namespace webrtc |
OLD | NEW |