blob: 9241fee2338938e29781211782611dcf69bd9081 [file] [log] [blame]
Weiwei Liu245d7912016-07-28 00:04:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -07002/*
3 * Copyright (c) 2016-2018, Regents of the University of California,
4 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
Weiwei Liu245d7912016-07-28 00:04:25 -07006 *
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 Ghasemi3dae1092017-12-21 12:39:08 -070024 * @author Chavoosh Ghasemi
Weiwei Liu245d7912016-07-28 00:04:25 -070025 */
26
27#include "tools/chunks/catchunks/aimd-rtt-estimator.hpp"
28
29#include "tests/test-common.hpp"
30
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -070031#include <limits>
32
Weiwei Liu245d7912016-07-28 00:04:25 -070033namespace ndn {
34namespace chunks {
35namespace aimd {
36namespace tests {
37
38class RttEstimatorFixture
39{
40protected:
41 RttEstimatorFixture()
42 : options(makeRttEstimatorOptions())
43 , rttEstimator(options)
44 {
45 }
46
47private:
48 static RttEstimator::Options
49 makeRttEstimatorOptions()
50 {
51 RttEstimator::Options rttOptions;
52 rttOptions.alpha = 0.125;
53 rttOptions.beta = 0.25;
54 rttOptions.k = 4;
55 rttOptions.minRto = Milliseconds(200.0);
56 rttOptions.maxRto = Milliseconds(4000.0);
57 return rttOptions;
58 }
59
60protected:
61 RttEstimator::Options options;
62 RttEstimator rttEstimator;
63};
64
65BOOST_AUTO_TEST_SUITE(Chunks)
66BOOST_FIXTURE_TEST_SUITE(TestAimdRttEstimator, RttEstimatorFixture)
67
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -070068BOOST_AUTO_TEST_CASE(MinAvgMaxRtt)
69{
70 // check initial values
71 BOOST_CHECK_CLOSE(rttEstimator.m_rttMin, std::numeric_limits<double>::max(), 0.1);
72 BOOST_CHECK_CLOSE(rttEstimator.m_rttAvg, 0.0, 0.1);
73 BOOST_CHECK_CLOSE(rttEstimator.m_rttMax, std::numeric_limits<double>::min(), 0.1);
74 BOOST_CHECK_EQUAL(rttEstimator.m_nRttSamples, 0);
75
76 // start with three samples
77 rttEstimator.addMeasurement(1, Milliseconds(100), 1);
78 rttEstimator.addMeasurement(2, Milliseconds(400), 1);
79 rttEstimator.addMeasurement(3, Milliseconds(250), 1);
80
81 BOOST_CHECK_CLOSE(rttEstimator.m_rttMin, 100, 0.1);
82 BOOST_CHECK_CLOSE(rttEstimator.m_rttAvg, 250, 0.1);
83 BOOST_CHECK_CLOSE(rttEstimator.m_rttMax, 400, 0.1);
84 BOOST_CHECK_EQUAL(rttEstimator.m_nRttSamples, 3);
85
86 // add another sample (new minimum)
87 rttEstimator.addMeasurement(4, Milliseconds(50), 2);
88 BOOST_CHECK_CLOSE(rttEstimator.m_rttMin, 50, 0.1);
89 BOOST_CHECK_CLOSE(rttEstimator.m_rttAvg, 200, 0.1);
90 BOOST_CHECK_CLOSE(rttEstimator.m_rttMax, 400, 0.1);
91 BOOST_CHECK_EQUAL(rttEstimator.m_nRttSamples, 4);
92
93 // add another sample (new maximum)
94 rttEstimator.addMeasurement(5, Milliseconds(700), 1);
95 BOOST_CHECK_CLOSE(rttEstimator.m_rttMin, 50, 0.1);
96 BOOST_CHECK_CLOSE(rttEstimator.m_rttAvg, 300, 0.1);
97 BOOST_CHECK_CLOSE(rttEstimator.m_rttMax, 700, 0.1);
98 BOOST_CHECK_EQUAL(rttEstimator.m_nRttSamples, 5);
99}
100
Weiwei Liu245d7912016-07-28 00:04:25 -0700101BOOST_AUTO_TEST_CASE(MeasureRtt)
102{
103 BOOST_REQUIRE(std::isnan(rttEstimator.m_sRtt.count()));
104 BOOST_REQUIRE(std::isnan(rttEstimator.m_rttVar.count()));
105 BOOST_REQUIRE_CLOSE(rttEstimator.m_rto.count(), options.initialRto.count(), 1);
106
107 // first measurement
108 rttEstimator.addMeasurement(1, Milliseconds(100), 1);
109
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -0700110 BOOST_CHECK_CLOSE(rttEstimator.m_sRtt.count(), 100, 0.1);
111 BOOST_CHECK_CLOSE(rttEstimator.m_rttVar.count(), 50, 0.1);
112 BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 300, 0.1);
Weiwei Liu245d7912016-07-28 00:04:25 -0700113
114 rttEstimator.m_sRtt = Milliseconds(500.0);
115 rttEstimator.m_rttVar = Milliseconds(100.0);
116 rttEstimator.m_rto = Milliseconds(900.0);
117
118 size_t nExpectedSamples = 1;
119 rttEstimator.addMeasurement(1, Milliseconds(100), nExpectedSamples);
120
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -0700121 BOOST_CHECK_CLOSE(rttEstimator.m_sRtt.count(), 450, 0.1);
122 BOOST_CHECK_CLOSE(rttEstimator.m_rttVar.count(), 175, 0.1);
Weiwei Liu245d7912016-07-28 00:04:25 -0700123 BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 1150, 0.1);
124
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -0700125 // expected samples larger than 1
Weiwei Liu245d7912016-07-28 00:04:25 -0700126 nExpectedSamples = 5;
127 rttEstimator.addMeasurement(1, Milliseconds(100), nExpectedSamples);
128
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -0700129 BOOST_CHECK_CLOSE(rttEstimator.m_sRtt.count(), 441.25, 0.1);
130 BOOST_CHECK_CLOSE(rttEstimator.m_rttVar.count(), 183.75, 0.1);
Weiwei Liu245d7912016-07-28 00:04:25 -0700131 BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 1176.25, 0.1);
132
133 rttEstimator.m_sRtt = Milliseconds(100.0);
134 rttEstimator.m_rttVar = Milliseconds(30.0);
135 rttEstimator.m_rto = Milliseconds(220.0);
136
137 // check if minRto works
138 nExpectedSamples = 1;
139 rttEstimator.addMeasurement(1, Milliseconds(100), nExpectedSamples);
140
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -0700141 BOOST_CHECK_CLOSE(rttEstimator.m_sRtt.count(), 100, 0.1);
142 BOOST_CHECK_CLOSE(rttEstimator.m_rttVar.count(), 22.5, 0.1);
143 BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 200, 0.1);
Weiwei Liu245d7912016-07-28 00:04:25 -0700144
145 rttEstimator.m_sRtt = Milliseconds(2000.0);
146 rttEstimator.m_rttVar = Milliseconds(400.0);
147 rttEstimator.m_rto = Milliseconds(3600.0);
148
149 // check if maxRto works
150 nExpectedSamples = 1;
151 rttEstimator.addMeasurement(1, Milliseconds(100), nExpectedSamples);
152
153 BOOST_CHECK_CLOSE(rttEstimator.m_sRtt.count(), 1762.5, 0.1);
154 BOOST_CHECK_CLOSE(rttEstimator.m_rttVar.count(), 775, 0.1);
155 BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 4000, 0.1);
156}
157
158BOOST_AUTO_TEST_CASE(RtoBackoff)
159{
160 rttEstimator.m_rto = Milliseconds(500.0);
161 rttEstimator.backoffRto();
162 BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 1000, 0.1);
163
164 // check if minRto works
165 rttEstimator.m_rto = Milliseconds(10.0);
166 rttEstimator.backoffRto();
167 BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 200, 0.1);
168
169 // check if maxRto works
170 rttEstimator.m_rto = Milliseconds(3000.0);
171 rttEstimator.backoffRto();
172 BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 4000, 0.1);
173}
174
175BOOST_AUTO_TEST_SUITE_END() // TestAimdRttEstimator
176BOOST_AUTO_TEST_SUITE_END() // Chunks
177
178} // namespace tests
179} // namespace aimd
180} // namespace chunks
181} // namespace ndn