Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 2 | /* |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2016-2019, Regents of the University of California, |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 6 | * |
| 7 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 8 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 9 | * |
| 10 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 22 | * |
| 23 | * @author Weiwei Liu |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 24 | * @author Chavoosh Ghasemi |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 25 | */ |
| 26 | |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame^] | 27 | #include "tools/chunks/catchunks/rtt-estimator.hpp" |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace chunks { |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 32 | namespace tests { |
| 33 | |
| 34 | class RttEstimatorFixture |
| 35 | { |
| 36 | protected: |
| 37 | RttEstimatorFixture() |
| 38 | : options(makeRttEstimatorOptions()) |
| 39 | , rttEstimator(options) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | static RttEstimator::Options |
| 45 | makeRttEstimatorOptions() |
| 46 | { |
| 47 | RttEstimator::Options rttOptions; |
| 48 | rttOptions.alpha = 0.125; |
| 49 | rttOptions.beta = 0.25; |
| 50 | rttOptions.k = 4; |
| 51 | rttOptions.minRto = Milliseconds(200.0); |
| 52 | rttOptions.maxRto = Milliseconds(4000.0); |
| 53 | return rttOptions; |
| 54 | } |
| 55 | |
| 56 | protected: |
| 57 | RttEstimator::Options options; |
| 58 | RttEstimator rttEstimator; |
| 59 | }; |
| 60 | |
| 61 | BOOST_AUTO_TEST_SUITE(Chunks) |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame^] | 62 | BOOST_FIXTURE_TEST_SUITE(TestRttEstimator, RttEstimatorFixture) |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 63 | |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 64 | BOOST_AUTO_TEST_CASE(MinAvgMaxRtt) |
| 65 | { |
| 66 | // check initial values |
| 67 | BOOST_CHECK_CLOSE(rttEstimator.m_rttMin, std::numeric_limits<double>::max(), 0.1); |
| 68 | BOOST_CHECK_CLOSE(rttEstimator.m_rttAvg, 0.0, 0.1); |
| 69 | BOOST_CHECK_CLOSE(rttEstimator.m_rttMax, std::numeric_limits<double>::min(), 0.1); |
| 70 | BOOST_CHECK_EQUAL(rttEstimator.m_nRttSamples, 0); |
| 71 | |
| 72 | // start with three samples |
| 73 | rttEstimator.addMeasurement(1, Milliseconds(100), 1); |
| 74 | rttEstimator.addMeasurement(2, Milliseconds(400), 1); |
| 75 | rttEstimator.addMeasurement(3, Milliseconds(250), 1); |
| 76 | |
| 77 | BOOST_CHECK_CLOSE(rttEstimator.m_rttMin, 100, 0.1); |
| 78 | BOOST_CHECK_CLOSE(rttEstimator.m_rttAvg, 250, 0.1); |
| 79 | BOOST_CHECK_CLOSE(rttEstimator.m_rttMax, 400, 0.1); |
| 80 | BOOST_CHECK_EQUAL(rttEstimator.m_nRttSamples, 3); |
| 81 | |
| 82 | // add another sample (new minimum) |
| 83 | rttEstimator.addMeasurement(4, Milliseconds(50), 2); |
| 84 | BOOST_CHECK_CLOSE(rttEstimator.m_rttMin, 50, 0.1); |
| 85 | BOOST_CHECK_CLOSE(rttEstimator.m_rttAvg, 200, 0.1); |
| 86 | BOOST_CHECK_CLOSE(rttEstimator.m_rttMax, 400, 0.1); |
| 87 | BOOST_CHECK_EQUAL(rttEstimator.m_nRttSamples, 4); |
| 88 | |
| 89 | // add another sample (new maximum) |
| 90 | rttEstimator.addMeasurement(5, Milliseconds(700), 1); |
| 91 | BOOST_CHECK_CLOSE(rttEstimator.m_rttMin, 50, 0.1); |
| 92 | BOOST_CHECK_CLOSE(rttEstimator.m_rttAvg, 300, 0.1); |
| 93 | BOOST_CHECK_CLOSE(rttEstimator.m_rttMax, 700, 0.1); |
| 94 | BOOST_CHECK_EQUAL(rttEstimator.m_nRttSamples, 5); |
| 95 | } |
| 96 | |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 97 | BOOST_AUTO_TEST_CASE(MeasureRtt) |
| 98 | { |
| 99 | BOOST_REQUIRE(std::isnan(rttEstimator.m_sRtt.count())); |
| 100 | BOOST_REQUIRE(std::isnan(rttEstimator.m_rttVar.count())); |
| 101 | BOOST_REQUIRE_CLOSE(rttEstimator.m_rto.count(), options.initialRto.count(), 1); |
| 102 | |
| 103 | // first measurement |
| 104 | rttEstimator.addMeasurement(1, Milliseconds(100), 1); |
| 105 | |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 106 | BOOST_CHECK_CLOSE(rttEstimator.m_sRtt.count(), 100, 0.1); |
| 107 | BOOST_CHECK_CLOSE(rttEstimator.m_rttVar.count(), 50, 0.1); |
| 108 | BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 300, 0.1); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 109 | |
| 110 | rttEstimator.m_sRtt = Milliseconds(500.0); |
| 111 | rttEstimator.m_rttVar = Milliseconds(100.0); |
| 112 | rttEstimator.m_rto = Milliseconds(900.0); |
| 113 | |
| 114 | size_t nExpectedSamples = 1; |
| 115 | rttEstimator.addMeasurement(1, Milliseconds(100), nExpectedSamples); |
| 116 | |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 117 | BOOST_CHECK_CLOSE(rttEstimator.m_sRtt.count(), 450, 0.1); |
| 118 | BOOST_CHECK_CLOSE(rttEstimator.m_rttVar.count(), 175, 0.1); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 119 | BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 1150, 0.1); |
| 120 | |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 121 | // expected samples larger than 1 |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 122 | nExpectedSamples = 5; |
| 123 | rttEstimator.addMeasurement(1, Milliseconds(100), nExpectedSamples); |
| 124 | |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 125 | BOOST_CHECK_CLOSE(rttEstimator.m_sRtt.count(), 441.25, 0.1); |
| 126 | BOOST_CHECK_CLOSE(rttEstimator.m_rttVar.count(), 183.75, 0.1); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 127 | BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 1176.25, 0.1); |
| 128 | |
| 129 | rttEstimator.m_sRtt = Milliseconds(100.0); |
| 130 | rttEstimator.m_rttVar = Milliseconds(30.0); |
| 131 | rttEstimator.m_rto = Milliseconds(220.0); |
| 132 | |
| 133 | // check if minRto works |
| 134 | nExpectedSamples = 1; |
| 135 | rttEstimator.addMeasurement(1, Milliseconds(100), nExpectedSamples); |
| 136 | |
Chavoosh Ghasemi | 3dae109 | 2017-12-21 12:39:08 -0700 | [diff] [blame] | 137 | BOOST_CHECK_CLOSE(rttEstimator.m_sRtt.count(), 100, 0.1); |
| 138 | BOOST_CHECK_CLOSE(rttEstimator.m_rttVar.count(), 22.5, 0.1); |
| 139 | BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 200, 0.1); |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 140 | |
| 141 | rttEstimator.m_sRtt = Milliseconds(2000.0); |
| 142 | rttEstimator.m_rttVar = Milliseconds(400.0); |
| 143 | rttEstimator.m_rto = Milliseconds(3600.0); |
| 144 | |
| 145 | // check if maxRto works |
| 146 | nExpectedSamples = 1; |
| 147 | rttEstimator.addMeasurement(1, Milliseconds(100), nExpectedSamples); |
| 148 | |
| 149 | BOOST_CHECK_CLOSE(rttEstimator.m_sRtt.count(), 1762.5, 0.1); |
| 150 | BOOST_CHECK_CLOSE(rttEstimator.m_rttVar.count(), 775, 0.1); |
| 151 | BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 4000, 0.1); |
| 152 | } |
| 153 | |
| 154 | BOOST_AUTO_TEST_CASE(RtoBackoff) |
| 155 | { |
| 156 | rttEstimator.m_rto = Milliseconds(500.0); |
| 157 | rttEstimator.backoffRto(); |
| 158 | BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 1000, 0.1); |
| 159 | |
| 160 | // check if minRto works |
| 161 | rttEstimator.m_rto = Milliseconds(10.0); |
| 162 | rttEstimator.backoffRto(); |
| 163 | BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 200, 0.1); |
| 164 | |
| 165 | // check if maxRto works |
| 166 | rttEstimator.m_rto = Milliseconds(3000.0); |
| 167 | rttEstimator.backoffRto(); |
| 168 | BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 4000, 0.1); |
| 169 | } |
| 170 | |
schneiderklaus | d8197df | 2019-03-16 11:31:40 -0700 | [diff] [blame^] | 171 | BOOST_AUTO_TEST_SUITE_END() // TestRttEstimator |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 172 | BOOST_AUTO_TEST_SUITE_END() // Chunks |
| 173 | |
| 174 | } // namespace tests |
Weiwei Liu | 245d791 | 2016-07-28 00:04:25 -0700 | [diff] [blame] | 175 | } // namespace chunks |
| 176 | } // namespace ndn |