OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/test_tools/simulator/simulator.h" | 5 #include "net/quic/test_tools/simulator/simulator.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "net/quic/test_tools/quic_test_utils.h" | 8 #include "net/quic/test_tools/quic_test_utils.h" |
9 #include "net/quic/test_tools/simulator/alarm_factory.h" | 9 #include "net/quic/test_tools/simulator/alarm_factory.h" |
10 #include "net/quic/test_tools/simulator/link.h" | 10 #include "net/quic/test_tools/simulator/link.h" |
11 #include "net/quic/test_tools/simulator/queue.h" | 11 #include "net/quic/test_tools/simulator/queue.h" |
12 #include "net/quic/test_tools/simulator/switch.h" | 12 #include "net/quic/test_tools/simulator/switch.h" |
| 13 #include "net/quic/test_tools/simulator/traffic_policer.h" |
13 | 14 |
14 #include "net/test/gtest_util.h" | 15 #include "net/test/gtest_util.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 namespace simulator { | 19 namespace simulator { |
19 | 20 |
20 // A simple counter that increments its value by 1 every specified period. | 21 // A simple counter that increments its value by 1 every specified period. |
21 class Counter : public Actor { | 22 class Counter : public Actor { |
22 public: | 23 public: |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 134 |
134 void SetTxPort(ConstrainedPortInterface* port) override { tx_port_ = port; } | 135 void SetTxPort(ConstrainedPortInterface* port) override { tx_port_ = port; } |
135 | 136 |
136 CounterPort* counter() { return &rx_port_; } | 137 CounterPort* counter() { return &rx_port_; } |
137 | 138 |
138 inline QuicByteCount bytes_transmitted() const { return bytes_transmitted_; } | 139 inline QuicByteCount bytes_transmitted() const { return bytes_transmitted_; } |
139 inline QuicPacketCount packets_transmitted() const { | 140 inline QuicPacketCount packets_transmitted() const { |
140 return packets_transmitted_; | 141 return packets_transmitted_; |
141 } | 142 } |
142 | 143 |
| 144 void Pause() { Unschedule(); } |
| 145 void Resume() { Schedule(clock_->Now()); } |
| 146 |
143 private: | 147 private: |
144 QuicByteCount packet_size_; | 148 QuicByteCount packet_size_; |
145 std::string destination_; | 149 std::string destination_; |
146 | 150 |
147 ConstrainedPortInterface* tx_port_; | 151 ConstrainedPortInterface* tx_port_; |
148 CounterPort rx_port_; | 152 CounterPort rx_port_; |
149 | 153 |
150 QuicByteCount bytes_transmitted_; | 154 QuicByteCount bytes_transmitted_; |
151 QuicPacketCount packets_transmitted_; | 155 QuicPacketCount packets_transmitted_; |
152 }; | 156 }; |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 TEST(SimulatorTest, RunFor) { | 557 TEST(SimulatorTest, RunFor) { |
554 Simulator simulator; | 558 Simulator simulator; |
555 | 559 |
556 Counter counter(&simulator, "counter", QuicTime::Delta::FromSeconds(3)); | 560 Counter counter(&simulator, "counter", QuicTime::Delta::FromSeconds(3)); |
557 | 561 |
558 simulator.RunFor(QuicTime::Delta::FromSeconds(100)); | 562 simulator.RunFor(QuicTime::Delta::FromSeconds(100)); |
559 | 563 |
560 EXPECT_EQ(33, counter.get_value()); | 564 EXPECT_EQ(33, counter.get_value()); |
561 } | 565 } |
562 | 566 |
| 567 // Set up a traffic policer in one direction that throttles at 25% of link |
| 568 // bandwidth, and put two link saturators at each endpoint. |
| 569 TEST(SimulatorTest, TrafficPolicer) { |
| 570 const QuicBandwidth bandwidth = |
| 571 QuicBandwidth::FromBytesPerSecond(1024 * 1024); |
| 572 const QuicTime::Delta base_propagation_delay = |
| 573 QuicTime::Delta::FromMilliseconds(5); |
| 574 const QuicTime::Delta timeout = QuicTime::Delta::FromSeconds(10); |
| 575 |
| 576 Simulator simulator; |
| 577 LinkSaturator saturator1(&simulator, "Saturator 1", 1000, "Saturator 2"); |
| 578 LinkSaturator saturator2(&simulator, "Saturator 2", 1000, "Saturator 1"); |
| 579 Switch network_switch(&simulator, "Switch", 8, |
| 580 bandwidth * base_propagation_delay * 10); |
| 581 |
| 582 const QuicByteCount initial_burst = 1000 * 10; |
| 583 const QuicByteCount max_bucket_size = 1000 * 100; |
| 584 const QuicBandwidth target_bandwidth = bandwidth * 0.25; |
| 585 TrafficPolicer policer(&simulator, "Policer", initial_burst, max_bucket_size, |
| 586 target_bandwidth, network_switch.port(2)); |
| 587 |
| 588 SymmetricLink link1(&saturator1, network_switch.port(1), bandwidth, |
| 589 base_propagation_delay); |
| 590 SymmetricLink link2(&saturator2, policer.output(), bandwidth, |
| 591 base_propagation_delay); |
| 592 |
| 593 // Ensure the initial burst passes without being dropped at all. |
| 594 bool simulator_result = simulator.RunUntilOrTimeout( |
| 595 [&saturator1, initial_burst]() { |
| 596 return saturator1.bytes_transmitted() == initial_burst; |
| 597 }, |
| 598 timeout); |
| 599 ASSERT_TRUE(simulator_result); |
| 600 saturator1.Pause(); |
| 601 simulator_result = simulator.RunUntilOrTimeout( |
| 602 [&saturator2, initial_burst]() { |
| 603 return saturator2.counter()->bytes() == initial_burst; |
| 604 }, |
| 605 timeout); |
| 606 ASSERT_TRUE(simulator_result); |
| 607 saturator1.Resume(); |
| 608 |
| 609 // Run for some time so that the initial burst is not visible. |
| 610 const QuicTime::Delta simulation_time = QuicTime::Delta::FromSeconds(10); |
| 611 simulator.RunFor(simulation_time); |
| 612 |
| 613 // Ensure we've transmitted the amount of data we expected. |
| 614 for (auto* saturator : {&saturator1, &saturator2}) { |
| 615 test::ExpectApproxEq(bandwidth * simulation_time, |
| 616 saturator->bytes_transmitted(), 0.01f); |
| 617 } |
| 618 |
| 619 // Check that only one direction is throttled. |
| 620 test::ExpectApproxEq(saturator1.bytes_transmitted() / 4, |
| 621 saturator2.counter()->bytes(), 0.1f); |
| 622 test::ExpectApproxEq(saturator2.bytes_transmitted(), |
| 623 saturator1.counter()->bytes(), 0.1f); |
| 624 } |
| 625 |
| 626 // Ensure that a larger burst is allowed when the policed saturator exits |
| 627 // quiescence. |
| 628 TEST(SimulatorTest, TrafficPolicerBurst) { |
| 629 const QuicBandwidth bandwidth = |
| 630 QuicBandwidth::FromBytesPerSecond(1024 * 1024); |
| 631 const QuicTime::Delta base_propagation_delay = |
| 632 QuicTime::Delta::FromMilliseconds(5); |
| 633 const QuicTime::Delta timeout = QuicTime::Delta::FromSeconds(10); |
| 634 |
| 635 Simulator simulator; |
| 636 LinkSaturator saturator1(&simulator, "Saturator 1", 1000, "Saturator 2"); |
| 637 LinkSaturator saturator2(&simulator, "Saturator 2", 1000, "Saturator 1"); |
| 638 Switch network_switch(&simulator, "Switch", 8, |
| 639 bandwidth * base_propagation_delay * 10); |
| 640 |
| 641 const QuicByteCount initial_burst = 1000 * 10; |
| 642 const QuicByteCount max_bucket_size = 1000 * 100; |
| 643 const QuicBandwidth target_bandwidth = bandwidth * 0.25; |
| 644 TrafficPolicer policer(&simulator, "Policer", initial_burst, max_bucket_size, |
| 645 target_bandwidth, network_switch.port(2)); |
| 646 |
| 647 SymmetricLink link1(&saturator1, network_switch.port(1), bandwidth, |
| 648 base_propagation_delay); |
| 649 SymmetricLink link2(&saturator2, policer.output(), bandwidth, |
| 650 base_propagation_delay); |
| 651 |
| 652 // Ensure at least one packet is sent on each side. |
| 653 bool simulator_result = simulator.RunUntilOrTimeout( |
| 654 [&saturator1, &saturator2]() { |
| 655 return saturator1.packets_transmitted() > 0 && |
| 656 saturator2.packets_transmitted() > 0; |
| 657 }, |
| 658 timeout); |
| 659 ASSERT_TRUE(simulator_result); |
| 660 |
| 661 // Wait until the bucket fills up. |
| 662 saturator1.Pause(); |
| 663 saturator2.Pause(); |
| 664 simulator.RunFor(1.5f * target_bandwidth.TransferTime(max_bucket_size)); |
| 665 |
| 666 // Send a burst. |
| 667 saturator1.Resume(); |
| 668 simulator.RunFor(bandwidth.TransferTime(max_bucket_size)); |
| 669 saturator1.Pause(); |
| 670 simulator.RunFor(2 * base_propagation_delay); |
| 671 |
| 672 // Expect the burst to pass without losses. |
| 673 test::ExpectApproxEq(saturator1.bytes_transmitted(), |
| 674 saturator2.counter()->bytes(), 0.1f); |
| 675 |
| 676 // Expect subsequent traffic to be policed. |
| 677 saturator1.Resume(); |
| 678 simulator.RunFor(QuicTime::Delta::FromSeconds(10)); |
| 679 test::ExpectApproxEq(saturator1.bytes_transmitted() / 4, |
| 680 saturator2.counter()->bytes(), 0.1f); |
| 681 } |
| 682 |
563 } // namespace simulator | 683 } // namespace simulator |
564 } // namespace net | 684 } // namespace net |
OLD | NEW |