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" |
| 21 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 22 | |
| 23 | #include "tests/test-common.hpp" |
| 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 | |
| 65 | BOOST_FIXTURE_TEST_SUITE(PingClientStatisticsCollector, StatisticsCollectorFixture) |
| 66 | |
| 67 | BOOST_AUTO_TEST_CASE(Resp50msResp50ms) |
| 68 | { |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 69 | sc.recordData(time::milliseconds(50)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 70 | |
| 71 | Statistics stats1 = sc.computeStatistics(); |
| 72 | BOOST_CHECK_EQUAL(stats1.prefix, pingOptions.prefix); |
| 73 | BOOST_CHECK_EQUAL(stats1.nSent, 1); |
| 74 | BOOST_CHECK_EQUAL(stats1.nReceived, 1); |
| 75 | BOOST_CHECK_CLOSE(stats1.minRtt, 50.0, 0.001); |
| 76 | BOOST_CHECK_CLOSE(stats1.maxRtt, 50.0, 0.001); |
| 77 | BOOST_CHECK_CLOSE(stats1.packetLossRate, 0.0, 0.001); |
| 78 | BOOST_CHECK_CLOSE(stats1.sumRtt, 50.0, 0.001); |
| 79 | BOOST_CHECK_CLOSE(stats1.avgRtt, 50.0, 0.001); |
| 80 | BOOST_CHECK_CLOSE(stats1.stdDevRtt, 0.0, 0.001); |
| 81 | |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 82 | sc.recordData(time::milliseconds(50)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 83 | |
| 84 | Statistics stats2 = sc.computeStatistics(); |
| 85 | BOOST_CHECK_EQUAL(stats2.prefix, pingOptions.prefix); |
| 86 | BOOST_CHECK_EQUAL(stats2.nSent, 2); |
| 87 | BOOST_CHECK_EQUAL(stats2.nReceived, 2); |
| 88 | BOOST_CHECK_CLOSE(stats2.minRtt, 50.0, 0.001); |
| 89 | BOOST_CHECK_CLOSE(stats2.maxRtt, 50.0, 0.001); |
| 90 | BOOST_CHECK_CLOSE(stats2.packetLossRate, 0.0, 0.001); |
| 91 | BOOST_CHECK_CLOSE(stats2.sumRtt, 100.0, 0.001); |
| 92 | BOOST_CHECK_CLOSE(stats2.avgRtt, 50.0, 0.001); |
| 93 | BOOST_CHECK_CLOSE(stats2.stdDevRtt, 0.0, 0.001); |
| 94 | } |
| 95 | |
| 96 | BOOST_AUTO_TEST_CASE(Resp50msResp100ms) |
| 97 | { |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 98 | sc.recordData(time::milliseconds(50)); |
| 99 | sc.recordData(time::milliseconds(100)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 100 | |
| 101 | Statistics stats = sc.computeStatistics(); |
| 102 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 103 | BOOST_CHECK_EQUAL(stats.nSent, 2); |
| 104 | BOOST_CHECK_EQUAL(stats.nReceived, 2); |
| 105 | BOOST_CHECK_CLOSE(stats.minRtt, 50.0, 0.001); |
| 106 | BOOST_CHECK_CLOSE(stats.maxRtt, 100.0, 0.001); |
| 107 | BOOST_CHECK_CLOSE(stats.packetLossRate, 0.0, 0.001); |
| 108 | BOOST_CHECK_CLOSE(stats.sumRtt, 150.0, 0.001); |
| 109 | BOOST_CHECK_CLOSE(stats.avgRtt, 75.0, 0.001); |
| 110 | BOOST_CHECK_CLOSE(stats.stdDevRtt, 25.0, 0.001); |
| 111 | } |
| 112 | |
| 113 | BOOST_AUTO_TEST_CASE(LossLoss) |
| 114 | { |
| 115 | sc.recordTimeout(); |
| 116 | sc.recordTimeout(); |
| 117 | |
| 118 | Statistics stats = sc.computeStatistics(); |
| 119 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 120 | BOOST_CHECK_EQUAL(stats.nSent, 2); |
| 121 | BOOST_CHECK_EQUAL(stats.nReceived, 0); |
| 122 | BOOST_CHECK_CLOSE(stats.packetLossRate, 1.0, 0.001); |
| 123 | } |
| 124 | |
| 125 | BOOST_AUTO_TEST_CASE(Resp50msLoss) |
| 126 | { |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 127 | sc.recordData(time::milliseconds(50)); |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 128 | sc.recordTimeout(); |
| 129 | |
| 130 | Statistics stats = sc.computeStatistics(); |
| 131 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 132 | BOOST_CHECK_EQUAL(stats.nSent, 2); |
| 133 | BOOST_CHECK_EQUAL(stats.nReceived, 1); |
| 134 | BOOST_CHECK_CLOSE(stats.minRtt, 50.0, 0.001); |
| 135 | BOOST_CHECK_CLOSE(stats.maxRtt, 50.0, 0.001); |
| 136 | BOOST_CHECK_CLOSE(stats.packetLossRate, 0.5, 0.001); |
| 137 | BOOST_CHECK_CLOSE(stats.sumRtt, 50.0, 0.001); |
| 138 | BOOST_CHECK_CLOSE(stats.avgRtt, 50.0, 0.001); |
| 139 | BOOST_CHECK_CLOSE(stats.stdDevRtt, 0.0, 0.001); |
| 140 | } |
| 141 | |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 142 | BOOST_AUTO_TEST_CASE(NackNack) |
| 143 | { |
| 144 | sc.recordNack(); |
| 145 | sc.recordNack(); |
| 146 | |
| 147 | Statistics stats = sc.computeStatistics(); |
| 148 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 149 | BOOST_CHECK_EQUAL(stats.nSent, 2); |
| 150 | BOOST_CHECK_EQUAL(stats.nNacked, 2); |
| 151 | BOOST_CHECK_EQUAL(stats.nReceived, 0); |
| 152 | BOOST_CHECK_CLOSE(stats.packetNackedRate, 1.0, 0.001); |
| 153 | } |
| 154 | |
| 155 | BOOST_AUTO_TEST_CASE(Resp50msNack) |
| 156 | { |
| 157 | sc.recordData(time::milliseconds(50)); |
| 158 | sc.recordNack(); |
| 159 | |
| 160 | Statistics stats = sc.computeStatistics(); |
| 161 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 162 | BOOST_CHECK_EQUAL(stats.nSent, 2); |
| 163 | BOOST_CHECK_EQUAL(stats.nReceived, 1); |
| 164 | BOOST_CHECK_CLOSE(stats.minRtt, 50.0, 0.001); |
| 165 | BOOST_CHECK_CLOSE(stats.maxRtt, 50.0, 0.001); |
| 166 | BOOST_CHECK_CLOSE(stats.sumRtt, 50.0, 0.001); |
| 167 | BOOST_CHECK_CLOSE(stats.avgRtt, 50.0, 0.001); |
| 168 | BOOST_CHECK_CLOSE(stats.stdDevRtt, 0.0, 0.001); |
| 169 | BOOST_CHECK_EQUAL(stats.nNacked, 1); |
| 170 | BOOST_CHECK_CLOSE(stats.packetNackedRate, 0.5, 0.001); |
| 171 | } |
| 172 | |
| 173 | BOOST_AUTO_TEST_CASE(NackLoss) |
| 174 | { |
| 175 | sc.recordNack(); |
| 176 | sc.recordTimeout(); |
| 177 | |
| 178 | Statistics stats = sc.computeStatistics(); |
| 179 | BOOST_CHECK_EQUAL(stats.prefix, pingOptions.prefix); |
| 180 | BOOST_CHECK_EQUAL(stats.nSent, 2); |
| 181 | BOOST_CHECK_EQUAL(stats.nReceived, 0); |
| 182 | BOOST_CHECK_EQUAL(stats.nNacked, 1); |
| 183 | BOOST_CHECK_CLOSE(stats.packetNackedRate, 0.5, 0.001); |
| 184 | BOOST_CHECK_CLOSE(stats.packetLossRate, 0.5, 0.001); |
| 185 | } |
| 186 | |
Junxiao Shi | 2713a3b | 2015-06-22 16:19:05 -0700 | [diff] [blame] | 187 | BOOST_AUTO_TEST_SUITE_END() |
| 188 | |
| 189 | } // namespace tests |
| 190 | } // namespace client |
| 191 | } // namespace ping |
| 192 | } // namespace ndn |