Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015-2021, Arizona Board of Regents. |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 7 | * |
| 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 19 | * @author Eric Newberry <enewberry@email.arizona.edu> |
| 20 | * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 21 | * @author Teng Liang <philoliang@email.arizona.edu> |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include "statistics-collector.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace ping { |
| 28 | namespace client { |
| 29 | |
| 30 | StatisticsCollector::StatisticsCollector(Ping& ping, const Options& options) |
| 31 | : m_ping(ping) |
| 32 | , m_options(options) |
| 33 | , m_nSent(0) |
| 34 | , m_nReceived(0) |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 35 | , m_nNacked(0) |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 36 | , m_pingStartTime(time::steady_clock::now()) |
| 37 | , m_minRtt(std::numeric_limits<double>::max()) |
| 38 | , m_maxRtt(0.0) |
| 39 | , m_sumRtt(0.0) |
| 40 | , m_sumRttSquared(0.0) |
| 41 | { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 42 | m_ping.afterData.connect([this] (auto&&, Rtt rtt) { recordData(rtt); }); |
| 43 | m_ping.afterNack.connect([this] (auto&&...) { recordNack(); }); |
| 44 | m_ping.afterTimeout.connect([this] (auto&&...) { recordTimeout(); }); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 48 | StatisticsCollector::recordData(Rtt rtt) |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 49 | { |
| 50 | m_nSent++; |
| 51 | m_nReceived++; |
| 52 | |
| 53 | double rttMs = rtt.count(); |
| 54 | |
| 55 | m_minRtt = std::min(m_minRtt, rttMs); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 56 | m_maxRtt = std::max(m_maxRtt, rttMs); |
| 57 | |
| 58 | m_sumRtt += rttMs; |
| 59 | m_sumRttSquared += rttMs * rttMs; |
| 60 | } |
| 61 | |
| 62 | void |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 63 | StatisticsCollector::recordNack() |
| 64 | { |
| 65 | m_nSent++; |
| 66 | m_nNacked++; |
| 67 | } |
| 68 | |
| 69 | void |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 70 | StatisticsCollector::recordTimeout() |
| 71 | { |
| 72 | m_nSent++; |
| 73 | } |
| 74 | |
| 75 | Statistics |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 76 | StatisticsCollector::computeStatistics() const |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 77 | { |
| 78 | Statistics statistics; |
| 79 | |
| 80 | statistics.prefix = m_options.prefix; |
| 81 | statistics.nSent = m_nSent; |
| 82 | statistics.nReceived = m_nReceived; |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 83 | statistics.nNacked = m_nNacked; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 84 | statistics.pingStartTime = m_pingStartTime; |
| 85 | statistics.minRtt = m_minRtt; |
| 86 | statistics.maxRtt = m_maxRtt; |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 87 | |
| 88 | if (m_nSent > 0) { |
| 89 | statistics.packetLossRate = static_cast<double>(m_nSent - m_nReceived - m_nNacked) / static_cast<double>(m_nSent); |
| 90 | statistics.packetNackedRate = static_cast<double>(m_nNacked) / static_cast<double>(m_nSent); |
| 91 | } |
| 92 | else { |
| 93 | statistics.packetLossRate = std::numeric_limits<double>::quiet_NaN(); |
| 94 | statistics.packetNackedRate = std::numeric_limits<double>::quiet_NaN(); |
| 95 | } |
| 96 | |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 97 | statistics.sumRtt = m_sumRtt; |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 98 | |
| 99 | if (m_nReceived > 0) { |
| 100 | statistics.avgRtt = m_sumRtt / m_nReceived; |
| 101 | statistics.stdDevRtt = std::sqrt((m_sumRttSquared / m_nReceived) - (statistics.avgRtt * statistics.avgRtt)); |
| 102 | } |
| 103 | else { |
| 104 | statistics.avgRtt = std::numeric_limits<double>::quiet_NaN(); |
| 105 | statistics.stdDevRtt = std::numeric_limits<double>::quiet_NaN(); |
| 106 | } |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 107 | |
| 108 | return statistics; |
| 109 | } |
| 110 | |
| 111 | std::ostream& |
Eric Newberry | dce5a53 | 2015-05-06 10:46:52 -0700 | [diff] [blame] | 112 | Statistics::printSummary(std::ostream& os) const |
| 113 | { |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 114 | os << nReceived << "/" << nSent << " packets"; |
Eric Newberry | dce5a53 | 2015-05-06 10:46:52 -0700 | [diff] [blame] | 115 | |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 116 | if (nSent > 0) { |
| 117 | os << ", " << packetLossRate * 100.0 << "% lost, " << packetNackedRate * 100.0 << "% nacked"; |
| 118 | } |
| 119 | |
| 120 | if (nReceived > 0) { |
| 121 | os << ", min/avg/max/mdev = " << minRtt << "/" << avgRtt << "/" << maxRtt << "/" << stdDevRtt |
| 122 | << " ms"; |
| 123 | } |
| 124 | |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 125 | return os << "\n"; |
Eric Newberry | dce5a53 | 2015-05-06 10:46:52 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | std::ostream& |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 129 | operator<<(std::ostream& os, const Statistics& statistics) |
| 130 | { |
| 131 | os << "\n"; |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 132 | os << "--- " << statistics.prefix << " ping statistics ---\n"; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 133 | os << statistics.nSent << " packets transmitted"; |
| 134 | os << ", " << statistics.nReceived << " received"; |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 135 | os << ", " << statistics.nNacked << " nacked"; |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 136 | |
| 137 | if (statistics.nSent > 0) { |
| 138 | os << ", " << statistics.packetLossRate * 100.0 << "% lost"; |
| 139 | os << ", " << statistics.packetNackedRate * 100.0 << "% nacked"; |
| 140 | } |
| 141 | |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 142 | os << ", time " << statistics.sumRtt << " ms"; |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 143 | |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 144 | if (statistics.nReceived > 0) { |
| 145 | os << "\n"; |
| 146 | os << "rtt min/avg/max/mdev = "; |
| 147 | os << statistics.minRtt << "/"; |
| 148 | os << statistics.avgRtt << "/"; |
| 149 | os << statistics.maxRtt << "/"; |
| 150 | os << statistics.stdDevRtt << " ms"; |
| 151 | } |
| 152 | |
| 153 | return os; |
| 154 | } |
| 155 | |
| 156 | } // namespace client |
| 157 | } // namespace ping |
| 158 | } // namespace ndn |