utility_function_unittest.cc (4497B)
1 /* 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 11 #include "modules/congestion_controller/pcc/utility_function.h" 12 13 #include <cmath> 14 #include <cstddef> 15 #include <vector> 16 17 #include "api/transport/network_types.h" 18 #include "api/units/data_rate.h" 19 #include "api/units/data_size.h" 20 #include "api/units/time_delta.h" 21 #include "api/units/timestamp.h" 22 #include "modules/congestion_controller/pcc/monitor_interval.h" 23 #include "test/gtest.h" 24 25 namespace webrtc { 26 namespace pcc { 27 namespace test { 28 namespace { 29 constexpr double kLossCoefficient = 11.35; 30 constexpr double kThroughputPower = 0.9; 31 constexpr double kThroughputCoefficient = 1; 32 constexpr double kDelayGradientNegativeBound = 10; 33 34 constexpr Timestamp kStartTime = Timestamp::Micros(0); 35 constexpr TimeDelta kPacketsDelta = TimeDelta::Millis(1); 36 constexpr TimeDelta kIntervalDuration = TimeDelta::Millis(100); 37 constexpr DataRate kSendingBitrate = DataRate::BitsPerSec(1000); 38 39 constexpr DataSize kDefaultDataSize = DataSize::Bytes(100); 40 constexpr TimeDelta kDefaultDelay = TimeDelta::Millis(100); 41 42 std::vector<PacketResult> CreatePacketResults( 43 const std::vector<Timestamp>& packets_send_times, 44 const std::vector<Timestamp>& packets_received_times = {}, 45 const std::vector<DataSize>& packets_sizes = {}) { 46 std::vector<PacketResult> packet_results; 47 PacketResult packet_result; 48 SentPacket sent_packet; 49 for (size_t i = 0; i < packets_send_times.size(); ++i) { 50 sent_packet.send_time = packets_send_times[i]; 51 if (packets_sizes.empty()) { 52 sent_packet.size = kDefaultDataSize; 53 } else { 54 sent_packet.size = packets_sizes[i]; 55 } 56 packet_result.sent_packet = sent_packet; 57 if (packets_received_times.empty()) { 58 packet_result.receive_time = packets_send_times[i] + kDefaultDelay; 59 } else { 60 packet_result.receive_time = packets_received_times[i]; 61 } 62 packet_results.push_back(packet_result); 63 } 64 return packet_results; 65 } 66 67 } // namespace 68 69 TEST(PccVivaceUtilityFunctionTest, 70 UtilityIsThroughputTermIfAllRestCoefficientsAreZero) { 71 VivaceUtilityFunction utility_function(0, 0, kThroughputCoefficient, 72 kThroughputPower, 0, 73 kDelayGradientNegativeBound); 74 PccMonitorInterval monitor_interval(kSendingBitrate, kStartTime, 75 kIntervalDuration); 76 monitor_interval.OnPacketsFeedback(CreatePacketResults( 77 {kStartTime + kPacketsDelta, kStartTime + 2 * kPacketsDelta, 78 kStartTime + 3 * kPacketsDelta, kStartTime + 2 * kIntervalDuration}, 79 {kStartTime + kPacketsDelta + kDefaultDelay, Timestamp::PlusInfinity(), 80 kStartTime + kDefaultDelay + 3 * kPacketsDelta, 81 Timestamp::PlusInfinity()}, 82 {kDefaultDataSize, kDefaultDataSize, kDefaultDataSize, 83 kDefaultDataSize})); 84 EXPECT_DOUBLE_EQ(utility_function.Compute(monitor_interval), 85 kThroughputCoefficient * 86 std::pow(kSendingBitrate.bps(), kThroughputPower)); 87 } 88 89 TEST(PccVivaceUtilityFunctionTest, 90 LossTermIsNonZeroIfLossCoefficientIsNonZero) { 91 VivaceUtilityFunction utility_function( 92 0, kLossCoefficient, kThroughputCoefficient, kThroughputPower, 0, 93 kDelayGradientNegativeBound); 94 PccMonitorInterval monitor_interval(kSendingBitrate, kStartTime, 95 kIntervalDuration); 96 monitor_interval.OnPacketsFeedback(CreatePacketResults( 97 {kStartTime + kPacketsDelta, kStartTime + 2 * kPacketsDelta, 98 kStartTime + 5 * kPacketsDelta, kStartTime + 2 * kIntervalDuration}, 99 {kStartTime + kDefaultDelay, Timestamp::PlusInfinity(), 100 kStartTime + kDefaultDelay, kStartTime + 3 * kIntervalDuration}, 101 {})); 102 // The second packet was lost. 103 EXPECT_DOUBLE_EQ(utility_function.Compute(monitor_interval), 104 kThroughputCoefficient * 105 std::pow(kSendingBitrate.bps(), kThroughputPower) - 106 kLossCoefficient * kSendingBitrate.bps() * 107 monitor_interval.GetLossRate()); 108 } 109 110 } // namespace test 111 } // namespace pcc 112 } // namespace webrtc