Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, The University of Memphis, |
| 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "statistics.hpp" |
| 23 | #include "test-common.hpp" |
| 24 | #include "hello-protocol.hpp" |
| 25 | #include "lsdb.hpp" |
| 26 | #include "nlsr.hpp" |
| 27 | |
| 28 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 29 | |
| 30 | namespace nlsr { |
| 31 | namespace test { |
| 32 | |
| 33 | class StatisticsFixture : public BaseFixture |
| 34 | { |
| 35 | public: |
| 36 | StatisticsFixture() |
| 37 | : face(std::make_shared<ndn::util::DummyClientFace>(g_ioService)) |
| 38 | , nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain) |
| 39 | , lsdb(nlsr.getLsdb()) |
| 40 | , hello(nlsr.m_helloProtocol) |
| 41 | , conf(nlsr.getConfParameter()) |
| 42 | , collector(nlsr.getStatsCollector()) |
| 43 | { |
| 44 | conf.setNetwork("/ndn"); |
| 45 | conf.setSiteName("/site"); |
| 46 | conf.setRouterName("/%C1.router/this-router"); |
| 47 | conf.buildRouterPrefix(); |
| 48 | |
| 49 | nlsr.initialize(); |
| 50 | |
| 51 | face->processEvents(ndn::time::milliseconds(1)); |
| 52 | face->sentInterests.clear(); |
| 53 | } |
| 54 | |
| 55 | /*! |
| 56 | * \brief Checks if lsa interest was received and data for interest was sent |
| 57 | * |
| 58 | * \param interestPrefix is an interest name prefix |
| 59 | * \param lsaType indicates whether the lsa is a name, adjacency, or coordinate |
| 60 | * \param seqNo sequence number that will be appended to an interest name |
| 61 | * \param receivedInterestType is the specific Statisitcs::PacketType interest that is received |
| 62 | * \param sentDataType is the Statistics::PacketType data being sent upon interest process |
| 63 | * |
| 64 | * This is a general function that can be used for all three types of lsa. Calling processInterest() |
| 65 | * from lsdb will cause the statsCollector to increment the incoming interest type and increment the |
| 66 | * outgoing data type. |
| 67 | */ |
| 68 | void |
| 69 | receiveInterestAndCheckSentStats(const std::string& interestPrefix, |
| 70 | const std::string& lsaType, |
| 71 | uint32_t seqNo, |
| 72 | Statistics::PacketType receivedInterestType, |
| 73 | Statistics::PacketType sentDataType) |
| 74 | { |
| 75 | size_t rcvBefore = collector.getStatistics().get(receivedInterestType); |
| 76 | size_t sentBefore = collector.getStatistics().get(sentDataType); |
| 77 | |
| 78 | ndn::Name interestName = ndn::Name(ndn::Name(interestPrefix + lsaType).appendNumber(seqNo)); |
| 79 | lsdb.processInterest(ndn::Name(), ndn::Interest(interestName)); |
| 80 | face->processEvents(ndn::time::milliseconds(1)); |
| 81 | |
| 82 | BOOST_CHECK_EQUAL(collector.getStatistics().get(receivedInterestType), rcvBefore + 1); |
| 83 | BOOST_CHECK_EQUAL(collector.getStatistics().get(sentDataType), sentBefore + 1); |
| 84 | } |
| 85 | |
| 86 | /*! |
| 87 | * \brief Checks if statistics update after an lsa interest is sent |
| 88 | * |
| 89 | * \param prefix is an interest prefix |
| 90 | * \param lsaType indicates whether the lsa is a name, adjacency, or coordinate |
| 91 | * \param seqNo is the sequence number |
| 92 | * \param statsType is a statistical PacketType |
| 93 | * |
| 94 | * The function is called to initiate an expressInterest call in lsdb and to check if the |
| 95 | * expected statistical packetType was incremented. |
| 96 | */ |
| 97 | void |
| 98 | sendInterestAndCheckStats(const std::string& prefix, |
| 99 | const std::string& lsaType, |
| 100 | uint32_t seqNo, |
| 101 | Statistics::PacketType statsType) |
| 102 | { |
| 103 | size_t sentBefore = collector.getStatistics().get(statsType); |
| 104 | |
| 105 | lsdb.expressInterest(ndn::Name(prefix + lsaType).appendNumber(seqNo), 0, |
| 106 | ndn::time::steady_clock::TimePoint::min()); |
| 107 | face->processEvents(ndn::time::milliseconds(1)); |
| 108 | |
| 109 | BOOST_CHECK_EQUAL(collector.getStatistics().get(statsType), sentBefore + 1); |
| 110 | } |
| 111 | |
| 112 | public: |
| 113 | std::shared_ptr<ndn::util::DummyClientFace> face; |
| 114 | Nlsr nlsr; |
| 115 | |
| 116 | Lsdb& lsdb; |
| 117 | HelloProtocol& hello; |
| 118 | ConfParameter& conf; |
| 119 | StatsCollector& collector; |
| 120 | }; |
| 121 | |
| 122 | BOOST_FIXTURE_TEST_SUITE(TestStatistics, StatisticsFixture) |
| 123 | |
| 124 | |
| 125 | // A statistical PacketType is directly incremented (without signals). |
| 126 | BOOST_AUTO_TEST_CASE(StatsIncrement) |
| 127 | { |
| 128 | Statistics stats; |
| 129 | BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 0); |
| 130 | stats.increment(Statistics::PacketType::SENT_HELLO_INTEREST); |
| 131 | BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 1); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * After a PacketType has been incremented, the resetAll() function is called, which sets all |
| 136 | * statistical packetType counts to 0 |
| 137 | */ |
| 138 | BOOST_AUTO_TEST_CASE(StatsReset) |
| 139 | { |
| 140 | Statistics stats; |
| 141 | stats.increment(Statistics::PacketType::SENT_HELLO_INTEREST); |
| 142 | stats.resetAll(); |
| 143 | BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 0); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /* |
| 148 | * This tests hello interests and hello data statistical collection by constructing an adjacency lsa |
| 149 | * and calling functions that trigger the sending and receiving hello of interests/data. |
| 150 | */ |
| 151 | BOOST_AUTO_TEST_CASE(SendHelloInterest) |
| 152 | { |
| 153 | nlsr.initialize(); |
| 154 | |
| 155 | face->processEvents(ndn::time::milliseconds(1)); |
| 156 | face->sentInterests.clear(); |
| 157 | |
| 158 | Adjacent other("/ndn/router/other", ndn::util::FaceUri("udp4://other"), 25, Adjacent::STATUS_INACTIVE, 0, 0); |
| 159 | |
| 160 | // This router's Adjacency LSA |
| 161 | nlsr.getAdjacencyList().insert(other); |
| 162 | |
| 163 | ndn::Name name(conf.getRouterPrefix()); |
| 164 | name.append("NLSR"); |
| 165 | name.append("INFO"); |
| 166 | name.append(other.getName().wireEncode()); |
| 167 | |
| 168 | hello.expressInterest(name, 1); |
| 169 | face->processEvents(ndn::time::milliseconds(1)); |
| 170 | |
| 171 | BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_HELLO_INTEREST), 1); |
| 172 | |
| 173 | ndn::Interest interest(name); |
| 174 | hello.processInterest(ndn::Name(), interest); |
| 175 | |
| 176 | face->processEvents(ndn::time::milliseconds(1)); |
| 177 | |
| 178 | BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_HELLO_INTEREST), 1); |
| 179 | BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_HELLO_DATA), 1); |
| 180 | |
| 181 | // Receive Hello Data |
| 182 | ndn::Name dataName = other.getName(); |
| 183 | dataName.append("NLSR"); |
| 184 | dataName.append("INFO"); |
| 185 | dataName.append(conf.getRouterPrefix().wireEncode()); |
| 186 | |
| 187 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(dataName); |
| 188 | hello.onContentValidated(data); |
| 189 | |
| 190 | BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_HELLO_DATA), 1); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * An interest is sent for each lsa type (name, adjacency, coordinate). The respective statistics are |
| 195 | * totaled and checked. |
| 196 | */ |
| 197 | BOOST_AUTO_TEST_CASE(LsdbSendLsaInterest) |
| 198 | { |
| 199 | const std::string interestPrefix("/ndn/NLSR/LSA/site/%C1.Router/router/"); |
| 200 | uint32_t seqNo = 1; |
| 201 | |
| 202 | // Adjacency LSA |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 203 | sendInterestAndCheckStats(interestPrefix, std::to_string(Lsa::Type::ADJACENCY), seqNo, |
| 204 | Statistics::PacketType::SENT_ADJ_LSA_INTEREST); |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 205 | |
| 206 | // Coordinate LSA |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 207 | sendInterestAndCheckStats(interestPrefix, std::to_string(Lsa::Type::COORDINATE), seqNo, |
| 208 | Statistics::PacketType::SENT_COORD_LSA_INTEREST); |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 209 | |
| 210 | // Name LSA |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 211 | sendInterestAndCheckStats(interestPrefix, std::to_string(Lsa::Type::NAME), seqNo, |
| 212 | Statistics::PacketType::SENT_NAME_LSA_INTEREST); |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 213 | |
| 214 | // 3 total lsa interests were sent |
| 215 | BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_INTEREST), 3); |
| 216 | } |
| 217 | |
| 218 | /* |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 219 | * Tests the statistics collected upon processing incoming lsa |
| 220 | * interests and respective outgoing data. This process will trigger |
| 221 | * both an increment for received lsa interest and sent lsa data. |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 222 | * |
| 223 | * /sa receiveInterestAndCheckSentStats |
| 224 | */ |
| 225 | BOOST_AUTO_TEST_CASE(LsdbReceiveInterestSendData) |
| 226 | { |
| 227 | std::string routerName("/ndn/site/%C1.Router/router"); |
| 228 | ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max(); |
| 229 | uint32_t seqNo = 1; |
| 230 | |
| 231 | // Adjacency LSA |
| 232 | Adjacent adjacency("adjacency"); |
| 233 | adjacency.setStatus(Adjacent::STATUS_ACTIVE); |
| 234 | |
| 235 | AdjacencyList adjacencies; |
| 236 | adjacencies.insert(adjacency); |
| 237 | |
| 238 | AdjLsa adjLsa(routerName, seqNo, MAX_TIME, 1, adjacencies); |
| 239 | lsdb.installAdjLsa(adjLsa); |
| 240 | |
| 241 | const std::string interestPrefix("/ndn/NLSR/LSA/site/%C1.Router/router/"); |
| 242 | |
| 243 | // Receive Adjacency LSA Interest |
| 244 | receiveInterestAndCheckSentStats(interestPrefix, |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 245 | std::to_string(Lsa::Type::ADJACENCY), |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 246 | seqNo, |
| 247 | Statistics::PacketType::RCV_ADJ_LSA_INTEREST, |
| 248 | Statistics::PacketType::SENT_ADJ_LSA_DATA); |
| 249 | |
| 250 | // Name LSA |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 251 | NamePrefixList prefixes{ndn::Name{"/ndn/name"}}; |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 252 | |
| 253 | NameLsa nameLsa(routerName, seqNo, MAX_TIME, prefixes); |
| 254 | lsdb.installNameLsa(nameLsa); |
| 255 | |
| 256 | // Receive Name LSA Interest |
| 257 | receiveInterestAndCheckSentStats(interestPrefix, |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 258 | std::to_string(Lsa::Type::NAME), |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 259 | seqNo, |
| 260 | Statistics::PacketType::RCV_NAME_LSA_INTEREST, |
| 261 | Statistics::PacketType::SENT_NAME_LSA_DATA); |
| 262 | |
| 263 | // Coordinate LSA |
| 264 | std::vector<double> angles = {20.0, 30.0}; |
| 265 | CoordinateLsa coordLsa(routerName, seqNo, MAX_TIME, 2.5, angles); |
| 266 | lsdb.installCoordinateLsa(coordLsa); |
| 267 | |
| 268 | // Receive Adjacency LSA Interest |
| 269 | receiveInterestAndCheckSentStats(interestPrefix, |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 270 | std::to_string(Lsa::Type::COORDINATE), |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 271 | seqNo, |
| 272 | Statistics::PacketType::RCV_COORD_LSA_INTEREST, |
| 273 | Statistics::PacketType::SENT_COORD_LSA_DATA); |
| 274 | |
| 275 | // 3 different lsa type interests should be received |
| 276 | BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_INTEREST), 3); |
| 277 | |
| 278 | // data should have been sent 3x, once per lsa type |
| 279 | BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_DATA), 3); |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Data for each lsa type (name, adjacency, coordinate) is sent to the lsdb and statistics are |
| 284 | * checked to verify the respective statistical PacketType has been received. |
| 285 | */ |
| 286 | BOOST_AUTO_TEST_CASE(LsdbReceiveData) |
| 287 | { |
| 288 | ndn::Name routerName("/ndn/cs/%C1.Router/router1"); |
| 289 | uint32_t seqNo = 1; |
| 290 | ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max(); |
| 291 | |
| 292 | // adjacency lsa |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 293 | ndn::Name adjInterest("/ndn/NLSR/LSA/cs/%C1.Router/router1/ADJACENCY/"); |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 294 | adjInterest.appendNumber(seqNo); |
| 295 | AdjLsa aLsa(routerName, seqNo, MAX_TIME, 1, nlsr.getAdjacencyList()); |
| 296 | lsdb.installAdjLsa(aLsa); |
| 297 | |
Nick Gordon | faf49f4 | 2017-10-23 12:36:28 -0500 | [diff] [blame] | 298 | const ndn::ConstBufferPtr aBuffer = std::make_shared<ndn::Buffer>(aLsa.serialize().c_str(), |
| 299 | aLsa.serialize().size()); |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 300 | lsdb.afterFetchLsa(aBuffer, adjInterest); |
| 301 | BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_ADJ_LSA_DATA), 1); |
| 302 | |
| 303 | // coordinate lsa |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 304 | ndn::Name coordInterest("/ndn/NLSR/LSA/cs/%C1.Router/router1/COORDINATE/"); |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 305 | coordInterest.appendNumber(seqNo); |
| 306 | std::vector<double> angles = {20.0, 30.0}; |
| 307 | CoordinateLsa cLsa(routerName, seqNo, MAX_TIME, 2.5, angles); |
| 308 | lsdb.installCoordinateLsa(cLsa); |
| 309 | |
Nick Gordon | faf49f4 | 2017-10-23 12:36:28 -0500 | [diff] [blame] | 310 | const ndn::ConstBufferPtr cBuffer = std::make_shared<ndn::Buffer>(cLsa.serialize().c_str(), |
| 311 | cLsa.serialize().size()); |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 312 | lsdb.afterFetchLsa(cBuffer, coordInterest); |
| 313 | BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_COORD_LSA_DATA), 1); |
| 314 | |
| 315 | // name lsa |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 316 | ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/NAME/"); |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 317 | interestName.appendNumber(seqNo); |
| 318 | NameLsa nLsa(routerName, seqNo, MAX_TIME, nlsr.getNamePrefixList()); |
| 319 | lsdb.installNameLsa(nLsa); |
| 320 | |
Nick Gordon | faf49f4 | 2017-10-23 12:36:28 -0500 | [diff] [blame] | 321 | const ndn::ConstBufferPtr nBuffer = std::make_shared<ndn::Buffer>(nLsa.serialize().c_str(), |
| 322 | nLsa.serialize().size()); |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 323 | lsdb.afterFetchLsa(nBuffer, interestName); |
| 324 | BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_NAME_LSA_DATA), 1); |
| 325 | |
| 326 | // 3 lsa data types should be received |
| 327 | BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_DATA), 3); |
| 328 | } |
| 329 | |
| 330 | BOOST_AUTO_TEST_SUITE_END() |
| 331 | |
| 332 | } // namespace test |
| 333 | } // namespace nlsr |