blob: 953f8e11fb75de3dbeec4dadbe208a26e493f4b3 [file] [log] [blame]
terelius5a388362016-12-09 13:50:011/*
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 Chapovalovbda50682018-02-14 09:08:2811#include "modules/congestion_controller/median_slope_estimator.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3112#include "rtc_base/random.h"
13#include "test/gtest.h"
terelius5a388362016-12-09 13:50:0114
15namespace webrtc {
16
17namespace {
18constexpr size_t kWindowSize = 20;
19constexpr double kGain = 1;
20constexpr int64_t kAvgTimeBetweenPackets = 10;
tereliuseb538fd2016-12-16 10:37:1021constexpr size_t kPacketCount = 2 * kWindowSize + 1;
22
23void 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}
terelius5a388362016-12-09 13:50:0146} // namespace
47
Sebastian Jansson56da2f72018-02-28 12:07:2848TEST(DeprecatedMedianSlopeEstimator, PerfectLineSlopeOneHalf) {
tereliuseb538fd2016-12-16 10:37:1049 TestEstimator(0.5, 0, 0.001);
terelius5a388362016-12-09 13:50:0150}
51
Sebastian Jansson56da2f72018-02-28 12:07:2852TEST(DeprecatedMedianSlopeEstimator, PerfectLineSlopeMinusOne) {
tereliuseb538fd2016-12-16 10:37:1053 TestEstimator(-1, 0, 0.001);
terelius5a388362016-12-09 13:50:0154}
55
Sebastian Jansson56da2f72018-02-28 12:07:2856TEST(DeprecatedMedianSlopeEstimator, PerfectLineSlopeZero) {
tereliuseb538fd2016-12-16 10:37:1057 TestEstimator(0, 0, 0.001);
terelius5a388362016-12-09 13:50:0158}
59
Sebastian Jansson56da2f72018-02-28 12:07:2860TEST(DeprecatedMedianSlopeEstimator, JitteryLineSlopeOneHalf) {
tereliuseb538fd2016-12-16 10:37:1061 TestEstimator(0.5, kAvgTimeBetweenPackets / 3.0, 0.01);
terelius5a388362016-12-09 13:50:0162}
63
Sebastian Jansson56da2f72018-02-28 12:07:2864TEST(DeprecatedMedianSlopeEstimator, JitteryLineSlopeMinusOne) {
tereliuseb538fd2016-12-16 10:37:1065 TestEstimator(-1, kAvgTimeBetweenPackets / 3.0, 0.05);
terelius5a388362016-12-09 13:50:0166}
67
Sebastian Jansson56da2f72018-02-28 12:07:2868TEST(DeprecatedMedianSlopeEstimator, JitteryLineSlopeZero) {
tereliuseb538fd2016-12-16 10:37:1069 TestEstimator(0, kAvgTimeBetweenPackets / 3.0, 0.02);
terelius5a388362016-12-09 13:50:0170}
71
72} // namespace webrtc