blob: e44f5032b02c19fc9c088bf6bce7909b63ac2e29 [file] [log] [blame]
tereliusafaef8b2016-11-17 03:48:18 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/congestion_controller/trendline_estimator.h"
12#include "rtc_base/random.h"
13#include "test/gtest.h"
tereliusafaef8b2016-11-17 03:48:18 -080014
15namespace webrtc {
16
17namespace {
tereliusb3564ad2016-12-15 08:20:25 -080018constexpr size_t kWindowSize = 20;
tereliusafaef8b2016-11-17 03:48:18 -080019constexpr double kSmoothing = 0.0;
20constexpr double kGain = 1;
21constexpr int64_t kAvgTimeBetweenPackets = 10;
tereliusb3564ad2016-12-15 08:20:25 -080022constexpr size_t kPacketCount = 2 * kWindowSize + 1;
23
24void 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}
tereliusafaef8b2016-11-17 03:48:18 -080047} // namespace
48
terelius43c38212016-12-15 06:42:44 -080049TEST(TrendlineEstimator, PerfectLineSlopeOneHalf) {
tereliusb3564ad2016-12-15 08:20:25 -080050 TestEstimator(0.5, 0, 0.001);
tereliusafaef8b2016-11-17 03:48:18 -080051}
52
53TEST(TrendlineEstimator, PerfectLineSlopeMinusOne) {
tereliusb3564ad2016-12-15 08:20:25 -080054 TestEstimator(-1, 0, 0.001);
tereliusafaef8b2016-11-17 03:48:18 -080055}
56
57TEST(TrendlineEstimator, PerfectLineSlopeZero) {
tereliusb3564ad2016-12-15 08:20:25 -080058 TestEstimator(0, 0, 0.001);
tereliusafaef8b2016-11-17 03:48:18 -080059}
60
61TEST(TrendlineEstimator, JitteryLineSlopeOneHalf) {
tereliusb3564ad2016-12-15 08:20:25 -080062 TestEstimator(0.5, kAvgTimeBetweenPackets / 3.0, 0.01);
tereliusafaef8b2016-11-17 03:48:18 -080063}
64
65TEST(TrendlineEstimator, JitteryLineSlopeMinusOne) {
tereliusb3564ad2016-12-15 08:20:25 -080066 TestEstimator(-1, kAvgTimeBetweenPackets / 3.0, 0.075);
tereliusafaef8b2016-11-17 03:48:18 -080067}
68
69TEST(TrendlineEstimator, JitteryLineSlopeZero) {
tereliusb3564ad2016-12-15 08:20:25 -080070 TestEstimator(0, kAvgTimeBetweenPackets / 3.0, 0.02);
tereliusafaef8b2016-11-17 03:48:18 -080071}
72
73} // namespace webrtc