blob: 0459fb847bb84867474ae1013896b3fb386d25fb [file] [log] [blame]
Anastasia Koloskovaddbbf462018-08-21 16:12:31 +02001/*
2 * Copyright (c) 2018 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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "modules/congestion_controller/pcc/utility_function.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <cmath>
16#include <type_traits>
Anastasia Koloskovaddbbf462018-08-21 16:12:31 +020017#include <vector>
18
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "api/transport/network_types.h"
20#include "api/units/data_rate.h"
21#include "api/units/data_size.h"
22#include "api/units/time_delta.h"
23#include "api/units/timestamp.h"
Anastasia Koloskovaddbbf462018-08-21 16:12:31 +020024#include "test/gtest.h"
25
26namespace webrtc {
27namespace pcc {
28namespace test {
29namespace {
30constexpr double kLossCoefficient = 11.35;
31constexpr double kThroughputPower = 0.9;
32constexpr double kThroughputCoefficient = 1;
33constexpr double kDelayGradientNegativeBound = 10;
34
35const Timestamp kStartTime = Timestamp::us(0);
36const TimeDelta kPacketsDelta = TimeDelta::ms(1);
37const TimeDelta kIntervalDuration = TimeDelta::ms(100);
38const DataRate kSendingBitrate = DataRate::bps(1000);
39
40const DataSize kDefaultDataSize = DataSize::bytes(100);
41const TimeDelta kDefaultDelay = TimeDelta::ms(100);
42
43std::vector<PacketResult> CreatePacketResults(
44 const std::vector<Timestamp>& packets_send_times,
45 const std::vector<Timestamp>& packets_received_times = {},
46 const std::vector<DataSize>& packets_sizes = {}) {
47 std::vector<PacketResult> packet_results;
48 PacketResult packet_result;
49 SentPacket sent_packet;
50 for (size_t i = 0; i < packets_send_times.size(); ++i) {
51 sent_packet.send_time = packets_send_times[i];
52 if (packets_sizes.empty()) {
53 sent_packet.size = kDefaultDataSize;
54 } else {
55 sent_packet.size = packets_sizes[i];
56 }
57 packet_result.sent_packet = sent_packet;
58 if (packets_received_times.empty()) {
59 packet_result.receive_time = packets_send_times[i] + kDefaultDelay;
60 } else {
61 packet_result.receive_time = packets_received_times[i];
62 }
63 packet_results.push_back(packet_result);
64 }
65 return packet_results;
66}
67
68} // namespace
69
70TEST(PccVivaceUtilityFunctionTest,
71 UtilityIsThroughputTermIfAllRestCoefficientsAreZero) {
72 VivaceUtilityFunction utility_function(0, 0, kThroughputCoefficient,
73 kThroughputPower, 0,
74 kDelayGradientNegativeBound);
75 PccMonitorInterval monitor_interval(kSendingBitrate, kStartTime,
76 kIntervalDuration);
77 monitor_interval.OnPacketsFeedback(CreatePacketResults(
78 {kStartTime + kPacketsDelta, kStartTime + 2 * kPacketsDelta,
79 kStartTime + 3 * kPacketsDelta, kStartTime + 2 * kIntervalDuration},
Sebastian Jansson9de4ef42018-09-04 17:32:36 +020080 {kStartTime + kPacketsDelta + kDefaultDelay, Timestamp::PlusInfinity(),
81 kStartTime + kDefaultDelay + 3 * kPacketsDelta,
82 Timestamp::PlusInfinity()},
Anastasia Koloskovaddbbf462018-08-21 16:12:31 +020083 {kDefaultDataSize, kDefaultDataSize, kDefaultDataSize,
84 kDefaultDataSize}));
85 EXPECT_DOUBLE_EQ(utility_function.Compute(monitor_interval),
86 kThroughputCoefficient *
87 std::pow(kSendingBitrate.bps(), kThroughputPower));
88}
89
90TEST(PccVivaceUtilityFunctionTest,
91 LossTermIsNonZeroIfLossCoefficientIsNonZero) {
92 VivaceUtilityFunction utility_function(
93 0, kLossCoefficient, kThroughputCoefficient, kThroughputPower, 0,
94 kDelayGradientNegativeBound);
95 PccMonitorInterval monitor_interval(kSendingBitrate, kStartTime,
96 kIntervalDuration);
97 monitor_interval.OnPacketsFeedback(CreatePacketResults(
98 {kStartTime + kPacketsDelta, kStartTime + 2 * kPacketsDelta,
99 kStartTime + 5 * kPacketsDelta, kStartTime + 2 * kIntervalDuration},
Sebastian Jansson9de4ef42018-09-04 17:32:36 +0200100 {kStartTime + kDefaultDelay, Timestamp::PlusInfinity(),
Anastasia Koloskovaddbbf462018-08-21 16:12:31 +0200101 kStartTime + kDefaultDelay, kStartTime + 3 * kIntervalDuration},
102 {}));
103 // The second packet was lost.
104 EXPECT_DOUBLE_EQ(utility_function.Compute(monitor_interval),
105 kThroughputCoefficient *
106 std::pow(kSendingBitrate.bps(), kThroughputPower) -
107 kLossCoefficient * kSendingBitrate.bps() *
108 monitor_interval.GetLossRate());
109}
110
111} // namespace test
112} // namespace pcc
113} // namespace webrtc