Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2022, Arizona Board of Regents. |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 4 | * |
| 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 Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 21 | |
| 22 | #include "tests/test-common.hpp" |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 23 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 24 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 26 | namespace ndn::ping::client::tests { |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 27 | |
| 28 | class StatisticsCollectorFixture |
| 29 | { |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 30 | private: |
| 31 | static Options |
| 32 | makeOptions() |
| 33 | { |
| 34 | Options opt; |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 35 | opt.prefix = "/ping-prefix"; |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 36 | opt.shouldAllowStaleData = false; |
| 37 | opt.shouldGenerateRandomSeq = false; |
| 38 | opt.shouldPrintTimestamp = false; |
| 39 | opt.nPings = 5; |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 40 | opt.interval = 100_ms; |
| 41 | opt.timeout = 2_s; |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 42 | opt.startSeq = 1; |
| 43 | return opt; |
| 44 | } |
| 45 | |
| 46 | protected: |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 47 | util::DummyClientFace face; |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 48 | Options pingOptions{makeOptions()}; |
| 49 | Ping pingProgram{face, pingOptions}; |
| 50 | StatisticsCollector sc{pingProgram, pingOptions}; |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 53 | BOOST_AUTO_TEST_SUITE(Ping) |
| 54 | BOOST_FIXTURE_TEST_SUITE(TestStatisticsCollector, StatisticsCollectorFixture) |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 55 | |
| 56 | BOOST_AUTO_TEST_CASE(Resp50msResp50ms) |
| 57 | { |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 58 | sc.recordData(time::milliseconds(50)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 59 | |
| 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 Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 71 | sc.recordData(time::milliseconds(50)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 72 | |
| 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 | |
| 85 | BOOST_AUTO_TEST_CASE(Resp50msResp100ms) |
| 86 | { |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 87 | sc.recordData(time::milliseconds(50)); |
| 88 | sc.recordData(time::milliseconds(100)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 89 | |
| 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 | |
| 102 | BOOST_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 Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 111 | BOOST_CHECK_EQUAL(stats.nNacked, 0); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 112 | BOOST_CHECK_CLOSE(stats.packetLossRate, 1.0, 0.001); |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 113 | 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 Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | BOOST_AUTO_TEST_CASE(Resp50msLoss) |
| 122 | { |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 123 | sc.recordData(time::milliseconds(50)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 124 | 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 Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 138 | BOOST_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 | |
| 151 | BOOST_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 | |
| 169 | BOOST_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 Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 183 | BOOST_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 Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 199 | BOOST_AUTO_TEST_SUITE_END() // TestStatisticsCollector |
| 200 | BOOST_AUTO_TEST_SUITE_END() // Ping |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 201 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 202 | } // namespace ndn::ping::client::tests |