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 | /* |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2015-2024, 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 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 26 | #include <cmath> |
| 27 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 28 | namespace ndn::ping::client { |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 29 | |
| 30 | StatisticsCollector::StatisticsCollector(Ping& ping, const Options& options) |
| 31 | : m_ping(ping) |
| 32 | , m_options(options) |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 33 | { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 34 | m_ping.afterData.connect([this] (auto&&, Rtt rtt) { recordData(rtt); }); |
| 35 | m_ping.afterNack.connect([this] (auto&&...) { recordNack(); }); |
| 36 | m_ping.afterTimeout.connect([this] (auto&&...) { recordTimeout(); }); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 40 | StatisticsCollector::recordData(Rtt rtt) |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 41 | { |
| 42 | m_nSent++; |
| 43 | m_nReceived++; |
| 44 | |
| 45 | double rttMs = rtt.count(); |
| 46 | |
| 47 | m_minRtt = std::min(m_minRtt, rttMs); |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 48 | m_maxRtt = std::max(m_maxRtt, rttMs); |
| 49 | |
| 50 | m_sumRtt += rttMs; |
| 51 | m_sumRttSquared += rttMs * rttMs; |
| 52 | } |
| 53 | |
| 54 | void |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 55 | StatisticsCollector::recordNack() |
| 56 | { |
| 57 | m_nSent++; |
| 58 | m_nNacked++; |
| 59 | } |
| 60 | |
| 61 | void |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 62 | StatisticsCollector::recordTimeout() |
| 63 | { |
| 64 | m_nSent++; |
| 65 | } |
| 66 | |
| 67 | Statistics |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 68 | StatisticsCollector::computeStatistics() const |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 69 | { |
| 70 | Statistics statistics; |
| 71 | |
| 72 | statistics.prefix = m_options.prefix; |
| 73 | statistics.nSent = m_nSent; |
| 74 | statistics.nReceived = m_nReceived; |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 75 | statistics.nNacked = m_nNacked; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 76 | statistics.pingStartTime = m_pingStartTime; |
| 77 | statistics.minRtt = m_minRtt; |
| 78 | statistics.maxRtt = m_maxRtt; |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 79 | |
| 80 | if (m_nSent > 0) { |
| 81 | statistics.packetLossRate = static_cast<double>(m_nSent - m_nReceived - m_nNacked) / static_cast<double>(m_nSent); |
| 82 | statistics.packetNackedRate = static_cast<double>(m_nNacked) / static_cast<double>(m_nSent); |
| 83 | } |
| 84 | else { |
| 85 | statistics.packetLossRate = std::numeric_limits<double>::quiet_NaN(); |
| 86 | statistics.packetNackedRate = std::numeric_limits<double>::quiet_NaN(); |
| 87 | } |
| 88 | |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 89 | statistics.sumRtt = m_sumRtt; |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 90 | |
| 91 | if (m_nReceived > 0) { |
| 92 | statistics.avgRtt = m_sumRtt / m_nReceived; |
| 93 | statistics.stdDevRtt = std::sqrt((m_sumRttSquared / m_nReceived) - (statistics.avgRtt * statistics.avgRtt)); |
| 94 | } |
| 95 | else { |
| 96 | statistics.avgRtt = std::numeric_limits<double>::quiet_NaN(); |
| 97 | statistics.stdDevRtt = std::numeric_limits<double>::quiet_NaN(); |
| 98 | } |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 99 | |
| 100 | return statistics; |
| 101 | } |
| 102 | |
Davide Pesavento | c8625bb | 2024-02-12 15:06:31 -0500 | [diff] [blame^] | 103 | void |
Eric Newberry | dce5a53 | 2015-05-06 10:46:52 -0700 | [diff] [blame] | 104 | Statistics::printSummary(std::ostream& os) const |
| 105 | { |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 106 | os << nReceived << "/" << nSent << " packets"; |
Eric Newberry | dce5a53 | 2015-05-06 10:46:52 -0700 | [diff] [blame] | 107 | |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 108 | if (nSent > 0) { |
| 109 | os << ", " << packetLossRate * 100.0 << "% lost, " << packetNackedRate * 100.0 << "% nacked"; |
| 110 | } |
| 111 | |
| 112 | if (nReceived > 0) { |
Davide Pesavento | c8625bb | 2024-02-12 15:06:31 -0500 | [diff] [blame^] | 113 | os << ", min/avg/max/mdev = " << minRtt << "/" << avgRtt << "/" << maxRtt |
| 114 | << "/" << stdDevRtt << " ms"; |
Eric Newberry | f162839 | 2016-12-24 19:40:39 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Davide Pesavento | c8625bb | 2024-02-12 15:06:31 -0500 | [diff] [blame^] | 117 | os << "\n"; |
Eric Newberry | dce5a53 | 2015-05-06 10:46:52 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Davide Pesavento | c8625bb | 2024-02-12 15:06:31 -0500 | [diff] [blame^] | 120 | void |
| 121 | Statistics::printFull(std::ostream& os) const |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 122 | { |
Davide Pesavento | c8625bb | 2024-02-12 15:06:31 -0500 | [diff] [blame^] | 123 | os << "\n--- " << prefix << " ping statistics ---\n" |
| 124 | << nSent << " packets transmitted" |
| 125 | << ", " << nReceived << " received" |
| 126 | << ", " << nNacked << " nacked"; |
| 127 | |
| 128 | if (nSent > 0) { |
| 129 | os << ", " << packetLossRate * 100.0 << "% lost, " << packetNackedRate * 100.0 << "% nacked"; |
| 130 | } |
| 131 | |
| 132 | os << ", time " << sumRtt << " ms"; |
| 133 | |
| 134 | if (nReceived > 0) { |
| 135 | os << "\nrtt min/avg/max/mdev = " << minRtt << "/" << avgRtt << "/" << maxRtt |
| 136 | << "/" << stdDevRtt << " ms"; |
| 137 | } |
| 138 | |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 139 | os << "\n"; |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 142 | } // namespace ndn::ping::client |