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