This source file includes following definitions.
- congestion_window
- acked_sequence_number_
- SendAvailableSendWindow
- UpdateRtt
- AckNPackets
- LoseNPackets
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <algorithm>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "net/quic/congestion_control/rtt_stats.h"
#include "net/quic/congestion_control/tcp_cubic_sender.h"
#include "net/quic/congestion_control/tcp_receiver.h"
#include "net/quic/quic_utils.h"
#include "net/quic/test_tools/mock_clock.h"
#include "testing/gtest/include/gtest/gtest.h"
using std::min;
namespace net {
namespace test {
const uint32 kDefaultWindowTCP = 10 * kDefaultTCPMSS;
const QuicTcpCongestionWindow kDefaultMaxCongestionWindowTCP = 10000;
class TcpCubicSenderPeer : public TcpCubicSender {
public:
TcpCubicSenderPeer(const QuicClock* clock,
bool reno,
QuicTcpCongestionWindow max_tcp_congestion_window)
: TcpCubicSender(
clock, &rtt_stats_, reno, max_tcp_congestion_window, &stats_) {
}
QuicTcpCongestionWindow congestion_window() {
return congestion_window_;
}
RttStats rtt_stats_;
QuicConnectionStats stats_;
using TcpCubicSender::AvailableSendWindow;
using TcpCubicSender::SendWindow;
};
class TcpCubicSenderTest : public ::testing::Test {
protected:
TcpCubicSenderTest()
: one_ms_(QuicTime::Delta::FromMilliseconds(1)),
sender_(new TcpCubicSenderPeer(&clock_, true,
kDefaultMaxCongestionWindowTCP)),
receiver_(new TcpReceiver()),
sequence_number_(1),
acked_sequence_number_(0) {
}
int SendAvailableSendWindow() {
int packets_sent = 0;
bool can_send = sender_->TimeUntilSend(
clock_.Now(), HAS_RETRANSMITTABLE_DATA).IsZero();
while (can_send) {
sender_->OnPacketSent(clock_.Now(), sequence_number_++, kDefaultTCPMSS,
HAS_RETRANSMITTABLE_DATA);
++packets_sent;
can_send = sender_->TimeUntilSend(
clock_.Now(), HAS_RETRANSMITTABLE_DATA).IsZero();
}
return packets_sent;
}
void UpdateRtt(QuicTime::Delta rtt) {
sender_->rtt_stats_.UpdateRtt(rtt, QuicTime::Delta::Zero());
sender_->UpdateRtt(rtt);
}
void AckNPackets(int n) {
for (int i = 0; i < n; ++i) {
++acked_sequence_number_;
UpdateRtt(QuicTime::Delta::FromMilliseconds(60));
sender_->OnPacketAcked(acked_sequence_number_, kDefaultTCPMSS);
}
clock_.AdvanceTime(one_ms_);
}
void LoseNPackets(int n) {
for (int i = 0; i < n; ++i) {
++acked_sequence_number_;
sender_->OnPacketAbandoned(acked_sequence_number_, kDefaultTCPMSS);
sender_->OnPacketLost(acked_sequence_number_, clock_.Now());
}
}
const QuicTime::Delta one_ms_;
MockClock clock_;
scoped_ptr<TcpCubicSenderPeer> sender_;
scoped_ptr<TcpReceiver> receiver_;
QuicPacketSequenceNumber sequence_number_;
QuicPacketSequenceNumber acked_sequence_number_;
};
TEST_F(TcpCubicSenderTest, SimpleSender) {
QuicCongestionFeedbackFrame feedback;
EXPECT_EQ(kDefaultWindowTCP, sender_->AvailableSendWindow());
EXPECT_EQ(kDefaultWindowTCP, sender_->GetCongestionWindow());
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now());
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
EXPECT_EQ(kDefaultWindowTCP, sender_->AvailableSendWindow());
EXPECT_EQ(kDefaultWindowTCP, sender_->GetCongestionWindow());
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
SendAvailableSendWindow();
EXPECT_FALSE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
}
TEST_F(TcpCubicSenderTest, ExponentialSlowStart) {
const int kNumberOfAcks = 20;
QuicCongestionFeedbackFrame feedback;
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now());
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
for (int i = 0; i < kNumberOfAcks; ++i) {
SendAvailableSendWindow();
AckNPackets(2);
}
QuicByteCount bytes_to_send = sender_->SendWindow();
EXPECT_EQ(kDefaultWindowTCP + kDefaultTCPMSS * 2 * kNumberOfAcks,
bytes_to_send);
}
TEST_F(TcpCubicSenderTest, SlowStartAckTrain) {
const int kNumberOfAcks = 65;
QuicCongestionFeedbackFrame feedback;
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now());
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
for (int i = 0; i < kNumberOfAcks; ++i) {
SendAvailableSendWindow();
AckNPackets(2);
}
QuicByteCount expected_send_window =
kDefaultWindowTCP + (kDefaultTCPMSS * 2 * kNumberOfAcks);
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
for (int i = 0; i < 69; ++i) {
SendAvailableSendWindow();
AckNPackets(2);
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
SendAvailableSendWindow();
AckNPackets(2);
expected_send_window += kDefaultTCPMSS;
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
TEST_F(TcpCubicSenderTest, SlowStartPacketLoss) {
QuicCongestionFeedbackFrame feedback;
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now());
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
const int kNumberOfAcks = 10;
for (int i = 0; i < kNumberOfAcks; ++i) {
SendAvailableSendWindow();
AckNPackets(2);
}
SendAvailableSendWindow();
QuicByteCount expected_send_window = kDefaultWindowTCP +
(kDefaultTCPMSS * 2 * kNumberOfAcks);
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
sender_->OnPacketLost(acked_sequence_number_ + 1, clock_.Now());
++acked_sequence_number_;
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
expected_send_window /= 2;
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
size_t number_of_packets_in_window = expected_send_window / kDefaultTCPMSS;
AckNPackets(number_of_packets_in_window);
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
EXPECT_EQ(0u, sender_->AvailableSendWindow());
for (size_t i = 0; i < number_of_packets_in_window; ++i) {
AckNPackets(1);
SendAvailableSendWindow();
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
for (size_t i = 0; i < number_of_packets_in_window - 2; ++i) {
AckNPackets(1);
SendAvailableSendWindow();
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
AckNPackets(1);
expected_send_window += kDefaultTCPMSS;
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
TEST_F(TcpCubicSenderTest, SlowStartPacketLossPRR) {
QuicCongestionFeedbackFrame feedback;
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now());
const int kNumberOfAcks = 5;
for (int i = 0; i < kNumberOfAcks; ++i) {
SendAvailableSendWindow();
AckNPackets(2);
}
SendAvailableSendWindow();
QuicByteCount expected_send_window = kDefaultWindowTCP +
(kDefaultTCPMSS * 2 * kNumberOfAcks);
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
LoseNPackets(1);
expected_send_window /= 2;
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
EXPECT_EQ(1, SendAvailableSendWindow());
size_t number_of_packets_in_window = expected_send_window / kDefaultTCPMSS;
size_t remaining_packets_in_recovery = number_of_packets_in_window * 2 - 1;
for (size_t i = 0; i < remaining_packets_in_recovery - 1; i += 2) {
AckNPackets(2);
EXPECT_TRUE(sender_->TimeUntilSend(
clock_.Now(), HAS_RETRANSMITTABLE_DATA).IsZero());
EXPECT_EQ(0u, sender_->AvailableSendWindow());
EXPECT_EQ(1, SendAvailableSendWindow());
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
if (remaining_packets_in_recovery % 2 == 1) {
AckNPackets(1);
}
for (size_t i = 0; i < number_of_packets_in_window - 1; ++i) {
AckNPackets(1);
EXPECT_EQ(1, SendAvailableSendWindow());
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
AckNPackets(1);
expected_send_window += kDefaultTCPMSS;
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
TEST_F(TcpCubicSenderTest, SlowStartBurstPacketLossPRR) {
QuicCongestionFeedbackFrame feedback;
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now());
const int kNumberOfAcks = 5;
for (int i = 0; i < kNumberOfAcks; ++i) {
SendAvailableSendWindow();
AckNPackets(2);
}
SendAvailableSendWindow();
QuicByteCount expected_send_window = kDefaultWindowTCP +
(kDefaultTCPMSS * 2 * kNumberOfAcks);
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
sender_->OnPacketAcked(acked_sequence_number_ + 15, kDefaultTCPMSS);
LoseNPackets(13);
expected_send_window /= 2;
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
EXPECT_EQ(2, SendAvailableSendWindow());
sender_->OnPacketAcked(acked_sequence_number_ + 4, kDefaultTCPMSS);
LoseNPackets(1);
EXPECT_EQ(2, SendAvailableSendWindow());
sender_->OnPacketAcked(acked_sequence_number_ + 4, kDefaultTCPMSS);
LoseNPackets(1);
EXPECT_EQ(2, SendAvailableSendWindow());
AckNPackets(1);
EXPECT_EQ(2, SendAvailableSendWindow());
AckNPackets(1);
EXPECT_EQ(2, SendAvailableSendWindow());
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
for (int i = 0; i < kNumberOfAcks; ++i) {
AckNPackets(1);
EXPECT_EQ(1, SendAvailableSendWindow());
}
}
TEST_F(TcpCubicSenderTest, RTOCongestionWindow) {
EXPECT_EQ(kDefaultWindowTCP, sender_->SendWindow());
sender_->OnRetransmissionTimeout(true);
EXPECT_EQ(2 * kDefaultTCPMSS, sender_->SendWindow());
}
TEST_F(TcpCubicSenderTest, RTOCongestionWindowNoRetransmission) {
EXPECT_EQ(kDefaultWindowTCP, sender_->SendWindow());
sender_->OnRetransmissionTimeout(false);
EXPECT_EQ(kDefaultWindowTCP, sender_->SendWindow());
}
TEST_F(TcpCubicSenderTest, RetransmissionDelay) {
const int64 kRttMs = 10;
const int64 kDeviationMs = 3;
EXPECT_EQ(QuicTime::Delta::Zero(), sender_->RetransmissionDelay());
UpdateRtt(QuicTime::Delta::FromMilliseconds(kRttMs));
QuicTime::Delta expected_delay =
QuicTime::Delta::FromMilliseconds(kRttMs + kRttMs / 2 * 4);
EXPECT_EQ(expected_delay, sender_->RetransmissionDelay());
for (int i = 0; i < 100; ++i) {
UpdateRtt(QuicTime::Delta::FromMilliseconds(kRttMs + kDeviationMs));
UpdateRtt(QuicTime::Delta::FromMilliseconds(kRttMs - kDeviationMs));
}
expected_delay = QuicTime::Delta::FromMilliseconds(kRttMs + kDeviationMs * 4);
EXPECT_NEAR(kRttMs, sender_->rtt_stats_.SmoothedRtt().ToMilliseconds(), 1);
EXPECT_NEAR(expected_delay.ToMilliseconds(),
sender_->RetransmissionDelay().ToMilliseconds(),
1);
EXPECT_EQ(static_cast<int64>(
sender_->GetCongestionWindow() * kNumMicrosPerSecond /
sender_->rtt_stats_.SmoothedRtt().ToMicroseconds()),
sender_->BandwidthEstimate().ToBytesPerSecond());
}
TEST_F(TcpCubicSenderTest, SlowStartMaxSendWindow) {
const QuicTcpCongestionWindow kMaxCongestionWindowTCP = 50;
const int kNumberOfAcks = 100;
sender_.reset(
new TcpCubicSenderPeer(&clock_, false, kMaxCongestionWindowTCP));
QuicCongestionFeedbackFrame feedback;
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now());
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
for (int i = 0; i < kNumberOfAcks; ++i) {
SendAvailableSendWindow();
AckNPackets(2);
}
QuicByteCount expected_send_window =
kMaxCongestionWindowTCP * kDefaultTCPMSS;
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
TEST_F(TcpCubicSenderTest, TcpRenoMaxCongestionWindow) {
const QuicTcpCongestionWindow kMaxCongestionWindowTCP = 50;
const int kNumberOfAcks = 1000;
sender_.reset(
new TcpCubicSenderPeer(&clock_, true, kMaxCongestionWindowTCP));
QuicCongestionFeedbackFrame feedback;
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now());
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
SendAvailableSendWindow();
AckNPackets(2);
sender_->OnPacketLost(acked_sequence_number_ + 1, clock_.Now());
for (int i = 0; i < kNumberOfAcks; ++i) {
SendAvailableSendWindow();
AckNPackets(2);
}
QuicByteCount expected_send_window =
kMaxCongestionWindowTCP * kDefaultTCPMSS;
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
TEST_F(TcpCubicSenderTest, TcpCubicMaxCongestionWindow) {
const QuicTcpCongestionWindow kMaxCongestionWindowTCP = 50;
const int kNumberOfAcks = 10000;
sender_.reset(
new TcpCubicSenderPeer(&clock_, false, kMaxCongestionWindowTCP));
QuicCongestionFeedbackFrame feedback;
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now());
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
SendAvailableSendWindow();
AckNPackets(2);
sender_->OnPacketLost(acked_sequence_number_ + 1, clock_.Now());
for (int i = 0; i < kNumberOfAcks; ++i) {
SendAvailableSendWindow();
AckNPackets(2);
}
QuicByteCount expected_send_window =
kMaxCongestionWindowTCP * kDefaultTCPMSS;
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
TEST_F(TcpCubicSenderTest, MultipleLossesInOneWindow) {
SendAvailableSendWindow();
const QuicByteCount initial_window = sender_->GetCongestionWindow();
sender_->OnPacketLost(acked_sequence_number_ + 1, clock_.Now());
const QuicByteCount post_loss_window = sender_->GetCongestionWindow();
EXPECT_GT(initial_window, post_loss_window);
sender_->OnPacketLost(acked_sequence_number_ + 3, clock_.Now());
EXPECT_EQ(post_loss_window, sender_->GetCongestionWindow());
sender_->OnPacketLost(sequence_number_ - 1, clock_.Now());
EXPECT_EQ(post_loss_window, sender_->GetCongestionWindow());
sender_->OnPacketLost(sequence_number_, clock_.Now());
EXPECT_GT(post_loss_window, sender_->GetCongestionWindow());
}
TEST_F(TcpCubicSenderTest, SendWindowNotAffectedByAcks) {
QuicByteCount send_window = sender_->AvailableSendWindow();
QuicByteCount bytes_in_packet = min(kDefaultTCPMSS, send_window);
sender_->OnPacketSent(clock_.Now(), sequence_number_++, bytes_in_packet,
NO_RETRANSMITTABLE_DATA);
EXPECT_EQ(send_window, sender_->AvailableSendWindow());
sender_->OnPacketSent(clock_.Now(), sequence_number_++, bytes_in_packet,
HAS_RETRANSMITTABLE_DATA);
EXPECT_GT(send_window, sender_->AvailableSendWindow());
}
TEST_F(TcpCubicSenderTest, ConfigureMaxInitialWindow) {
QuicTcpCongestionWindow congestion_window = sender_->congestion_window();
QuicConfig config;
config.set_server_initial_congestion_window(2 * congestion_window,
2 * congestion_window);
EXPECT_EQ(2 * congestion_window, config.server_initial_congestion_window());
sender_->SetFromConfig(config, true);
EXPECT_EQ(2 * congestion_window, sender_->congestion_window());
}
TEST_F(TcpCubicSenderTest, CongestionAvoidanceAtEndOfRecovery) {
QuicCongestionFeedbackFrame feedback;
EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(),
HAS_RETRANSMITTABLE_DATA).IsZero());
ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback));
sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now());
const int kNumberOfAcks = 5;
for (int i = 0; i < kNumberOfAcks; ++i) {
SendAvailableSendWindow();
AckNPackets(2);
}
SendAvailableSendWindow();
QuicByteCount expected_send_window = kDefaultWindowTCP +
(kDefaultTCPMSS * 2 * kNumberOfAcks);
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
LoseNPackets(1);
expected_send_window /= 2;
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
for (int i = 0; i < 10; ++i) {
SendAvailableSendWindow();
AckNPackets(2);
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
for (int i = 0; i < 4; ++i) {
SendAvailableSendWindow();
AckNPackets(2);
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
AckNPackets(2);
expected_send_window += kDefaultTCPMSS;
EXPECT_EQ(expected_send_window, sender_->GetCongestionWindow());
}
}
}