blob: 7070fb0be38f542c56d2d09eb8d8f9355b4617a8 [file] [log] [blame]
Davide Pesavento4ad933a2019-06-02 21:15:42 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2016-2023, Regents of the University of California,
Davide Pesavento4ad933a2019-06-02 21:15:42 -04004 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
6 *
7 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
8 *
9 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later version.
12 *
13 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 *
17 * You should have received copies of the GNU General Public License and GNU Lesser
18 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
19 * <http://www.gnu.org/licenses/>.
20 *
21 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
22 */
23
24#include "ndn-cxx/util/rtt-estimator.hpp"
25
26#include "tests/boost-test.hpp"
27
28#include <cmath>
29
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040030namespace ndn::tests {
31
32using ndn::util::RttEstimator;
33using ndn::util::RttEstimatorWithStats;
Davide Pesavento4ad933a2019-06-02 21:15:42 -040034
35BOOST_AUTO_TEST_SUITE(Util)
36BOOST_AUTO_TEST_SUITE(TestRttEstimator)
37
Davide Pesavento4c5b7b82019-08-20 19:03:10 -040038BOOST_AUTO_TEST_CASE(CopyAssign)
Davide Pesavento4ad933a2019-06-02 21:15:42 -040039{
Davide Pesavento4c5b7b82019-08-20 19:03:10 -040040 RttEstimator est1;
41 RttEstimator est2;
42
43 est1.addMeasurement(100_ms);
44 est1.addMeasurement(150_ms);
45 est2.addMeasurement(75_ms);
46 est2.addMeasurement(70_ms);
47
48 BOOST_CHECK_NE(est1.getEstimatedRto(), est2.getEstimatedRto());
49 BOOST_CHECK_NE(est1.getRttVariation(), est2.getRttVariation());
50 BOOST_CHECK_NE(est1.getSmoothedRtt(), est2.getSmoothedRtt());
51
52 est1 = est2;
53
54 BOOST_CHECK_EQUAL(est1.getEstimatedRto(), est2.getEstimatedRto());
55 BOOST_CHECK_EQUAL(est1.getRttVariation(), est2.getRttVariation());
56 BOOST_CHECK_EQUAL(est1.getSmoothedRtt(), est2.getSmoothedRtt());
57}
58
59BOOST_AUTO_TEST_CASE(EstimatedRto)
60{
61 auto opts = make_shared<RttEstimator::Options>();
62 opts->initialRto = 400_ms;
63 opts->maxRto = 2_s;
64 RttEstimator rttEstimator(opts);
65
66 // check initial values
67 BOOST_CHECK_EQUAL(rttEstimator.hasSamples(), false);
68 BOOST_CHECK_EQUAL(rttEstimator.getSmoothedRtt(), -1_ns);
69 BOOST_CHECK_EQUAL(rttEstimator.getRttVariation(), -1_ns);
70 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), opts->initialRto);
71
72 // first measurement
73 rttEstimator.addMeasurement(200_ms);
74
75 BOOST_CHECK_EQUAL(rttEstimator.hasSamples(), true);
76 BOOST_CHECK_EQUAL(rttEstimator.getSmoothedRtt(), 200_ms);
77 BOOST_CHECK_EQUAL(rttEstimator.getRttVariation(), 100_ms);
78 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), 600_ms);
79
80 rttEstimator.addMeasurement(100_ms, 1);
81
82 BOOST_CHECK_EQUAL(rttEstimator.hasSamples(), true);
83 BOOST_CHECK_EQUAL(rttEstimator.getSmoothedRtt(), 187500_us);
84 BOOST_CHECK_EQUAL(rttEstimator.getRttVariation(), 100000_us);
85 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), 587500_us);
86
87 // expected samples larger than 1
88 rttEstimator.addMeasurement(50_ms, 5);
89
90 BOOST_CHECK_EQUAL(rttEstimator.hasSamples(), true);
91 BOOST_CHECK_EQUAL(rttEstimator.getSmoothedRtt(), 184062500_ns);
92 BOOST_CHECK_EQUAL(rttEstimator.getRttVariation(), 101875000_ns);
93 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), 591562500_ns);
94
95 // check if minRto works
96 for (int i = 0; i < 20; i++) {
97 rttEstimator.addMeasurement(10_ms);
98 }
99
100 BOOST_CHECK_EQUAL(rttEstimator.getSmoothedRtt(), 22046646_ns);
101 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), opts->minRto);
102
103 // check if maxRto works
104 for (int i = 0; i < 10; i++) {
105 rttEstimator.addMeasurement(1_s);
106 rttEstimator.addMeasurement(10_ms);
107 }
108
109 BOOST_CHECK_EQUAL(rttEstimator.getSmoothedRtt(), 440859284_ns);
110 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), opts->maxRto);
111}
112
113BOOST_AUTO_TEST_CASE(BackoffRto)
114{
115 auto opts = make_shared<RttEstimator::Options>();
116 opts->initialRto = 500_ms;
117 opts->maxRto = 4_s;
118 RttEstimator rttEstimator(opts);
119
120 rttEstimator.backoffRto();
121 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), 1_s);
122
123 // check if minRto works
124 for (int i = 0; i < 10; i++) {
125 rttEstimator.addMeasurement(5_ms);
126 }
127 rttEstimator.backoffRto();
128 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), 400_ms);
129
130 // check if maxRto works
131 for (int i = 0; i < 10; i++) {
132 rttEstimator.addMeasurement(5_s);
133 }
134 rttEstimator.backoffRto();
135 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), 4_s);
136}
137
138BOOST_AUTO_TEST_CASE(Stats)
139{
140 RttEstimatorWithStats rttEstimator;
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400141
142 // check initial values
Davide Pesavento57a32762019-06-24 02:36:21 -0400143 BOOST_CHECK_EQUAL(rttEstimator.getMinRtt().count(), std::numeric_limits<time::nanoseconds::rep>::max());
144 BOOST_CHECK_EQUAL(rttEstimator.getAvgRtt().count(), 0);
145 BOOST_CHECK_EQUAL(rttEstimator.getMaxRtt().count(), std::numeric_limits<time::nanoseconds::rep>::min());
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400146
147 // start with three samples
Davide Pesavento4c5b7b82019-08-20 19:03:10 -0400148 rttEstimator.addMeasurement(100_ms);
149 rttEstimator.addMeasurement(400_ms);
150 rttEstimator.addMeasurement(250_ms);
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400151
Davide Pesavento57a32762019-06-24 02:36:21 -0400152 BOOST_CHECK_EQUAL(rttEstimator.getMinRtt(), 100_ms);
153 BOOST_CHECK_EQUAL(rttEstimator.getAvgRtt(), 250_ms);
154 BOOST_CHECK_EQUAL(rttEstimator.getMaxRtt(), 400_ms);
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400155
156 // add another sample (new minimum)
Davide Pesavento57a32762019-06-24 02:36:21 -0400157 rttEstimator.addMeasurement(50_ms, 2);
158 BOOST_CHECK_EQUAL(rttEstimator.getMinRtt(), 50_ms);
159 BOOST_CHECK_EQUAL(rttEstimator.getAvgRtt(), 200_ms);
160 BOOST_CHECK_EQUAL(rttEstimator.getMaxRtt(), 400_ms);
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400161
162 // add another sample (new maximum)
Davide Pesavento57a32762019-06-24 02:36:21 -0400163 rttEstimator.addMeasurement(700_ms, 1);
164 BOOST_CHECK_EQUAL(rttEstimator.getMinRtt(), 50_ms);
165 BOOST_CHECK_EQUAL(rttEstimator.getAvgRtt(), 300_ms);
166 BOOST_CHECK_EQUAL(rttEstimator.getMaxRtt(), 700_ms);
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400167}
168
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400169BOOST_AUTO_TEST_SUITE_END() // TestRttEstimator
170BOOST_AUTO_TEST_SUITE_END() // Util
171
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400172} // namespace ndn::tests