1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // A test only class to enable simulations of send algorithms.
7 #ifndef NET_QUIC_CONGESTION_CONTROL_SEND_ALGORITHM_SIMULATOR_H_
8 #define NET_QUIC_CONGESTION_CONTROL_SEND_ALGORITHM_SIMULATOR_H_
14 #include "base/basictypes.h"
15 #include "base/format_macros.h"
16 #include "base/strings/stringprintf.h"
17 #include "net/quic/congestion_control/send_algorithm_interface.h"
18 #include "net/quic/quic_protocol.h"
19 #include "net/quic/quic_time.h"
20 #include "net/quic/test_tools/mock_clock.h"
21 #include "net/quic/test_tools/quic_test_utils.h"
23 using base::StringPrintf
;
27 class SendAlgorithmSimulator
{
30 Sender(SendAlgorithmInterface
* send_algorithm
,
32 Sender(SendAlgorithmInterface
* send_algorithm
,
34 QuicTime::Delta additional_rtt
);
37 QuicByteCount cwnd
= send_algorithm
->GetCongestionWindow();
38 max_cwnd
= std::max(max_cwnd
, cwnd
);
39 min_cwnd
= std::min(min_cwnd
, cwnd
);
40 if (last_cwnd
> cwnd
) {
41 max_cwnd_drop
= std::max(max_cwnd_drop
, last_cwnd
- cwnd
);
46 std::string
DebugString() {
47 return StringPrintf("observed goodput(bytes/s):%" PRId64
50 " max_cwnd:%" PRIu64
" min_cwnd:%" PRIu64
51 " max_cwnd_drop:%" PRIu64
,
52 last_transfer_bandwidth
.ToBytesPerSecond(),
53 last_transfer_loss_rate
,
54 send_algorithm
->GetCongestionWindow(),
55 max_cwnd
, min_cwnd
, max_cwnd_drop
);
58 SendAlgorithmInterface
* send_algorithm
;
60 QuicTime::Delta additional_rtt
;
62 // Last sequence number the sender sent.
63 QuicPacketSequenceNumber last_sent
;
64 // Last packet sequence number acked.
65 QuicPacketSequenceNumber last_acked
;
66 // Packet sequence number to ack up to.
67 QuicPacketSequenceNumber next_acked
;
69 // Stats collected for understanding the congestion control.
70 QuicByteCount max_cwnd
;
71 QuicByteCount min_cwnd
;
72 QuicByteCount max_cwnd_drop
;
73 QuicByteCount last_cwnd
;
75 QuicBandwidth last_transfer_bandwidth
;
76 float last_transfer_loss_rate
;
80 Transfer(Sender
* sender
,
81 QuicByteCount num_bytes
,
86 QuicByteCount num_bytes
;
87 QuicByteCount bytes_acked
;
88 QuicByteCount bytes_lost
;
89 QuicByteCount bytes_in_flight
;
97 send_time(QuicTime::Zero()),
98 ack_time(QuicTime::Zero()),
101 SentPacket(QuicPacketSequenceNumber sequence_number
,
106 : sequence_number(sequence_number
),
107 send_time(send_time
),
110 transfer(transfer
) {}
112 QuicPacketSequenceNumber sequence_number
;
119 // |rtt_stats| should be the same RttStats used by the |send_algorithm|.
120 SendAlgorithmSimulator(MockClock
* clock_
,
121 QuicBandwidth bandwidth
,
122 QuicTime::Delta rtt
);
123 ~SendAlgorithmSimulator();
125 // For local ad-hoc testing.
126 void set_bandwidth(QuicBandwidth bandwidth
) {
127 bandwidth_
= bandwidth
;
130 void set_forward_loss_rate(float loss_rate
) {
131 DCHECK_LT(loss_rate
, 1.0f
);
132 forward_loss_rate_
= loss_rate
;
135 // For local ad-hoc testing.
136 void set_reverse_loss_rate(float loss_rate
) {
137 DCHECK_LT(loss_rate
, 1.0f
);
138 reverse_loss_rate_
= loss_rate
;
141 // For local ad-hoc testing.
142 void set_loss_correlation(float loss_correlation
) {
143 DCHECK_LT(loss_correlation
, 1.0f
);
144 loss_correlation_
= loss_correlation
;
147 void set_buffer_size(size_t buffer_size_bytes
) {
148 buffer_size_
= buffer_size_bytes
;
151 void set_delayed_ack_timer(QuicTime::Delta delayed_ack_timer
) {
152 delayed_ack_timer_
= delayed_ack_timer
;
155 // Advance the time by |delta| without sending anything.
156 // For local ad-hoc testing.
157 void AdvanceTime(QuicTime::Delta delta
);
159 // Adds a pending sender. The send will run when TransferBytes is called.
160 // Adding two transfers with the same sender is unsupported.
161 void AddTransfer(Sender
* sender
, size_t num_bytes
);
163 // Adds a pending sending to start at the specified time.
164 void AddTransfer(Sender
* sender
,
169 // Convenience method to transfer all bytes.
170 void TransferBytes();
172 // Transfers bytes through the connection until |max_bytes| are reached,
173 // |max_time| is reached, or all senders have finished sending. If max_bytes
174 // is 0, it does not apply, and if |max_time| is Zero, no time limit applies.
175 void TransferBytes(QuicByteCount max_bytes
, QuicTime::Delta max_time
);
178 // A pending packet event, either a send or an ack.
180 PacketEvent(QuicTime::Delta time_delta
, Transfer
* transfer
)
181 : time_delta(time_delta
),
182 transfer(transfer
) {}
184 QuicTime::Delta time_delta
;
188 // NextSendTime returns the next time any of the pending transfers send,
189 // and populates transfer if the send time is not infinite.
190 PacketEvent
NextSendEvent();
192 // NextAckTime takes into account packet loss in both forward and reverse
193 // direction, as well as delayed ack behavior.
194 PacketEvent
NextAckEvent();
196 // Sets the next acked.
197 QuicTime::Delta
FindNextAcked(Transfer
* transfer
);
199 // Sets the |next_acked| packet for the |transfer| starting at the specified
200 // |last_acked|. Returns QuicTime::Delta::Infinite and doesn't set
201 // |next_acked| if there is no ack after |last_acked|.
202 QuicTime::Delta
FindNextAck(const Transfer
* transfer
,
203 QuicPacketSequenceNumber last_acked
,
204 QuicPacketSequenceNumber
* next_acked
) const;
206 // Returns true if any of the packets |transfer| is waiting for less than
207 // next_acked have been lost.
208 bool HasRecentLostPackets(const Transfer
* transfer
,
209 QuicPacketSequenceNumber next_acked
) const;
211 // Process all the acks that should have arrived by the current time, and
212 // lose any packets that are missing. Returns the number of bytes acked.
213 void HandlePendingAck(Transfer
* transfer
);
215 void SendDataNow(Transfer
* transfer
);
217 // List of all pending transfers waiting to use the connection.
218 std::vector
<Transfer
> pending_transfers_
;
221 // Whether the next ack should be lost.
223 // The times acks are expected, assuming acks are not lost and every packet
225 std::list
<SentPacket
> sent_packets_
;
227 test::SimpleRandom simple_random_
;
228 float forward_loss_rate_
; // Loss rate on the forward path.
229 float reverse_loss_rate_
; // Loss rate on the reverse path.
230 float loss_correlation_
; // Likelihood the subsequent packet is lost.
231 QuicBandwidth bandwidth_
;
232 QuicTime::Delta rtt_
;
233 size_t buffer_size_
; // In bytes.
234 QuicTime::Delta delayed_ack_timer_
;
236 DISALLOW_COPY_AND_ASSIGN(SendAlgorithmSimulator
);
241 #endif // NET_QUIC_CONGESTION_CONTROL_SEND_ALGORITHM_SIMULATOR_H_