blob: b819c9b6e7ecbc05c258bedf5104dc82e19156d9 [file] [log] [blame]
Davide Pesavento4ad933a2019-06-02 21:15:42 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2016-2019, Regents of the University of California,
4 * 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
30namespace ndn {
31namespace util {
32namespace tests {
33
34BOOST_AUTO_TEST_SUITE(Util)
35BOOST_AUTO_TEST_SUITE(TestRttEstimator)
36
Davide Pesavento4c5b7b82019-08-20 19:03:10 -040037BOOST_AUTO_TEST_CASE(CopyAssign)
Davide Pesavento4ad933a2019-06-02 21:15:42 -040038{
Davide Pesavento4c5b7b82019-08-20 19:03:10 -040039 RttEstimator est1;
40 RttEstimator est2;
41
42 est1.addMeasurement(100_ms);
43 est1.addMeasurement(150_ms);
44 est2.addMeasurement(75_ms);
45 est2.addMeasurement(70_ms);
46
47 BOOST_CHECK_NE(est1.getEstimatedRto(), est2.getEstimatedRto());
48 BOOST_CHECK_NE(est1.getRttVariation(), est2.getRttVariation());
49 BOOST_CHECK_NE(est1.getSmoothedRtt(), est2.getSmoothedRtt());
50
51 est1 = est2;
52
53 BOOST_CHECK_EQUAL(est1.getEstimatedRto(), est2.getEstimatedRto());
54 BOOST_CHECK_EQUAL(est1.getRttVariation(), est2.getRttVariation());
55 BOOST_CHECK_EQUAL(est1.getSmoothedRtt(), est2.getSmoothedRtt());
56}
57
58BOOST_AUTO_TEST_CASE(EstimatedRto)
59{
60 auto opts = make_shared<RttEstimator::Options>();
61 opts->initialRto = 400_ms;
62 opts->maxRto = 2_s;
63 RttEstimator rttEstimator(opts);
64
65 // check initial values
66 BOOST_CHECK_EQUAL(rttEstimator.hasSamples(), false);
67 BOOST_CHECK_EQUAL(rttEstimator.getSmoothedRtt(), -1_ns);
68 BOOST_CHECK_EQUAL(rttEstimator.getRttVariation(), -1_ns);
69 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), opts->initialRto);
70
71 // first measurement
72 rttEstimator.addMeasurement(200_ms);
73
74 BOOST_CHECK_EQUAL(rttEstimator.hasSamples(), true);
75 BOOST_CHECK_EQUAL(rttEstimator.getSmoothedRtt(), 200_ms);
76 BOOST_CHECK_EQUAL(rttEstimator.getRttVariation(), 100_ms);
77 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), 600_ms);
78
79 rttEstimator.addMeasurement(100_ms, 1);
80
81 BOOST_CHECK_EQUAL(rttEstimator.hasSamples(), true);
82 BOOST_CHECK_EQUAL(rttEstimator.getSmoothedRtt(), 187500_us);
83 BOOST_CHECK_EQUAL(rttEstimator.getRttVariation(), 100000_us);
84 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), 587500_us);
85
86 // expected samples larger than 1
87 rttEstimator.addMeasurement(50_ms, 5);
88
89 BOOST_CHECK_EQUAL(rttEstimator.hasSamples(), true);
90 BOOST_CHECK_EQUAL(rttEstimator.getSmoothedRtt(), 184062500_ns);
91 BOOST_CHECK_EQUAL(rttEstimator.getRttVariation(), 101875000_ns);
92 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), 591562500_ns);
93
94 // check if minRto works
95 for (int i = 0; i < 20; i++) {
96 rttEstimator.addMeasurement(10_ms);
97 }
98
99 BOOST_CHECK_EQUAL(rttEstimator.getSmoothedRtt(), 22046646_ns);
100 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), opts->minRto);
101
102 // check if maxRto works
103 for (int i = 0; i < 10; i++) {
104 rttEstimator.addMeasurement(1_s);
105 rttEstimator.addMeasurement(10_ms);
106 }
107
108 BOOST_CHECK_EQUAL(rttEstimator.getSmoothedRtt(), 440859284_ns);
109 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), opts->maxRto);
110}
111
112BOOST_AUTO_TEST_CASE(BackoffRto)
113{
114 auto opts = make_shared<RttEstimator::Options>();
115 opts->initialRto = 500_ms;
116 opts->maxRto = 4_s;
117 RttEstimator rttEstimator(opts);
118
119 rttEstimator.backoffRto();
120 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), 1_s);
121
122 // check if minRto works
123 for (int i = 0; i < 10; i++) {
124 rttEstimator.addMeasurement(5_ms);
125 }
126 rttEstimator.backoffRto();
127 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), 400_ms);
128
129 // check if maxRto works
130 for (int i = 0; i < 10; i++) {
131 rttEstimator.addMeasurement(5_s);
132 }
133 rttEstimator.backoffRto();
134 BOOST_CHECK_EQUAL(rttEstimator.getEstimatedRto(), 4_s);
135}
136
137BOOST_AUTO_TEST_CASE(Stats)
138{
139 RttEstimatorWithStats rttEstimator;
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400140
141 // check initial values
Davide Pesavento57a32762019-06-24 02:36:21 -0400142 BOOST_CHECK_EQUAL(rttEstimator.getMinRtt().count(), std::numeric_limits<time::nanoseconds::rep>::max());
143 BOOST_CHECK_EQUAL(rttEstimator.getAvgRtt().count(), 0);
144 BOOST_CHECK_EQUAL(rttEstimator.getMaxRtt().count(), std::numeric_limits<time::nanoseconds::rep>::min());
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400145
146 // start with three samples
Davide Pesavento4c5b7b82019-08-20 19:03:10 -0400147 rttEstimator.addMeasurement(100_ms);
148 rttEstimator.addMeasurement(400_ms);
149 rttEstimator.addMeasurement(250_ms);
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400150
Davide Pesavento57a32762019-06-24 02:36:21 -0400151 BOOST_CHECK_EQUAL(rttEstimator.getMinRtt(), 100_ms);
152 BOOST_CHECK_EQUAL(rttEstimator.getAvgRtt(), 250_ms);
153 BOOST_CHECK_EQUAL(rttEstimator.getMaxRtt(), 400_ms);
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400154
155 // add another sample (new minimum)
Davide Pesavento57a32762019-06-24 02:36:21 -0400156 rttEstimator.addMeasurement(50_ms, 2);
157 BOOST_CHECK_EQUAL(rttEstimator.getMinRtt(), 50_ms);
158 BOOST_CHECK_EQUAL(rttEstimator.getAvgRtt(), 200_ms);
159 BOOST_CHECK_EQUAL(rttEstimator.getMaxRtt(), 400_ms);
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400160
161 // add another sample (new maximum)
Davide Pesavento57a32762019-06-24 02:36:21 -0400162 rttEstimator.addMeasurement(700_ms, 1);
163 BOOST_CHECK_EQUAL(rttEstimator.getMinRtt(), 50_ms);
164 BOOST_CHECK_EQUAL(rttEstimator.getAvgRtt(), 300_ms);
165 BOOST_CHECK_EQUAL(rttEstimator.getMaxRtt(), 700_ms);
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400166}
167
Davide Pesavento4ad933a2019-06-02 21:15:42 -0400168BOOST_AUTO_TEST_SUITE_END() // TestRttEstimator
169BOOST_AUTO_TEST_SUITE_END() // Util
170
171} // namespace tests
172} // namespace util
173} // namespace ndn