blob: 0b6b39f776d9e6636a23f6a88209bd14a9c321d1 [file] [log] [blame]
Eric Newberry4164b8e2015-04-23 17:29:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventof8d9a532021-07-03 16:04:12 -04002/*
Davide Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2015-2022, Arizona Board of Regents.
Eric Newberry4164b8e2015-04-23 17:29:18 -07004 *
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 Pesaventof8d9a532021-07-03 16:04:12 -040019 * @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 Newberry4164b8e2015-04-23 17:29:18 -070022 */
23
24#include "statistics-collector.hpp"
25
Davide Pesaventob3570c62022-02-19 19:19:00 -050026namespace ndn::ping::client {
Eric Newberry4164b8e2015-04-23 17:29:18 -070027
28StatisticsCollector::StatisticsCollector(Ping& ping, const Options& options)
29 : m_ping(ping)
30 , m_options(options)
Eric Newberry4164b8e2015-04-23 17:29:18 -070031{
Davide Pesaventof8d9a532021-07-03 16:04:12 -040032 m_ping.afterData.connect([this] (auto&&, Rtt rtt) { recordData(rtt); });
33 m_ping.afterNack.connect([this] (auto&&...) { recordNack(); });
34 m_ping.afterTimeout.connect([this] (auto&&...) { recordTimeout(); });
Eric Newberry4164b8e2015-04-23 17:29:18 -070035}
36
37void
Teng Liang62884862016-03-31 18:15:36 -070038StatisticsCollector::recordData(Rtt rtt)
Eric Newberry4164b8e2015-04-23 17:29:18 -070039{
40 m_nSent++;
41 m_nReceived++;
42
43 double rttMs = rtt.count();
44
45 m_minRtt = std::min(m_minRtt, rttMs);
Eric Newberry4164b8e2015-04-23 17:29:18 -070046 m_maxRtt = std::max(m_maxRtt, rttMs);
47
48 m_sumRtt += rttMs;
49 m_sumRttSquared += rttMs * rttMs;
50}
51
52void
Teng Liang62884862016-03-31 18:15:36 -070053StatisticsCollector::recordNack()
54{
55 m_nSent++;
56 m_nNacked++;
57}
58
59void
Eric Newberry4164b8e2015-04-23 17:29:18 -070060StatisticsCollector::recordTimeout()
61{
62 m_nSent++;
63}
64
65Statistics
Davide Pesaventof8d9a532021-07-03 16:04:12 -040066StatisticsCollector::computeStatistics() const
Eric Newberry4164b8e2015-04-23 17:29:18 -070067{
68 Statistics statistics;
69
70 statistics.prefix = m_options.prefix;
71 statistics.nSent = m_nSent;
72 statistics.nReceived = m_nReceived;
Teng Liang62884862016-03-31 18:15:36 -070073 statistics.nNacked = m_nNacked;
Eric Newberry4164b8e2015-04-23 17:29:18 -070074 statistics.pingStartTime = m_pingStartTime;
75 statistics.minRtt = m_minRtt;
76 statistics.maxRtt = m_maxRtt;
Eric Newberryf1628392016-12-24 19:40:39 -070077
78 if (m_nSent > 0) {
79 statistics.packetLossRate = static_cast<double>(m_nSent - m_nReceived - m_nNacked) / static_cast<double>(m_nSent);
80 statistics.packetNackedRate = static_cast<double>(m_nNacked) / static_cast<double>(m_nSent);
81 }
82 else {
83 statistics.packetLossRate = std::numeric_limits<double>::quiet_NaN();
84 statistics.packetNackedRate = std::numeric_limits<double>::quiet_NaN();
85 }
86
Eric Newberry4164b8e2015-04-23 17:29:18 -070087 statistics.sumRtt = m_sumRtt;
Eric Newberryf1628392016-12-24 19:40:39 -070088
89 if (m_nReceived > 0) {
90 statistics.avgRtt = m_sumRtt / m_nReceived;
91 statistics.stdDevRtt = std::sqrt((m_sumRttSquared / m_nReceived) - (statistics.avgRtt * statistics.avgRtt));
92 }
93 else {
94 statistics.avgRtt = std::numeric_limits<double>::quiet_NaN();
95 statistics.stdDevRtt = std::numeric_limits<double>::quiet_NaN();
96 }
Eric Newberry4164b8e2015-04-23 17:29:18 -070097
98 return statistics;
99}
100
101std::ostream&
Eric Newberrydce5a532015-05-06 10:46:52 -0700102Statistics::printSummary(std::ostream& os) const
103{
Eric Newberryf1628392016-12-24 19:40:39 -0700104 os << nReceived << "/" << nSent << " packets";
Eric Newberrydce5a532015-05-06 10:46:52 -0700105
Eric Newberryf1628392016-12-24 19:40:39 -0700106 if (nSent > 0) {
107 os << ", " << packetLossRate * 100.0 << "% lost, " << packetNackedRate * 100.0 << "% nacked";
108 }
109
110 if (nReceived > 0) {
111 os << ", min/avg/max/mdev = " << minRtt << "/" << avgRtt << "/" << maxRtt << "/" << stdDevRtt
112 << " ms";
113 }
114
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400115 return os << "\n";
Eric Newberrydce5a532015-05-06 10:46:52 -0700116}
117
118std::ostream&
Eric Newberry4164b8e2015-04-23 17:29:18 -0700119operator<<(std::ostream& os, const Statistics& statistics)
120{
121 os << "\n";
Eric Newberryf1628392016-12-24 19:40:39 -0700122 os << "--- " << statistics.prefix << " ping statistics ---\n";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700123 os << statistics.nSent << " packets transmitted";
124 os << ", " << statistics.nReceived << " received";
Teng Liang62884862016-03-31 18:15:36 -0700125 os << ", " << statistics.nNacked << " nacked";
Eric Newberryf1628392016-12-24 19:40:39 -0700126
127 if (statistics.nSent > 0) {
128 os << ", " << statistics.packetLossRate * 100.0 << "% lost";
129 os << ", " << statistics.packetNackedRate * 100.0 << "% nacked";
130 }
131
Eric Newberry4164b8e2015-04-23 17:29:18 -0700132 os << ", time " << statistics.sumRtt << " ms";
Eric Newberryf1628392016-12-24 19:40:39 -0700133
Eric Newberry4164b8e2015-04-23 17:29:18 -0700134 if (statistics.nReceived > 0) {
135 os << "\n";
136 os << "rtt min/avg/max/mdev = ";
137 os << statistics.minRtt << "/";
138 os << statistics.avgRtt << "/";
139 os << statistics.maxRtt << "/";
140 os << statistics.stdDevRtt << " ms";
141 }
142
143 return os;
144}
145
Davide Pesaventob3570c62022-02-19 19:19:00 -0500146} // namespace ndn::ping::client