blob: e2ff3fbd59b21e003c6a80652029e4f367bff380 [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/*
schneiderklausd8197df2019-03-16 11:31:40 -07003 * Copyright (c) 2016-2019, Regents of the University of California,
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -07004 * 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
schneiderklausd8197df2019-03-16 11:31:40 -070027#include "tools/chunks/catchunks/rtt-estimator.hpp"
Weiwei Liu245d7912016-07-28 00:04:25 -070028#include "tests/test-common.hpp"
29
30namespace ndn {
31namespace chunks {
Weiwei Liu245d7912016-07-28 00:04:25 -070032namespace tests {
33
34class RttEstimatorFixture
35{
36protected:
37 RttEstimatorFixture()
38 : options(makeRttEstimatorOptions())
39 , rttEstimator(options)
40 {
41 }
42
43private:
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
56protected:
57 RttEstimator::Options options;
58 RttEstimator rttEstimator;
59};
60
61BOOST_AUTO_TEST_SUITE(Chunks)
schneiderklausd8197df2019-03-16 11:31:40 -070062BOOST_FIXTURE_TEST_SUITE(TestRttEstimator, RttEstimatorFixture)
Weiwei Liu245d7912016-07-28 00:04:25 -070063
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -070064BOOST_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 Liu245d7912016-07-28 00:04:25 -070097BOOST_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 Ghasemi3dae1092017-12-21 12:39:08 -0700106 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 Liu245d7912016-07-28 00:04:25 -0700109
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 Ghasemi3dae1092017-12-21 12:39:08 -0700117 BOOST_CHECK_CLOSE(rttEstimator.m_sRtt.count(), 450, 0.1);
118 BOOST_CHECK_CLOSE(rttEstimator.m_rttVar.count(), 175, 0.1);
Weiwei Liu245d7912016-07-28 00:04:25 -0700119 BOOST_CHECK_CLOSE(rttEstimator.m_rto.count(), 1150, 0.1);
120
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -0700121 // expected samples larger than 1
Weiwei Liu245d7912016-07-28 00:04:25 -0700122 nExpectedSamples = 5;
123 rttEstimator.addMeasurement(1, Milliseconds(100), nExpectedSamples);
124
Chavoosh Ghasemi3dae1092017-12-21 12:39:08 -0700125 BOOST_CHECK_CLOSE(rttEstimator.m_sRtt.count(), 441.25, 0.1);
126 BOOST_CHECK_CLOSE(rttEstimator.m_rttVar.count(), 183.75, 0.1);
Weiwei Liu245d7912016-07-28 00:04:25 -0700127 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 Ghasemi3dae1092017-12-21 12:39:08 -0700137 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 Liu245d7912016-07-28 00:04:25 -0700140
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
154BOOST_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
schneiderklausd8197df2019-03-16 11:31:40 -0700171BOOST_AUTO_TEST_SUITE_END() // TestRttEstimator
Weiwei Liu245d7912016-07-28 00:04:25 -0700172BOOST_AUTO_TEST_SUITE_END() // Chunks
173
174} // namespace tests
Weiwei Liu245d7912016-07-28 00:04:25 -0700175} // namespace chunks
176} // namespace ndn