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