You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2019 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. #include "call/simulated_network.h"
  11. #include <algorithm>
  12. #include <map>
  13. #include <set>
  14. #include <vector>
  15. #include "absl/algorithm/container.h"
  16. #include "api/units/data_rate.h"
  17. #include "test/gtest.h"
  18. namespace webrtc {
  19. namespace {
  20. constexpr int kNotReceived = PacketDeliveryInfo::kNotReceived;
  21. }
  22. TEST(SimulatedNetworkTest, CodelDoesNothingAtCapacity) {
  23. const TimeDelta kRuntime = TimeDelta::seconds(30);
  24. DataRate link_capacity = DataRate::kbps(1000);
  25. const DataSize packet_size = DataSize::bytes(1000);
  26. SimulatedNetwork::Config config;
  27. config.codel_active_queue_management = true;
  28. config.queue_delay_ms = 10;
  29. config.link_capacity_kbps = link_capacity.kbps();
  30. SimulatedNetwork network(config);
  31. // Need to round up here as otherwise we actually will choke.
  32. const TimeDelta packet_inverval =
  33. packet_size / link_capacity + TimeDelta::ms(1);
  34. // Send at capacity and see we get no loss.
  35. Timestamp start_time = Timestamp::ms(0);
  36. Timestamp current_time = start_time;
  37. Timestamp next_packet_time = start_time;
  38. uint64_t next_id = 0;
  39. std::set<uint64_t> pending;
  40. while (current_time - start_time < kRuntime) {
  41. if (current_time >= next_packet_time) {
  42. bool success = network.EnqueuePacket(PacketInFlightInfo{
  43. packet_size.bytes<size_t>(), current_time.us(), next_id});
  44. EXPECT_TRUE(success);
  45. pending.insert(next_id);
  46. ++next_id;
  47. next_packet_time += packet_inverval;
  48. }
  49. Timestamp next_delivery = Timestamp::PlusInfinity();
  50. if (network.NextDeliveryTimeUs())
  51. next_delivery = Timestamp::us(*network.NextDeliveryTimeUs());
  52. current_time = std::min(next_packet_time, next_delivery);
  53. if (current_time >= next_delivery) {
  54. for (PacketDeliveryInfo packet :
  55. network.DequeueDeliverablePackets(current_time.us())) {
  56. EXPECT_NE(packet.receive_time_us, kNotReceived);
  57. pending.erase(packet.packet_id);
  58. }
  59. }
  60. }
  61. while (network.NextDeliveryTimeUs()) {
  62. for (PacketDeliveryInfo packet :
  63. network.DequeueDeliverablePackets(*network.NextDeliveryTimeUs())) {
  64. EXPECT_NE(packet.receive_time_us, kNotReceived);
  65. pending.erase(packet.packet_id);
  66. }
  67. }
  68. EXPECT_EQ(pending.size(), 0u);
  69. }
  70. TEST(SimulatedNetworkTest, CodelLimitsDelayAndDropsPacketsOnOverload) {
  71. const TimeDelta kRuntime = TimeDelta::seconds(30);
  72. const TimeDelta kCheckInterval = TimeDelta::ms(2000);
  73. DataRate link_capacity = DataRate::kbps(1000);
  74. const DataSize rough_packet_size = DataSize::bytes(1500);
  75. const double overload_rate = 1.5;
  76. SimulatedNetwork::Config config;
  77. config.codel_active_queue_management = true;
  78. config.queue_delay_ms = 10;
  79. config.link_capacity_kbps = link_capacity.kbps();
  80. SimulatedNetwork network(config);
  81. const TimeDelta packet_inverval = rough_packet_size / link_capacity;
  82. const DataSize packet_size = overload_rate * link_capacity * packet_inverval;
  83. // Send above capacity and see delays are still controlled at the cost of
  84. // packet loss.
  85. Timestamp start_time = Timestamp::ms(0);
  86. Timestamp current_time = start_time;
  87. Timestamp next_packet_time = start_time;
  88. Timestamp last_check = start_time;
  89. uint64_t next_id = 1;
  90. std::map<uint64_t, int64_t> send_times_us;
  91. int lost = 0;
  92. std::vector<int64_t> delays_us;
  93. while (current_time - start_time < kRuntime) {
  94. if (current_time >= next_packet_time) {
  95. bool success = network.EnqueuePacket(PacketInFlightInfo{
  96. packet_size.bytes<size_t>(), current_time.us(), next_id});
  97. send_times_us.insert({next_id, current_time.us()});
  98. ++next_id;
  99. EXPECT_TRUE(success);
  100. next_packet_time += packet_inverval;
  101. }
  102. Timestamp next_delivery = Timestamp::PlusInfinity();
  103. if (network.NextDeliveryTimeUs())
  104. next_delivery = Timestamp::us(*network.NextDeliveryTimeUs());
  105. current_time = std::min(next_packet_time, next_delivery);
  106. if (current_time >= next_delivery) {
  107. for (PacketDeliveryInfo packet :
  108. network.DequeueDeliverablePackets(current_time.us())) {
  109. if (packet.receive_time_us == kNotReceived) {
  110. ++lost;
  111. } else {
  112. delays_us.push_back(packet.receive_time_us -
  113. send_times_us[packet.packet_id]);
  114. }
  115. send_times_us.erase(packet.packet_id);
  116. }
  117. }
  118. if (current_time > last_check + kCheckInterval) {
  119. last_check = current_time;
  120. TimeDelta average_delay =
  121. TimeDelta::us(absl::c_accumulate(delays_us, 0)) / delays_us.size();
  122. double loss_ratio = static_cast<double>(lost) / (lost + delays_us.size());
  123. EXPECT_LT(average_delay.ms(), 200)
  124. << "Time " << (current_time - start_time).ms() << "\n";
  125. EXPECT_GT(loss_ratio, 0.5 * (overload_rate - 1));
  126. }
  127. }
  128. while (network.NextDeliveryTimeUs()) {
  129. for (PacketDeliveryInfo packet :
  130. network.DequeueDeliverablePackets(*network.NextDeliveryTimeUs())) {
  131. send_times_us.erase(packet.packet_id);
  132. }
  133. }
  134. EXPECT_EQ(send_times_us.size(), 0u);
  135. }
  136. } // namespace webrtc