blob: 30f2c4be3147b639364fbadffb047395c5cc3c4c [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/*
Davide Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2014-2024, 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>
Davide Pesavento5748e822024-01-26 18:40:22 -050025#include <cmath>
Junxiao Shi2713a3b2015-06-22 16:19:05 -070026
Davide Pesaventob3570c62022-02-19 19:19:00 -050027namespace ndn::ping::client::tests {
Junxiao Shi2713a3b2015-06-22 16:19:05 -070028
29class StatisticsCollectorFixture
30{
Junxiao Shi2713a3b2015-06-22 16:19:05 -070031private:
32 static Options
33 makeOptions()
34 {
35 Options opt;
Davide Pesaventob3570c62022-02-19 19:19:00 -050036 opt.prefix = "/ping-prefix";
Junxiao Shi2713a3b2015-06-22 16:19:05 -070037 opt.shouldAllowStaleData = false;
38 opt.shouldGenerateRandomSeq = false;
39 opt.shouldPrintTimestamp = false;
40 opt.nPings = 5;
Davide Pesaventob3570c62022-02-19 19:19:00 -050041 opt.interval = 100_ms;
42 opt.timeout = 2_s;
Junxiao Shi2713a3b2015-06-22 16:19:05 -070043 opt.startSeq = 1;
44 return opt;
45 }
46
47protected:
Junxiao Shi869d73e2023-08-10 22:52:26 +000048 DummyClientFace face;
Davide Pesaventob3570c62022-02-19 19:19:00 -050049 Options pingOptions{makeOptions()};
50 Ping pingProgram{face, pingOptions};
51 StatisticsCollector sc{pingProgram, pingOptions};
Junxiao Shi2713a3b2015-06-22 16:19:05 -070052};
53
Davide Pesavento013de9b2016-09-01 12:06:56 +000054BOOST_AUTO_TEST_SUITE(Ping)
55BOOST_FIXTURE_TEST_SUITE(TestStatisticsCollector, StatisticsCollectorFixture)
Junxiao Shi2713a3b2015-06-22 16:19:05 -070056
57BOOST_AUTO_TEST_CASE(Resp50msResp50ms)
58{
Teng Liang62884862016-03-31 18:15:36 -070059 sc.recordData(time::milliseconds(50));
Junxiao Shi2713a3b2015-06-22 16:19:05 -070060
61 Statistics stats1 = sc.computeStatistics();
62 BOOST_CHECK_EQUAL(stats1.prefix, pingOptions.prefix);
63 BOOST_CHECK_EQUAL(stats1.nSent, 1);
64 BOOST_CHECK_EQUAL(stats1.nReceived, 1);
65 BOOST_CHECK_CLOSE(stats1.minRtt, 50.0, 0.001);
66 BOOST_CHECK_CLOSE(stats1.maxRtt, 50.0, 0.001);
67 BOOST_CHECK_CLOSE(stats1.packetLossRate, 0.0, 0.001);
68 BOOST_CHECK_CLOSE(stats1.sumRtt, 50.0, 0.001);
69 BOOST_CHECK_CLOSE(stats1.avgRtt, 50.0, 0.001);
70 BOOST_CHECK_CLOSE(stats1.stdDevRtt, 0.0, 0.001);
71
Teng Liang62884862016-03-31 18:15:36 -070072 sc.recordData(time::milliseconds(50));
Junxiao Shi2713a3b2015-06-22 16:19:05 -070073
74 Statistics stats2 = sc.computeStatistics();
75 BOOST_CHECK_EQUAL(stats2.prefix, pingOptions.prefix);
76 BOOST_CHECK_EQUAL(stats2.nSent, 2);
77 BOOST_CHECK_EQUAL(stats2.nReceived, 2);
78 BOOST_CHECK_CLOSE(stats2.minRtt, 50.0, 0.001);
79 BOOST_CHECK_CLOSE(stats2.maxRtt, 50.0, 0.001);
80 BOOST_CHECK_CLOSE(stats2.packetLossRate, 0.0, 0.001);
81 BOOST_CHECK_CLOSE(stats2.sumRtt, 100.0, 0.001);
82 BOOST_CHECK_CLOSE(stats2.avgRtt, 50.0, 0.001);
83 BOOST_CHECK_CLOSE(stats2.stdDevRtt, 0.0, 0.001);
84}
85
86BOOST_AUTO_TEST_CASE(Resp50msResp100ms)
87{
Teng Liang62884862016-03-31 18:15:36 -070088 sc.recordData(time::milliseconds(50));
89 sc.recordData(time::milliseconds(100));
Junxiao Shi2713a3b2015-06-22 16:19:05 -070090
91 Statistics stats = sc.computeStatistics();
92 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
93 BOOST_CHECK_EQUAL(stats.nSent, 2);
94 BOOST_CHECK_EQUAL(stats.nReceived, 2);
95 BOOST_CHECK_CLOSE(stats.minRtt, 50.0, 0.001);
96 BOOST_CHECK_CLOSE(stats.maxRtt, 100.0, 0.001);
97 BOOST_CHECK_CLOSE(stats.packetLossRate, 0.0, 0.001);
98 BOOST_CHECK_CLOSE(stats.sumRtt, 150.0, 0.001);
99 BOOST_CHECK_CLOSE(stats.avgRtt, 75.0, 0.001);
100 BOOST_CHECK_CLOSE(stats.stdDevRtt, 25.0, 0.001);
101}
102
103BOOST_AUTO_TEST_CASE(LossLoss)
104{
105 sc.recordTimeout();
106 sc.recordTimeout();
107
108 Statistics stats = sc.computeStatistics();
109 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
110 BOOST_CHECK_EQUAL(stats.nSent, 2);
111 BOOST_CHECK_EQUAL(stats.nReceived, 0);
Eric Newberryf1628392016-12-24 19:40:39 -0700112 BOOST_CHECK_EQUAL(stats.nNacked, 0);
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700113 BOOST_CHECK_CLOSE(stats.packetLossRate, 1.0, 0.001);
Eric Newberryf1628392016-12-24 19:40:39 -0700114 BOOST_CHECK_CLOSE(stats.packetNackedRate, 0.0, 0.001);
115 BOOST_CHECK_CLOSE(stats.minRtt, std::numeric_limits<double>::max(), 0.001);
116 BOOST_CHECK_CLOSE(stats.maxRtt, 0.0, 0.001);
117 BOOST_CHECK_CLOSE(stats.sumRtt, 0.0, 0.001);
118 BOOST_CHECK(std::isnan(stats.avgRtt));
119 BOOST_CHECK(std::isnan(stats.stdDevRtt));
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700120}
121
122BOOST_AUTO_TEST_CASE(Resp50msLoss)
123{
Teng Liang62884862016-03-31 18:15:36 -0700124 sc.recordData(time::milliseconds(50));
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700125 sc.recordTimeout();
126
127 Statistics stats = sc.computeStatistics();
128 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
129 BOOST_CHECK_EQUAL(stats.nSent, 2);
130 BOOST_CHECK_EQUAL(stats.nReceived, 1);
131 BOOST_CHECK_CLOSE(stats.minRtt, 50.0, 0.001);
132 BOOST_CHECK_CLOSE(stats.maxRtt, 50.0, 0.001);
133 BOOST_CHECK_CLOSE(stats.packetLossRate, 0.5, 0.001);
134 BOOST_CHECK_CLOSE(stats.sumRtt, 50.0, 0.001);
135 BOOST_CHECK_CLOSE(stats.avgRtt, 50.0, 0.001);
136 BOOST_CHECK_CLOSE(stats.stdDevRtt, 0.0, 0.001);
137}
138
Teng Liang62884862016-03-31 18:15:36 -0700139BOOST_AUTO_TEST_CASE(NackNack)
140{
141 sc.recordNack();
142 sc.recordNack();
143
144 Statistics stats = sc.computeStatistics();
145 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
146 BOOST_CHECK_EQUAL(stats.nSent, 2);
147 BOOST_CHECK_EQUAL(stats.nNacked, 2);
148 BOOST_CHECK_EQUAL(stats.nReceived, 0);
149 BOOST_CHECK_CLOSE(stats.packetNackedRate, 1.0, 0.001);
150}
151
152BOOST_AUTO_TEST_CASE(Resp50msNack)
153{
154 sc.recordData(time::milliseconds(50));
155 sc.recordNack();
156
157 Statistics stats = sc.computeStatistics();
158 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
159 BOOST_CHECK_EQUAL(stats.nSent, 2);
160 BOOST_CHECK_EQUAL(stats.nReceived, 1);
161 BOOST_CHECK_CLOSE(stats.minRtt, 50.0, 0.001);
162 BOOST_CHECK_CLOSE(stats.maxRtt, 50.0, 0.001);
163 BOOST_CHECK_CLOSE(stats.sumRtt, 50.0, 0.001);
164 BOOST_CHECK_CLOSE(stats.avgRtt, 50.0, 0.001);
165 BOOST_CHECK_CLOSE(stats.stdDevRtt, 0.0, 0.001);
166 BOOST_CHECK_EQUAL(stats.nNacked, 1);
167 BOOST_CHECK_CLOSE(stats.packetNackedRate, 0.5, 0.001);
168}
169
170BOOST_AUTO_TEST_CASE(NackLoss)
171{
172 sc.recordNack();
173 sc.recordTimeout();
174
175 Statistics stats = sc.computeStatistics();
176 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
177 BOOST_CHECK_EQUAL(stats.nSent, 2);
178 BOOST_CHECK_EQUAL(stats.nReceived, 0);
179 BOOST_CHECK_EQUAL(stats.nNacked, 1);
180 BOOST_CHECK_CLOSE(stats.packetNackedRate, 0.5, 0.001);
181 BOOST_CHECK_CLOSE(stats.packetLossRate, 0.5, 0.001);
182}
183
Eric Newberryf1628392016-12-24 19:40:39 -0700184BOOST_AUTO_TEST_CASE(NoneSent)
185{
186 Statistics stats = sc.computeStatistics();
187 BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix);
188 BOOST_CHECK_EQUAL(stats.nSent, 0);
189 BOOST_CHECK_EQUAL(stats.nReceived, 0);
190 BOOST_CHECK_EQUAL(stats.nNacked, 0);
191 BOOST_CHECK(std::isnan(stats.packetLossRate));
192 BOOST_CHECK(std::isnan(stats.packetNackedRate));
193 BOOST_CHECK_CLOSE(stats.minRtt, std::numeric_limits<double>::max(), 0.001);
194 BOOST_CHECK_CLOSE(stats.maxRtt, 0.0, 0.001);
195 BOOST_CHECK_CLOSE(stats.sumRtt, 0.0, 0.001);
196 BOOST_CHECK(std::isnan(stats.avgRtt));
197 BOOST_CHECK(std::isnan(stats.stdDevRtt));
198}
199
Davide Pesavento013de9b2016-09-01 12:06:56 +0000200BOOST_AUTO_TEST_SUITE_END() // TestStatisticsCollector
201BOOST_AUTO_TEST_SUITE_END() // Ping
Junxiao Shi2713a3b2015-06-22 16:19:05 -0700202
Davide Pesaventob3570c62022-02-19 19:19:00 -0500203} // namespace ndn::ping::client::tests