Davide Pesavento | 912d2e8 | 2019-01-10 17:04:31 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 9266906 | 2022-03-10 19:09:44 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Arizona Board of Regents. |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 4 | * |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 17 | * |
| 18 | * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 19 | */ |
| 20 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 21 | #include "util.hpp" |
| 22 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 23 | #include <ndn-cxx/data.hpp> |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 24 | #include <ndn-cxx/face.hpp> |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 25 | #include <ndn-cxx/interest.hpp> |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 26 | #include <ndn-cxx/lp/tags.hpp> |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 27 | #include <ndn-cxx/util/random.hpp> |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 28 | #include <ndn-cxx/util/time.hpp> |
Davide Pesavento | 912d2e8 | 2019-01-10 17:04:31 -0500 | [diff] [blame] | 29 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 30 | #include <limits> |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 31 | #include <optional> |
Davide Pesavento | 56f79d6 | 2019-01-26 17:30:32 -0500 | [diff] [blame] | 32 | #include <sstream> |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 33 | #include <string_view> |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 34 | #include <vector> |
| 35 | |
Davide Pesavento | 912d2e8 | 2019-01-10 17:04:31 -0500 | [diff] [blame] | 36 | #include <boost/asio/deadline_timer.hpp> |
| 37 | #include <boost/asio/io_service.hpp> |
| 38 | #include <boost/asio/signal_set.hpp> |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame] | 39 | #include <boost/date_time/posix_time/posix_time.hpp> |
Davide Pesavento | 912d2e8 | 2019-01-10 17:04:31 -0500 | [diff] [blame] | 40 | #include <boost/lexical_cast.hpp> |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame] | 41 | #include <boost/noncopyable.hpp> |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 42 | #include <boost/program_options/options_description.hpp> |
| 43 | #include <boost/program_options/parsers.hpp> |
| 44 | #include <boost/program_options/variables_map.hpp> |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame] | 45 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 46 | namespace po = boost::program_options; |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 47 | using namespace ndn::time_literals; |
| 48 | using namespace std::string_literals; |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame] | 49 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 50 | namespace ndntg { |
| 51 | |
| 52 | namespace time = ndn::time; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 53 | |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame] | 54 | class NdnTrafficClient : boost::noncopyable |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 55 | { |
| 56 | public: |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 57 | explicit |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 58 | NdnTrafficClient(const std::string& configFile) |
| 59 | : m_signalSet(m_ioService, SIGINT, SIGTERM) |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 60 | , m_logger("NdnTrafficClient") |
Alexander Afanasyev | 740812e | 2014-10-30 15:37:45 -0700 | [diff] [blame] | 61 | , m_face(m_ioService) |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 62 | , m_configurationFile(configFile) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 63 | { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 66 | void |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 67 | setMaximumInterests(uint64_t maxInterests) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 68 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 69 | m_nMaximumInterests = maxInterests; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 73 | setInterestInterval(time::milliseconds interval) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 74 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 75 | BOOST_ASSERT(interval > 0_ms); |
| 76 | m_interestInterval = interval; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame] | 80 | setQuietLogging() |
| 81 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 82 | m_wantQuiet = true; |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 85 | int |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 86 | run() |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 87 | { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 88 | m_logger.initializeLog(std::to_string(ndn::random::generateWord32())); |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 89 | |
| 90 | if (!readConfigurationFile(m_configurationFile, m_trafficPatterns, m_logger)) { |
| 91 | return 2; |
| 92 | } |
| 93 | |
| 94 | if (!checkTrafficPatternCorrectness()) { |
| 95 | m_logger.log("ERROR: Traffic configuration provided is not proper", false, true); |
| 96 | return 2; |
| 97 | } |
| 98 | |
| 99 | m_logger.log("Traffic configuration file processing completed.\n", true, false); |
| 100 | for (std::size_t i = 0; i < m_trafficPatterns.size(); i++) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 101 | m_logger.log("Traffic Pattern Type #" + std::to_string(i + 1), false, false); |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 102 | m_trafficPatterns[i].printTrafficConfiguration(m_logger); |
| 103 | m_logger.log("", false, false); |
| 104 | } |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 105 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 106 | if (m_nMaximumInterests == 0) { |
| 107 | logStatistics(); |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 108 | return 0; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 109 | } |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 110 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 111 | m_signalSet.async_wait([this] (auto&&...) { stop(); }); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 112 | |
| 113 | boost::asio::deadline_timer timer(m_ioService, |
| 114 | boost::posix_time::millisec(m_interestInterval.count())); |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 115 | timer.async_wait([this, &timer] (auto&&...) { generateTraffic(timer); }); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 116 | |
| 117 | try { |
| 118 | m_face.processEvents(); |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 119 | return m_hasError ? 1 : 0; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 120 | } |
| 121 | catch (const std::exception& e) { |
| 122 | m_logger.log("ERROR: "s + e.what(), true, true); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 123 | m_ioService.stop(); |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 124 | return 1; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 125 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 128 | private: |
| 129 | class InterestTrafficConfiguration |
| 130 | { |
| 131 | public: |
| 132 | void |
| 133 | printTrafficConfiguration(Logger& logger) const |
| 134 | { |
Davide Pesavento | 56f79d6 | 2019-01-26 17:30:32 -0500 | [diff] [blame] | 135 | std::ostringstream os; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 136 | |
Davide Pesavento | 56f79d6 | 2019-01-26 17:30:32 -0500 | [diff] [blame] | 137 | os << "TrafficPercentage=" << m_trafficPercentage << ", "; |
| 138 | os << "Name=" << m_name << ", "; |
| 139 | if (m_nameAppendBytes) { |
| 140 | os << "NameAppendBytes=" << *m_nameAppendBytes << ", "; |
| 141 | } |
| 142 | if (m_nameAppendSeqNum) { |
| 143 | os << "NameAppendSequenceNumber=" << *m_nameAppendSeqNum << ", "; |
| 144 | } |
| 145 | if (m_canBePrefix) { |
| 146 | os << "CanBePrefix=" << m_canBePrefix << ", "; |
| 147 | } |
| 148 | if (m_mustBeFresh) { |
| 149 | os << "MustBeFresh=" << m_mustBeFresh << ", "; |
| 150 | } |
| 151 | if (m_nonceDuplicationPercentage > 0) { |
| 152 | os << "NonceDuplicationPercentage=" << m_nonceDuplicationPercentage << ", "; |
| 153 | } |
| 154 | if (m_interestLifetime >= 0_ms) { |
| 155 | os << "InterestLifetime=" << m_interestLifetime.count() << ", "; |
| 156 | } |
| 157 | if (m_nextHopFaceId > 0) { |
| 158 | os << "NextHopFaceId=" << m_nextHopFaceId << ", "; |
| 159 | } |
| 160 | if (m_expectedContent) { |
| 161 | os << "ExpectedContent=" << *m_expectedContent << ", "; |
| 162 | } |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 163 | |
Davide Pesavento | 56f79d6 | 2019-01-26 17:30:32 -0500 | [diff] [blame] | 164 | auto str = os.str(); |
| 165 | str = str.substr(0, str.length() - 2); // remove suffix ", " |
| 166 | logger.log(str, false, false); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | bool |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 170 | parseConfigurationLine(const std::string& line, Logger& logger, int lineNumber) |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 171 | { |
| 172 | std::string parameter, value; |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 173 | if (!extractParameterAndValue(line, parameter, value)) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 174 | logger.log("Line " + std::to_string(lineNumber) + " - Invalid syntax: " + line, |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 175 | false, true); |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | if (parameter == "TrafficPercentage") { |
| 180 | m_trafficPercentage = std::stoul(value); |
| 181 | } |
| 182 | else if (parameter == "Name") { |
| 183 | m_name = value; |
| 184 | } |
| 185 | else if (parameter == "NameAppendBytes") { |
| 186 | m_nameAppendBytes = std::stoul(value); |
| 187 | } |
| 188 | else if (parameter == "NameAppendSequenceNumber") { |
| 189 | m_nameAppendSeqNum = std::stoull(value); |
| 190 | } |
Davide Pesavento | 56f79d6 | 2019-01-26 17:30:32 -0500 | [diff] [blame] | 191 | else if (parameter == "CanBePrefix") { |
| 192 | m_canBePrefix = parseBoolean(value); |
| 193 | } |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 194 | else if (parameter == "MustBeFresh") { |
| 195 | m_mustBeFresh = parseBoolean(value); |
| 196 | } |
| 197 | else if (parameter == "NonceDuplicationPercentage") { |
| 198 | m_nonceDuplicationPercentage = std::stoul(value); |
| 199 | } |
| 200 | else if (parameter == "InterestLifetime") { |
| 201 | m_interestLifetime = time::milliseconds(std::stoul(value)); |
| 202 | } |
| 203 | else if (parameter == "NextHopFaceId") { |
| 204 | m_nextHopFaceId = std::stoull(value); |
| 205 | } |
| 206 | else if (parameter == "ExpectedContent") { |
| 207 | m_expectedContent = value; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 208 | } |
| 209 | else { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 210 | logger.log("Line " + std::to_string(lineNumber) + " - Ignoring unknown parameter: " + parameter, |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 211 | false, true); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 212 | } |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | bool |
| 217 | checkTrafficDetailCorrectness() const |
| 218 | { |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | public: |
| 223 | uint8_t m_trafficPercentage = 0; |
| 224 | std::string m_name; |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 225 | std::optional<std::size_t> m_nameAppendBytes; |
| 226 | std::optional<uint64_t> m_nameAppendSeqNum; |
Davide Pesavento | 56f79d6 | 2019-01-26 17:30:32 -0500 | [diff] [blame] | 227 | bool m_canBePrefix = false; |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 228 | bool m_mustBeFresh = false; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 229 | uint8_t m_nonceDuplicationPercentage = 0; |
| 230 | time::milliseconds m_interestLifetime = -1_ms; |
| 231 | uint64_t m_nextHopFaceId = 0; |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 232 | std::optional<std::string> m_expectedContent; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 233 | |
| 234 | uint64_t m_nInterestsSent = 0; |
| 235 | uint64_t m_nInterestsReceived = 0; |
| 236 | uint64_t m_nNacks = 0; |
| 237 | uint64_t m_nContentInconsistencies = 0; |
| 238 | |
| 239 | // RTT is stored as milliseconds with fractional sub-milliseconds precision |
| 240 | double m_minimumInterestRoundTripTime = std::numeric_limits<double>::max(); |
| 241 | double m_maximumInterestRoundTripTime = 0; |
| 242 | double m_totalInterestRoundTripTime = 0; |
| 243 | }; |
| 244 | |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 245 | void |
| 246 | logStatistics() |
| 247 | { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 248 | m_logger.log("\n\n== Interest Traffic Report ==\n", false, true); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 249 | m_logger.log("Total Traffic Pattern Types = " + |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 250 | std::to_string(m_trafficPatterns.size()), false, true); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 251 | m_logger.log("Total Interests Sent = " + |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 252 | std::to_string(m_nInterestsSent), false, true); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 253 | m_logger.log("Total Responses Received = " + |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 254 | std::to_string(m_nInterestsReceived), false, true); |
Eric Newberry | 976c204 | 2016-06-19 23:37:35 -0700 | [diff] [blame] | 255 | m_logger.log("Total Nacks Received = " + |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 256 | std::to_string(m_nNacks), false, true); |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 257 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 258 | double loss = 0.0; |
| 259 | if (m_nInterestsSent > 0) { |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 260 | loss = (m_nInterestsSent - m_nInterestsReceived) * 100.0 / m_nInterestsSent; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 261 | } |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 262 | m_logger.log("Total Interest Loss = " + std::to_string(loss) + "%", false, true); |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 263 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 264 | double average = 0.0; |
| 265 | double inconsistency = 0.0; |
| 266 | if (m_nInterestsReceived > 0) { |
| 267 | average = m_totalInterestRoundTripTime / m_nInterestsReceived; |
| 268 | inconsistency = m_nContentInconsistencies * 100.0 / m_nInterestsReceived; |
| 269 | } |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 270 | m_logger.log("Total Data Inconsistency = " + |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 271 | std::to_string(inconsistency) + "%", false, true); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 272 | m_logger.log("Total Round Trip Time = " + |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 273 | std::to_string(m_totalInterestRoundTripTime) + "ms", false, true); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 274 | m_logger.log("Average Round Trip Time = " + |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 275 | std::to_string(average) + "ms\n", false, true); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 276 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 277 | for (std::size_t patternId = 0; patternId < m_trafficPatterns.size(); patternId++) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 278 | m_logger.log("Traffic Pattern Type #" + std::to_string(patternId + 1), false, true); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 279 | m_trafficPatterns[patternId].printTrafficConfiguration(m_logger); |
| 280 | m_logger.log("Total Interests Sent = " + |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 281 | std::to_string(m_trafficPatterns[patternId].m_nInterestsSent), false, true); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 282 | m_logger.log("Total Responses Received = " + |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 283 | std::to_string(m_trafficPatterns[patternId].m_nInterestsReceived), false, true); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 284 | m_logger.log("Total Nacks Received = " + |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 285 | std::to_string(m_trafficPatterns[patternId].m_nNacks), false, true); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 286 | loss = 0; |
| 287 | if (m_trafficPatterns[patternId].m_nInterestsSent > 0) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 288 | loss = m_trafficPatterns[patternId].m_nInterestsSent - m_trafficPatterns[patternId].m_nInterestsReceived; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 289 | loss *= 100.0; |
| 290 | loss /= m_trafficPatterns[patternId].m_nInterestsSent; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 291 | } |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 292 | m_logger.log("Total Interest Loss = " + std::to_string(loss) + "%", false, true); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 293 | average = 0; |
| 294 | inconsistency = 0; |
| 295 | if (m_trafficPatterns[patternId].m_nInterestsReceived > 0) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 296 | average = m_trafficPatterns[patternId].m_totalInterestRoundTripTime / |
| 297 | m_trafficPatterns[patternId].m_nInterestsReceived; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 298 | inconsistency = m_trafficPatterns[patternId].m_nContentInconsistencies; |
| 299 | inconsistency *= 100.0 / m_trafficPatterns[patternId].m_nInterestsReceived; |
| 300 | } |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 301 | m_logger.log("Total Data Inconsistency = " + std::to_string(inconsistency) + "%", false, true); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 302 | m_logger.log("Total Round Trip Time = " + |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 303 | std::to_string(m_trafficPatterns[patternId].m_totalInterestRoundTripTime) + "ms", false, true); |
| 304 | m_logger.log("Average Round Trip Time = " + std::to_string(average) + "ms\n", false, true); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 305 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | bool |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 309 | checkTrafficPatternCorrectness() const |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 310 | { |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 311 | // TODO |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 312 | return true; |
| 313 | } |
| 314 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 315 | uint32_t |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 316 | getNewNonce() |
| 317 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 318 | if (m_nonces.size() >= 1000) |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 319 | m_nonces.clear(); |
jeraldabraham | 473ef3d | 2014-03-06 12:40:35 -0700 | [diff] [blame] | 320 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 321 | auto randomNonce = ndn::random::generateWord32(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 322 | while (std::find(m_nonces.begin(), m_nonces.end(), randomNonce) != m_nonces.end()) |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 323 | randomNonce = ndn::random::generateWord32(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 324 | |
| 325 | m_nonces.push_back(randomNonce); |
| 326 | return randomNonce; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 329 | uint32_t |
| 330 | getOldNonce() |
| 331 | { |
| 332 | if (m_nonces.empty()) |
| 333 | return getNewNonce(); |
| 334 | |
| 335 | std::uniform_int_distribution<std::size_t> dist(0, m_nonces.size() - 1); |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 336 | return m_nonces[dist(ndn::random::getRandomNumberEngine())]; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 337 | } |
| 338 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 339 | static auto |
Davide Pesavento | 912d2e8 | 2019-01-10 17:04:31 -0500 | [diff] [blame] | 340 | generateRandomNameComponent(std::size_t length) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 341 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 342 | // per ISO C++ std, cannot instantiate uniform_int_distribution with uint8_t |
| 343 | static std::uniform_int_distribution<unsigned short> dist(std::numeric_limits<uint8_t>::min(), |
| 344 | std::numeric_limits<uint8_t>::max()); |
| 345 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 346 | ndn::Buffer buf(length); |
Davide Pesavento | 912d2e8 | 2019-01-10 17:04:31 -0500 | [diff] [blame] | 347 | for (std::size_t i = 0; i < length; i++) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 348 | buf[i] = static_cast<uint8_t>(dist(ndn::random::getRandomNumberEngine())); |
Eric Newberry | 3b28419 | 2015-07-06 21:44:46 -0700 | [diff] [blame] | 349 | } |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 350 | return ndn::name::Component(buf); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 351 | } |
| 352 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 353 | auto |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 354 | prepareInterest(std::size_t patternId) |
| 355 | { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 356 | ndn::Interest interest; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 357 | auto& pattern = m_trafficPatterns[patternId]; |
| 358 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 359 | ndn::Name name(pattern.m_name); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 360 | if (pattern.m_nameAppendBytes > 0) { |
| 361 | name.append(generateRandomNameComponent(*pattern.m_nameAppendBytes)); |
| 362 | } |
| 363 | if (pattern.m_nameAppendSeqNum) { |
| 364 | auto seqNum = *pattern.m_nameAppendSeqNum; |
| 365 | name.appendSequenceNumber(seqNum); |
| 366 | pattern.m_nameAppendSeqNum = seqNum + 1; |
| 367 | } |
| 368 | interest.setName(name); |
| 369 | |
Davide Pesavento | 56f79d6 | 2019-01-26 17:30:32 -0500 | [diff] [blame] | 370 | interest.setCanBePrefix(pattern.m_canBePrefix); |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 371 | interest.setMustBeFresh(pattern.m_mustBeFresh); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 372 | |
| 373 | static std::uniform_int_distribution<> duplicateNonceDist(1, 100); |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 374 | if (duplicateNonceDist(ndn::random::getRandomNumberEngine()) <= pattern.m_nonceDuplicationPercentage) |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 375 | interest.setNonce(getOldNonce()); |
| 376 | else |
| 377 | interest.setNonce(getNewNonce()); |
| 378 | |
| 379 | if (pattern.m_interestLifetime >= 0_ms) |
| 380 | interest.setInterestLifetime(pattern.m_interestLifetime); |
| 381 | |
| 382 | if (pattern.m_nextHopFaceId > 0) |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 383 | interest.setTag(std::make_shared<ndn::lp::NextHopFaceIdTag>(pattern.m_nextHopFaceId)); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 384 | |
| 385 | return interest; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 389 | onData(const ndn::Data& data, int globalRef, int localRef, std::size_t patternId, |
| 390 | const time::steady_clock::time_point& sentTime) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 391 | { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 392 | auto logLine = "Data Received - PatternType=" + std::to_string(patternId + 1) + |
| 393 | ", GlobalID=" + std::to_string(globalRef) + |
| 394 | ", LocalID=" + std::to_string(localRef) + |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 395 | ", Name=" + data.getName().toUri(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 396 | |
| 397 | m_nInterestsReceived++; |
| 398 | m_trafficPatterns[patternId].m_nInterestsReceived++; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 399 | |
| 400 | if (m_trafficPatterns[patternId].m_expectedContent) { |
Davide Pesavento | 9266906 | 2022-03-10 19:09:44 -0500 | [diff] [blame] | 401 | std::string receivedContent = readString(data.getContent()); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 402 | if (receivedContent != *m_trafficPatterns[patternId].m_expectedContent) { |
Davide Pesavento | 912d2e8 | 2019-01-10 17:04:31 -0500 | [diff] [blame] | 403 | m_nContentInconsistencies++; |
| 404 | m_trafficPatterns[patternId].m_nContentInconsistencies++; |
| 405 | logLine += ", IsConsistent=No"; |
jeraldabraham | 473ef3d | 2014-03-06 12:40:35 -0700 | [diff] [blame] | 406 | } |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 407 | else { |
Davide Pesavento | 912d2e8 | 2019-01-10 17:04:31 -0500 | [diff] [blame] | 408 | logLine += ", IsConsistent=Yes"; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 409 | } |
Davide Pesavento | 912d2e8 | 2019-01-10 17:04:31 -0500 | [diff] [blame] | 410 | } |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 411 | else { |
jeraldabraham | 473ef3d | 2014-03-06 12:40:35 -0700 | [diff] [blame] | 412 | logLine += ", IsConsistent=NotChecked"; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 413 | } |
| 414 | if (!m_wantQuiet) { |
jeraldabraham | 420dbf0 | 2014-04-25 22:58:31 -0700 | [diff] [blame] | 415 | m_logger.log(logLine, true, false); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 416 | } |
| 417 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 418 | double roundTripTime = (time::steady_clock::now() - sentTime).count() / 1000000.0; |
| 419 | if (m_minimumInterestRoundTripTime > roundTripTime) |
| 420 | m_minimumInterestRoundTripTime = roundTripTime; |
| 421 | if (m_maximumInterestRoundTripTime < roundTripTime) |
| 422 | m_maximumInterestRoundTripTime = roundTripTime; |
| 423 | if (m_trafficPatterns[patternId].m_minimumInterestRoundTripTime > roundTripTime) |
| 424 | m_trafficPatterns[patternId].m_minimumInterestRoundTripTime = roundTripTime; |
| 425 | if (m_trafficPatterns[patternId].m_maximumInterestRoundTripTime < roundTripTime) |
| 426 | m_trafficPatterns[patternId].m_maximumInterestRoundTripTime = roundTripTime; |
| 427 | m_totalInterestRoundTripTime += roundTripTime; |
| 428 | m_trafficPatterns[patternId].m_totalInterestRoundTripTime += roundTripTime; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 429 | |
| 430 | if (m_nMaximumInterests == globalRef) { |
| 431 | stop(); |
Davide Pesavento | 912d2e8 | 2019-01-10 17:04:31 -0500 | [diff] [blame] | 432 | } |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 436 | onNack(const ndn::Interest& interest, const ndn::lp::Nack& nack, |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 437 | int globalRef, int localRef, std::size_t patternId) |
Eric Newberry | 976c204 | 2016-06-19 23:37:35 -0700 | [diff] [blame] | 438 | { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 439 | auto logLine = "Interest Nack'd - PatternType=" + std::to_string(patternId + 1) + |
| 440 | ", GlobalID=" + std::to_string(globalRef) + |
| 441 | ", LocalID=" + std::to_string(localRef) + |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 442 | ", Name=" + interest.getName().toUri() + |
| 443 | ", NackReason=" + boost::lexical_cast<std::string>(nack.getReason()); |
Eric Newberry | 976c204 | 2016-06-19 23:37:35 -0700 | [diff] [blame] | 444 | m_logger.log(logLine, true, false); |
| 445 | |
| 446 | m_nNacks++; |
| 447 | m_trafficPatterns[patternId].m_nNacks++; |
| 448 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 449 | if (m_nMaximumInterests == globalRef) { |
| 450 | stop(); |
Eric Newberry | 976c204 | 2016-06-19 23:37:35 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | void |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 455 | onTimeout(const ndn::Interest& interest, int globalRef, int localRef, int patternId) |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 456 | { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 457 | auto logLine = "Interest Timed Out - PatternType=" + std::to_string(patternId + 1) + |
| 458 | ", GlobalID=" + std::to_string(globalRef) + |
| 459 | ", LocalID=" + std::to_string(localRef) + |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 460 | ", Name=" + interest.getName().toUri(); |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 461 | m_logger.log(logLine, true, false); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 462 | |
| 463 | if (m_nMaximumInterests == globalRef) { |
| 464 | stop(); |
| 465 | } |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | void |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 469 | generateTraffic(boost::asio::deadline_timer& timer) |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 470 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 471 | if (m_nMaximumInterests && m_nInterestsSent >= *m_nMaximumInterests) { |
Davide Pesavento | 912d2e8 | 2019-01-10 17:04:31 -0500 | [diff] [blame] | 472 | return; |
| 473 | } |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 474 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 475 | static std::uniform_int_distribution<> trafficDist(1, 100); |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 476 | int trafficKey = trafficDist(ndn::random::getRandomNumberEngine()); |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 477 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 478 | int cumulativePercentage = 0; |
| 479 | std::size_t patternId = 0; |
| 480 | for (; patternId < m_trafficPatterns.size(); patternId++) { |
| 481 | cumulativePercentage += m_trafficPatterns[patternId].m_trafficPercentage; |
| 482 | if (trafficKey <= cumulativePercentage) { |
| 483 | auto interest = prepareInterest(patternId); |
| 484 | try { |
| 485 | m_nInterestsSent++; |
| 486 | m_trafficPatterns[patternId].m_nInterestsSent++; |
| 487 | auto sentTime = time::steady_clock::now(); |
| 488 | m_face.expressInterest(interest, |
| 489 | bind(&NdnTrafficClient::onData, this, _2, m_nInterestsSent, |
| 490 | m_trafficPatterns[patternId].m_nInterestsSent, patternId, sentTime), |
| 491 | bind(&NdnTrafficClient::onNack, this, _1, _2, m_nInterestsSent, |
| 492 | m_trafficPatterns[patternId].m_nInterestsSent, patternId), |
| 493 | bind(&NdnTrafficClient::onTimeout, this, _1, m_nInterestsSent, |
| 494 | m_trafficPatterns[patternId].m_nInterestsSent, patternId)); |
| 495 | |
| 496 | if (!m_wantQuiet) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 497 | auto logLine = "Sending Interest - PatternType=" + std::to_string(patternId + 1) + |
| 498 | ", GlobalID=" + std::to_string(m_nInterestsSent) + |
| 499 | ", LocalID=" + std::to_string(m_trafficPatterns[patternId].m_nInterestsSent) + |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 500 | ", Name=" + interest.getName().toUri(); |
| 501 | m_logger.log(logLine, true, false); |
| 502 | } |
| 503 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 504 | timer.expires_at(timer.expires_at() + boost::posix_time::millisec(m_interestInterval.count())); |
| 505 | timer.async_wait([this, &timer] (auto&&...) { generateTraffic(timer); }); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 506 | } |
| 507 | catch (const std::exception& e) { |
| 508 | m_logger.log("ERROR: "s + e.what(), true, true); |
| 509 | } |
| 510 | break; |
| 511 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 512 | } |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 513 | if (patternId == m_trafficPatterns.size()) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 514 | timer.expires_at(timer.expires_at() + boost::posix_time::millisec(m_interestInterval.count())); |
| 515 | timer.async_wait([this, &timer] (auto&&...) { generateTraffic(timer); }); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 519 | void |
| 520 | stop() |
| 521 | { |
| 522 | if (m_nContentInconsistencies > 0 || m_nInterestsSent != m_nInterestsReceived) { |
| 523 | m_hasError = true; |
| 524 | } |
| 525 | |
| 526 | logStatistics(); |
| 527 | m_face.shutdown(); |
| 528 | m_ioService.stop(); |
| 529 | } |
| 530 | |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 531 | private: |
Alexander Afanasyev | 976c397 | 2014-05-26 17:03:40 +0300 | [diff] [blame] | 532 | boost::asio::io_service m_ioService; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 533 | boost::asio::signal_set m_signalSet; |
| 534 | Logger m_logger; |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 535 | ndn::Face m_face; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 536 | |
| 537 | std::string m_configurationFile; |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 538 | std::optional<uint64_t> m_nMaximumInterests; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 539 | time::milliseconds m_interestInterval = 1_s; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 540 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 541 | std::vector<InterestTrafficConfiguration> m_trafficPatterns; |
| 542 | std::vector<uint32_t> m_nonces; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 543 | uint64_t m_nInterestsSent = 0; |
| 544 | uint64_t m_nInterestsReceived = 0; |
| 545 | uint64_t m_nNacks = 0; |
| 546 | uint64_t m_nContentInconsistencies = 0; |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 547 | |
Davide Pesavento | 912d2e8 | 2019-01-10 17:04:31 -0500 | [diff] [blame] | 548 | // RTT is stored as milliseconds with fractional sub-milliseconds precision |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 549 | double m_minimumInterestRoundTripTime = std::numeric_limits<double>::max(); |
| 550 | double m_maximumInterestRoundTripTime = 0; |
| 551 | double m_totalInterestRoundTripTime = 0; |
| 552 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 553 | bool m_wantQuiet = false; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 554 | bool m_hasError = false; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 555 | }; |
| 556 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 557 | } // namespace ndntg |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 558 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 559 | static void |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 560 | usage(std::ostream& os, std::string_view programName, const po::options_description& desc) |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 561 | { |
| 562 | os << "Usage: " << programName << " [options] <Traffic_Configuration_File>\n" |
| 563 | << "\n" |
| 564 | << "Generate Interest traffic as per provided Traffic_Configuration_File.\n" |
| 565 | << "Interests are continuously generated unless a total number is specified.\n" |
| 566 | << "Set the environment variable NDN_TRAFFIC_LOGFOLDER to redirect output to a log file.\n" |
| 567 | << "\n" |
| 568 | << desc; |
| 569 | } |
| 570 | |
jeraldabraham | cc3c6c9 | 2014-03-28 02:21:45 -0700 | [diff] [blame] | 571 | int |
| 572 | main(int argc, char* argv[]) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 573 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 574 | std::string configFile; |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 575 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 576 | po::options_description visibleOptions("Options"); |
| 577 | visibleOptions.add_options() |
| 578 | ("help,h", "print this help message and exit") |
| 579 | ("count,c", po::value<int>(), "total number of Interests to be generated") |
| 580 | ("interval,i", po::value<ndn::time::milliseconds::rep>()->default_value(1000), |
| 581 | "Interest generation interval in milliseconds") |
| 582 | ("quiet,q", po::bool_switch(), "turn off logging of Interest generation/Data reception") |
| 583 | ; |
| 584 | |
| 585 | po::options_description hiddenOptions; |
| 586 | hiddenOptions.add_options() |
| 587 | ("config-file", po::value<std::string>(&configFile)) |
| 588 | ; |
| 589 | |
| 590 | po::positional_options_description posOptions; |
| 591 | posOptions.add("config-file", -1); |
| 592 | |
| 593 | po::options_description allOptions; |
| 594 | allOptions.add(visibleOptions).add(hiddenOptions); |
| 595 | |
| 596 | po::variables_map vm; |
| 597 | try { |
| 598 | po::store(po::command_line_parser(argc, argv).options(allOptions).positional(posOptions).run(), vm); |
| 599 | po::notify(vm); |
| 600 | } |
| 601 | catch (const po::error& e) { |
| 602 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 603 | return 2; |
| 604 | } |
| 605 | catch (const boost::bad_any_cast& e) { |
| 606 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 607 | return 2; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 610 | if (vm.count("help") > 0) { |
| 611 | usage(std::cout, argv[0], visibleOptions); |
| 612 | return 0; |
| 613 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 614 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 615 | if (configFile.empty()) { |
| 616 | usage(std::cerr, argv[0], visibleOptions); |
| 617 | return 2; |
| 618 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 619 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 620 | ndntg::NdnTrafficClient client(configFile); |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 621 | |
| 622 | if (vm.count("count") > 0) { |
| 623 | int count = vm["count"].as<int>(); |
| 624 | if (count < 0) { |
| 625 | std::cerr << "ERROR: the argument for option '--count' cannot be negative" << std::endl; |
| 626 | return 2; |
| 627 | } |
| 628 | client.setMaximumInterests(static_cast<uint64_t>(count)); |
| 629 | } |
| 630 | |
| 631 | if (vm.count("interval") > 0) { |
| 632 | ndn::time::milliseconds interval(vm["interval"].as<ndn::time::milliseconds::rep>()); |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 633 | if (interval <= 0_ms) { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 634 | std::cerr << "ERROR: the argument for option '--interval' must be positive" << std::endl; |
| 635 | return 2; |
| 636 | } |
| 637 | client.setInterestInterval(interval); |
| 638 | } |
| 639 | |
| 640 | if (vm["quiet"].as<bool>()) { |
| 641 | client.setQuietLogging(); |
| 642 | } |
| 643 | |
Davide Pesavento | 306e5bc | 2019-01-26 16:20:34 -0500 | [diff] [blame] | 644 | return client.run(); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 645 | } |