Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 23 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 24 | |
| 25 | namespace ndn { |
| 26 | namespace ping { |
| 27 | namespace client { |
| 28 | namespace tests { |
| 29 | |
| 30 | using namespace ndn::tests; |
| 31 | |
| 32 | class StatisticsCollectorFixture |
| 33 | { |
| 34 | protected: |
| 35 | StatisticsCollectorFixture() |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 36 | : pingOptions(makeOptions()) |
| 37 | , pingProgram(face, pingOptions) |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 38 | , sc(pingProgram, pingOptions) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | static Options |
| 44 | makeOptions() |
| 45 | { |
| 46 | Options opt; |
| 47 | opt.prefix = "ndn:/ping-prefix"; |
| 48 | opt.shouldAllowStaleData = false; |
| 49 | opt.shouldGenerateRandomSeq = false; |
| 50 | opt.shouldPrintTimestamp = false; |
| 51 | opt.nPings = 5; |
| 52 | opt.interval = time::milliseconds(100); |
| 53 | opt.timeout = time::milliseconds(2000); |
| 54 | opt.startSeq = 1; |
| 55 | return opt; |
| 56 | } |
| 57 | |
| 58 | protected: |
Junxiao Shi | aa1b3c9 | 2016-07-14 14:56:53 +0000 | [diff] [blame] | 59 | util::DummyClientFace face; |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 60 | Options pingOptions; |
| 61 | Ping pingProgram; |
| 62 | StatisticsCollector sc; |
| 63 | }; |
| 64 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 65 | BOOST_AUTO_TEST_SUITE(Ping) |
| 66 | BOOST_FIXTURE_TEST_SUITE(TestStatisticsCollector, StatisticsCollectorFixture) |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 67 | |
| 68 | BOOST_AUTO_TEST_CASE(Resp50msResp50ms) |
| 69 | { |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 70 | sc.recordData(time::milliseconds(50)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 71 | |
| 72 | Statistics stats1 = sc.computeStatistics(); |
| 73 | BOOST_CHECK_EQUAL(stats1.prefix, pingOptions.prefix); |
| 74 | BOOST_CHECK_EQUAL(stats1.nSent, 1); |
| 75 | BOOST_CHECK_EQUAL(stats1.nReceived, 1); |
| 76 | BOOST_CHECK_CLOSE(stats1.minRtt, 50.0, 0.001); |
| 77 | BOOST_CHECK_CLOSE(stats1.maxRtt, 50.0, 0.001); |
| 78 | BOOST_CHECK_CLOSE(stats1.packetLossRate, 0.0, 0.001); |
| 79 | BOOST_CHECK_CLOSE(stats1.sumRtt, 50.0, 0.001); |
| 80 | BOOST_CHECK_CLOSE(stats1.avgRtt, 50.0, 0.001); |
| 81 | BOOST_CHECK_CLOSE(stats1.stdDevRtt, 0.0, 0.001); |
| 82 | |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 83 | sc.recordData(time::milliseconds(50)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 84 | |
| 85 | Statistics stats2 = sc.computeStatistics(); |
| 86 | BOOST_CHECK_EQUAL(stats2.prefix, pingOptions.prefix); |
| 87 | BOOST_CHECK_EQUAL(stats2.nSent, 2); |
| 88 | BOOST_CHECK_EQUAL(stats2.nReceived, 2); |
| 89 | BOOST_CHECK_CLOSE(stats2.minRtt, 50.0, 0.001); |
| 90 | BOOST_CHECK_CLOSE(stats2.maxRtt, 50.0, 0.001); |
| 91 | BOOST_CHECK_CLOSE(stats2.packetLossRate, 0.0, 0.001); |
| 92 | BOOST_CHECK_CLOSE(stats2.sumRtt, 100.0, 0.001); |
| 93 | BOOST_CHECK_CLOSE(stats2.avgRtt, 50.0, 0.001); |
| 94 | BOOST_CHECK_CLOSE(stats2.stdDevRtt, 0.0, 0.001); |
| 95 | } |
| 96 | |
| 97 | BOOST_AUTO_TEST_CASE(Resp50msResp100ms) |
| 98 | { |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 99 | sc.recordData(time::milliseconds(50)); |
| 100 | sc.recordData(time::milliseconds(100)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 101 | |
| 102 | Statistics stats = sc.computeStatistics(); |
| 103 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 104 | BOOST_CHECK_EQUAL(stats.nSent, 2); |
| 105 | BOOST_CHECK_EQUAL(stats.nReceived, 2); |
| 106 | BOOST_CHECK_CLOSE(stats.minRtt, 50.0, 0.001); |
| 107 | BOOST_CHECK_CLOSE(stats.maxRtt, 100.0, 0.001); |
| 108 | BOOST_CHECK_CLOSE(stats.packetLossRate, 0.0, 0.001); |
| 109 | BOOST_CHECK_CLOSE(stats.sumRtt, 150.0, 0.001); |
| 110 | BOOST_CHECK_CLOSE(stats.avgRtt, 75.0, 0.001); |
| 111 | BOOST_CHECK_CLOSE(stats.stdDevRtt, 25.0, 0.001); |
| 112 | } |
| 113 | |
| 114 | BOOST_AUTO_TEST_CASE(LossLoss) |
| 115 | { |
| 116 | sc.recordTimeout(); |
| 117 | sc.recordTimeout(); |
| 118 | |
| 119 | Statistics stats = sc.computeStatistics(); |
| 120 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 121 | BOOST_CHECK_EQUAL(stats.nSent, 2); |
| 122 | BOOST_CHECK_EQUAL(stats.nReceived, 0); |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame^] | 123 | BOOST_CHECK_EQUAL(stats.nNacked, 0); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 124 | BOOST_CHECK_CLOSE(stats.packetLossRate, 1.0, 0.001); |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame^] | 125 | BOOST_CHECK_CLOSE(stats.packetNackedRate, 0.0, 0.001); |
| 126 | BOOST_CHECK_CLOSE(stats.minRtt, std::numeric_limits<double>::max(), 0.001); |
| 127 | BOOST_CHECK_CLOSE(stats.maxRtt, 0.0, 0.001); |
| 128 | BOOST_CHECK_CLOSE(stats.sumRtt, 0.0, 0.001); |
| 129 | BOOST_CHECK(std::isnan(stats.avgRtt)); |
| 130 | BOOST_CHECK(std::isnan(stats.stdDevRtt)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_CASE(Resp50msLoss) |
| 134 | { |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 135 | sc.recordData(time::milliseconds(50)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 136 | sc.recordTimeout(); |
| 137 | |
| 138 | Statistics stats = sc.computeStatistics(); |
| 139 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 140 | BOOST_CHECK_EQUAL(stats.nSent, 2); |
| 141 | BOOST_CHECK_EQUAL(stats.nReceived, 1); |
| 142 | BOOST_CHECK_CLOSE(stats.minRtt, 50.0, 0.001); |
| 143 | BOOST_CHECK_CLOSE(stats.maxRtt, 50.0, 0.001); |
| 144 | BOOST_CHECK_CLOSE(stats.packetLossRate, 0.5, 0.001); |
| 145 | BOOST_CHECK_CLOSE(stats.sumRtt, 50.0, 0.001); |
| 146 | BOOST_CHECK_CLOSE(stats.avgRtt, 50.0, 0.001); |
| 147 | BOOST_CHECK_CLOSE(stats.stdDevRtt, 0.0, 0.001); |
| 148 | } |
| 149 | |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 150 | BOOST_AUTO_TEST_CASE(NackNack) |
| 151 | { |
| 152 | sc.recordNack(); |
| 153 | sc.recordNack(); |
| 154 | |
| 155 | Statistics stats = sc.computeStatistics(); |
| 156 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 157 | BOOST_CHECK_EQUAL(stats.nSent, 2); |
| 158 | BOOST_CHECK_EQUAL(stats.nNacked, 2); |
| 159 | BOOST_CHECK_EQUAL(stats.nReceived, 0); |
| 160 | BOOST_CHECK_CLOSE(stats.packetNackedRate, 1.0, 0.001); |
| 161 | } |
| 162 | |
| 163 | BOOST_AUTO_TEST_CASE(Resp50msNack) |
| 164 | { |
| 165 | sc.recordData(time::milliseconds(50)); |
| 166 | sc.recordNack(); |
| 167 | |
| 168 | Statistics stats = sc.computeStatistics(); |
| 169 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 170 | BOOST_CHECK_EQUAL(stats.nSent, 2); |
| 171 | BOOST_CHECK_EQUAL(stats.nReceived, 1); |
| 172 | BOOST_CHECK_CLOSE(stats.minRtt, 50.0, 0.001); |
| 173 | BOOST_CHECK_CLOSE(stats.maxRtt, 50.0, 0.001); |
| 174 | BOOST_CHECK_CLOSE(stats.sumRtt, 50.0, 0.001); |
| 175 | BOOST_CHECK_CLOSE(stats.avgRtt, 50.0, 0.001); |
| 176 | BOOST_CHECK_CLOSE(stats.stdDevRtt, 0.0, 0.001); |
| 177 | BOOST_CHECK_EQUAL(stats.nNacked, 1); |
| 178 | BOOST_CHECK_CLOSE(stats.packetNackedRate, 0.5, 0.001); |
| 179 | } |
| 180 | |
| 181 | BOOST_AUTO_TEST_CASE(NackLoss) |
| 182 | { |
| 183 | sc.recordNack(); |
| 184 | sc.recordTimeout(); |
| 185 | |
| 186 | Statistics stats = sc.computeStatistics(); |
| 187 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 188 | BOOST_CHECK_EQUAL(stats.nSent, 2); |
| 189 | BOOST_CHECK_EQUAL(stats.nReceived, 0); |
| 190 | BOOST_CHECK_EQUAL(stats.nNacked, 1); |
| 191 | BOOST_CHECK_CLOSE(stats.packetNackedRate, 0.5, 0.001); |
| 192 | BOOST_CHECK_CLOSE(stats.packetLossRate, 0.5, 0.001); |
| 193 | } |
| 194 | |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame^] | 195 | BOOST_AUTO_TEST_CASE(NoneSent) |
| 196 | { |
| 197 | Statistics stats = sc.computeStatistics(); |
| 198 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 199 | BOOST_CHECK_EQUAL(stats.nSent, 0); |
| 200 | BOOST_CHECK_EQUAL(stats.nReceived, 0); |
| 201 | BOOST_CHECK_EQUAL(stats.nNacked, 0); |
| 202 | BOOST_CHECK(std::isnan(stats.packetLossRate)); |
| 203 | BOOST_CHECK(std::isnan(stats.packetNackedRate)); |
| 204 | BOOST_CHECK_CLOSE(stats.minRtt, std::numeric_limits<double>::max(), 0.001); |
| 205 | BOOST_CHECK_CLOSE(stats.maxRtt, 0.0, 0.001); |
| 206 | BOOST_CHECK_CLOSE(stats.sumRtt, 0.0, 0.001); |
| 207 | BOOST_CHECK(std::isnan(stats.avgRtt)); |
| 208 | BOOST_CHECK(std::isnan(stats.stdDevRtt)); |
| 209 | } |
| 210 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 211 | BOOST_AUTO_TEST_SUITE_END() // TestStatisticsCollector |
| 212 | BOOST_AUTO_TEST_SUITE_END() // Ping |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 213 | |
| 214 | } // namespace tests |
| 215 | } // namespace client |
| 216 | } // namespace ping |
| 217 | } // namespace ndn |