Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(496)

Side by Side Diff: webrtc/modules/congestion_controller/median_slope_estimator_unittest.cc

Issue 2578543002: Avoid precision loss in MedianSlopeEstimator from int64_t -> double conversion (Closed)
Patch Set: Move test function to anonymous namespace Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/modules/congestion_controller/median_slope_estimator.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 }
21 } // namespace 46 } // namespace
22 47
23 TEST(MedianSlopeEstimator, PerfectLineSlopeOneHalf) { 48 TEST(MedianSlopeEstimator, PerfectLineSlopeOneHalf) {
24 MedianSlopeEstimator estimator(kWindowSize, kGain); 49 TestEstimator(0.5, 0, 0.001);
25 Random rand(0x1234567);
26 double now_ms = rand.Rand<double>() * 10000;
27 for (size_t i = 1; i < 2 * kWindowSize; i++) {
28 double send_delta = rand.Rand<double>() * 2 * kAvgTimeBetweenPackets;
29 double recv_delta = 2 * send_delta;
30 now_ms += recv_delta;
31 estimator.Update(recv_delta, send_delta, now_ms);
32 if (i < kWindowSize)
33 EXPECT_NEAR(estimator.trendline_slope(), 0, 0.001);
34 else
35 EXPECT_NEAR(estimator.trendline_slope(), 0.5, 0.001);
36 }
37 } 50 }
38 51
39 TEST(MedianSlopeEstimator, PerfectLineSlopeMinusOne) { 52 TEST(MedianSlopeEstimator, PerfectLineSlopeMinusOne) {
40 MedianSlopeEstimator estimator(kWindowSize, kGain); 53 TestEstimator(-1, 0, 0.001);
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 }
53 } 54 }
54 55
55 TEST(MedianSlopeEstimator, PerfectLineSlopeZero) { 56 TEST(MedianSlopeEstimator, PerfectLineSlopeZero) {
56 MedianSlopeEstimator estimator(kWindowSize, kGain); 57 TestEstimator(0, 0, 0.001);
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 }
66 } 58 }
67 59
68 TEST(MedianSlopeEstimator, JitteryLineSlopeOneHalf) { 60 TEST(MedianSlopeEstimator, JitteryLineSlopeOneHalf) {
69 MedianSlopeEstimator estimator(kWindowSize, kGain); 61 TestEstimator(0.5, kAvgTimeBetweenPackets / 3.0, 0.01);
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 }
82 } 62 }
83 63
84 TEST(MedianSlopeEstimator, JitteryLineSlopeMinusOne) { 64 TEST(MedianSlopeEstimator, JitteryLineSlopeMinusOne) {
85 MedianSlopeEstimator estimator(kWindowSize, kGain); 65 TestEstimator(-1, kAvgTimeBetweenPackets / 3.0, 0.05);
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 }
98 } 66 }
99 67
100 TEST(MedianSlopeEstimator, JitteryLineSlopeZero) { 68 TEST(MedianSlopeEstimator, JitteryLineSlopeZero) {
101 MedianSlopeEstimator estimator(kWindowSize, kGain); 69 TestEstimator(0, kAvgTimeBetweenPackets / 3.0, 0.02);
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 }
111 } 70 }
112 71
113 } // namespace webrtc 72 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/congestion_controller/median_slope_estimator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698