blob: 9d5423a6a637b983d49011ad795776c79b455640 [file] [log] [blame]
Junxiao Shi2713a3b2015-06-22 16:19:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventob3570c62022-02-19 19:19:00 -05002/*
3 * Copyright (c) 2014-2022, Arizona Board of Regents.
Junxiao Shi2713a3b2015-06-22 16:19:05 -07004 *
5 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
7 *
8 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "tools/ping/client/statistics-collector.hpp"
Junxiao Shi2713a3b2015-06-22 16:19:05 -070021
22#include "tests/test-common.hpp"
Davide Pesaventob3570c62022-02-19 19:19:00 -050023
Davide Pesavento013de9b2016-09-01 12:06:56 +000024#include <ndn-cxx/util/dummy-client-face.hpp>
Junxiao Shi2713a3b2015-06-22 16:19:05 -070025
Davide Pesaventob3570c62022-02-19 19:19:00 -050026namespace ndn::ping::client::tests {
Junxiao Shi2713a3b2015-06-22 16:19:05 -070027
28class StatisticsCollectorFixture
29{
Junxiao Shi2713a3b2015-06-22 16:19:05 -070030private:
31 static Options
32 makeOptions()
33 {
34 Options opt;
Davide Pesaventob3570c62022-02-19 19:19:00 -050035 opt.prefix = "/ping-prefix";
Junxiao Shi2713a3b2015-06-22 16:19:05 -070036 opt.shouldAllowStaleData = false;
37 opt.shouldGenerateRandomSeq = false;
38 opt.shouldPrintTimestamp = false;
39 opt.nPings = 5;
Davide Pesaventob3570c62022-02-19 19:19:00 -050040 opt.interval = 100_ms;
41 opt.timeout = 2_s;
Junxiao Shi2713a3b2015-06-22 16:19:05 -070042 opt.startSeq = 1;
43 return opt;
44 }
45
46protected:
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000047 util::DummyClientFace face;
Davide Pesaventob3570c62022-02-19 19:19:00 -050048 Options pingOptions{makeOptions()};
49 Ping pingProgram{face, pingOptions};
50 StatisticsCollector sc{pingProgram, pingOptions};
Junxiao Shi2713a3b2015-06-22 16:19:05 -070051};
52
Davide Pesavento013de9b2016-09-01 12:06:56 +000053BOOST_AUTO_TEST_SUITE(Ping)
54BOOST_FIXTURE_TEST_SUITE(TestStatisticsCollector, StatisticsCollectorFixture)
Junxiao Shi2713a3b2015-06-22 16:19:05 -070055
56BOOST_AUTO_TEST_CASE(Resp50msResp50ms)
57{
Teng Liang62884862016-03-31 18:15:36 -070058 sc.recordData(time::milliseconds(50));
Junxiao Shi2713a3b2015-06-22 16:19:05 -070059
60 Statistics stats1 = sc.computeStatistics();
61 BOOST_CHECK_EQUAL(stats1.prefix, pingOptions.prefix);
62 BOOST_CHECK_EQUAL(stats1.nSent, 1);
63 BOOST_CHECK_EQUAL(stats1.nReceived, 1);
64 BOOST_CHECK_CLOSE(stats1.minRtt, 50.0, 0.001);
65 BOOST_CHECK_CLOSE(stats1.maxRtt, 50.0, 0.001);
66 BOOST_CHECK_CLOSE(stats1.packetLossRate, 0.0, 0.001);
67 BOOST_CHECK_CLOSE(stats1.sumRtt, 50.0, 0.001);
68 BOOST_CHECK_CLOSE(stats1.avgRtt, 50.0, 0.001);
69 BOOST_CHECK_CLOSE(stats1.stdDevRtt, 0.0, 0.001);
70
Teng Liang62884862016-03-31 18:15:36 -070071 sc.recordData(time::milliseconds(50));
Junxiao Shi2713a3b2015-06-22 16:19:05 -070072
73 Statistics stats2 = sc.computeStatistics();
74 BOOST_CHECK_EQUAL(stats2.prefix, pingOptions.prefix);
75 BOOST_CHECK_EQUAL(stats2.nSent, 2);
76 BOOST_CHECK_EQUAL(stats2.nReceived, 2);
77 BOOST_CHECK_CLOSE(stats2.minRtt, 50.0, 0.001);
78 BOOST_CHECK_CLOSE(stats2.maxRtt, 50.0, 0.001);
79 BOOST_CHECK_CLOSE(stats2.packetLossRate, 0.0, 0.001);
80 BOOST_CHECK_CLOSE(stats2.sumRtt, 100.0, 0.001);
81 BOOST_CHECK_CLOSE(stats2.avgRtt, 50.0, 0.001);
82 BOOST_CHECK_CLOSE(stats2.stdDevRtt, 0.0, 0.001);
83}
84
85BOOST_AUTO_TEST_CASE(Resp50msResp100ms)
86{
Teng Liang62884862016-03-31 18:15:36 -070087 sc.recordData(time::milliseconds(50));
88 sc.recordData(time::milliseconds(100));
Junxiao Shi2713a3b2015-06-22 16:19:05 -070089
90 Statistics stats = sc.computeStatistics();
91 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
92 BOOST_CHECK_EQUAL(stats.nSent, 2);
93 BOOST_CHECK_EQUAL(stats.nReceived, 2);
94 BOOST_CHECK_CLOSE(stats.minRtt, 50.0, 0.001);
95 BOOST_CHECK_CLOSE(stats.maxRtt, 100.0, 0.001);
96 BOOST_CHECK_CLOSE(stats.packetLossRate, 0.0, 0.001);
97 BOOST_CHECK_CLOSE(stats.sumRtt, 150.0, 0.001);
98 BOOST_CHECK_CLOSE(stats.avgRtt, 75.0, 0.001);
99 BOOST_CHECK_CLOSE(stats.stdDevRtt, 25.0, 0.001);
100}
101
102BOOST_AUTO_TEST_CASE(LossLoss)
103{
104 sc.recordTimeout();
105 sc.recordTimeout();
106
107 Statistics stats = sc.computeStatistics();
108 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
109 BOOST_CHECK_EQUAL(stats.nSent, 2);
110 BOOST_CHECK_EQUAL(stats.nReceived, 0);
Eric Newberryf1628392016-12-24 19:40:39 -0700111 BOOST_CHECK_EQUAL(stats.nNacked, 0);
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700112 BOOST_CHECK_CLOSE(stats.packetLossRate, 1.0, 0.001);
Eric Newberryf1628392016-12-24 19:40:39 -0700113 BOOST_CHECK_CLOSE(stats.packetNackedRate, 0.0, 0.001);
114 BOOST_CHECK_CLOSE(stats.minRtt, std::numeric_limits<double>::max(), 0.001);
115 BOOST_CHECK_CLOSE(stats.maxRtt, 0.0, 0.001);
116 BOOST_CHECK_CLOSE(stats.sumRtt, 0.0, 0.001);
117 BOOST_CHECK(std::isnan(stats.avgRtt));
118 BOOST_CHECK(std::isnan(stats.stdDevRtt));
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700119}
120
121BOOST_AUTO_TEST_CASE(Resp50msLoss)
122{
Teng Liang62884862016-03-31 18:15:36 -0700123 sc.recordData(time::milliseconds(50));
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700124 sc.recordTimeout();
125
126 Statistics stats = sc.computeStatistics();
127 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
128 BOOST_CHECK_EQUAL(stats.nSent, 2);
129 BOOST_CHECK_EQUAL(stats.nReceived, 1);
130 BOOST_CHECK_CLOSE(stats.minRtt, 50.0, 0.001);
131 BOOST_CHECK_CLOSE(stats.maxRtt, 50.0, 0.001);
132 BOOST_CHECK_CLOSE(stats.packetLossRate, 0.5, 0.001);
133 BOOST_CHECK_CLOSE(stats.sumRtt, 50.0, 0.001);
134 BOOST_CHECK_CLOSE(stats.avgRtt, 50.0, 0.001);
135 BOOST_CHECK_CLOSE(stats.stdDevRtt, 0.0, 0.001);
136}
137
Teng Liang62884862016-03-31 18:15:36 -0700138BOOST_AUTO_TEST_CASE(NackNack)
139{
140 sc.recordNack();
141 sc.recordNack();
142
143 Statistics stats = sc.computeStatistics();
144 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
145 BOOST_CHECK_EQUAL(stats.nSent, 2);
146 BOOST_CHECK_EQUAL(stats.nNacked, 2);
147 BOOST_CHECK_EQUAL(stats.nReceived, 0);
148 BOOST_CHECK_CLOSE(stats.packetNackedRate, 1.0, 0.001);
149}
150
151BOOST_AUTO_TEST_CASE(Resp50msNack)
152{
153 sc.recordData(time::milliseconds(50));
154 sc.recordNack();
155
156 Statistics stats = sc.computeStatistics();
157 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
158 BOOST_CHECK_EQUAL(stats.nSent, 2);
159 BOOST_CHECK_EQUAL(stats.nReceived, 1);
160 BOOST_CHECK_CLOSE(stats.minRtt, 50.0, 0.001);
161 BOOST_CHECK_CLOSE(stats.maxRtt, 50.0, 0.001);
162 BOOST_CHECK_CLOSE(stats.sumRtt, 50.0, 0.001);
163 BOOST_CHECK_CLOSE(stats.avgRtt, 50.0, 0.001);
164 BOOST_CHECK_CLOSE(stats.stdDevRtt, 0.0, 0.001);
165 BOOST_CHECK_EQUAL(stats.nNacked, 1);
166 BOOST_CHECK_CLOSE(stats.packetNackedRate, 0.5, 0.001);
167}
168
169BOOST_AUTO_TEST_CASE(NackLoss)
170{
171 sc.recordNack();
172 sc.recordTimeout();
173
174 Statistics stats = sc.computeStatistics();
175 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
176 BOOST_CHECK_EQUAL(stats.nSent, 2);
177 BOOST_CHECK_EQUAL(stats.nReceived, 0);
178 BOOST_CHECK_EQUAL(stats.nNacked, 1);
179 BOOST_CHECK_CLOSE(stats.packetNackedRate, 0.5, 0.001);
180 BOOST_CHECK_CLOSE(stats.packetLossRate, 0.5, 0.001);
181}
182
Eric Newberryf1628392016-12-24 19:40:39 -0700183BOOST_AUTO_TEST_CASE(NoneSent)
184{
185 Statistics stats = sc.computeStatistics();
186 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
187 BOOST_CHECK_EQUAL(stats.nSent, 0);
188 BOOST_CHECK_EQUAL(stats.nReceived, 0);
189 BOOST_CHECK_EQUAL(stats.nNacked, 0);
190 BOOST_CHECK(std::isnan(stats.packetLossRate));
191 BOOST_CHECK(std::isnan(stats.packetNackedRate));
192 BOOST_CHECK_CLOSE(stats.minRtt, std::numeric_limits<double>::max(), 0.001);
193 BOOST_CHECK_CLOSE(stats.maxRtt, 0.0, 0.001);
194 BOOST_CHECK_CLOSE(stats.sumRtt, 0.0, 0.001);
195 BOOST_CHECK(std::isnan(stats.avgRtt));
196 BOOST_CHECK(std::isnan(stats.stdDevRtt));
197}
198
Davide Pesavento013de9b2016-09-01 12:06:56 +0000199BOOST_AUTO_TEST_SUITE_END() // TestStatisticsCollector
200BOOST_AUTO_TEST_SUITE_END() // Ping
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700201
Davide Pesaventob3570c62022-02-19 19:19:00 -0500202} // namespace ndn::ping::client::tests