blob: 0e7ad2a4db99d2ecc9a0b1071f4d7905348600fc [file] [log] [blame]
Eric Newberry4164b8e2015-04-23 17:29:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Teng Liang62884862016-03-31 18:15:36 -07003 * Copyright (c) 2015-2016, 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 *
19 * @author: Eric Newberry <enewberry@email.arizona.edu>
20 * @author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
Teng Liang62884862016-03-31 18:15:36 -070021 * @author: Teng Liang <philoliang@email.arizona.edu>
Eric Newberry4164b8e2015-04-23 17:29:18 -070022 */
23
24#include "statistics-collector.hpp"
25
26namespace ndn {
27namespace ping {
28namespace client {
29
30StatisticsCollector::StatisticsCollector(Ping& ping, const Options& options)
31 : m_ping(ping)
32 , m_options(options)
33 , m_nSent(0)
34 , m_nReceived(0)
Teng Liang62884862016-03-31 18:15:36 -070035 , m_nNacked(0)
Eric Newberry4164b8e2015-04-23 17:29:18 -070036 , 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{
Teng Liang62884862016-03-31 18:15:36 -070042 m_ping.afterData.connect(bind(&StatisticsCollector::recordData, this, _2));
43 m_ping.afterNack.connect(bind(&StatisticsCollector::recordNack, this));
Eric Newberry4164b8e2015-04-23 17:29:18 -070044 m_ping.afterTimeout.connect(bind(&StatisticsCollector::recordTimeout, this));
45}
46
47void
Teng Liang62884862016-03-31 18:15:36 -070048StatisticsCollector::recordData(Rtt rtt)
Eric Newberry4164b8e2015-04-23 17:29:18 -070049{
50 m_nSent++;
51 m_nReceived++;
52
53 double rttMs = rtt.count();
54
55 m_minRtt = std::min(m_minRtt, rttMs);
56
57 m_maxRtt = std::max(m_maxRtt, rttMs);
58
59 m_sumRtt += rttMs;
60 m_sumRttSquared += rttMs * rttMs;
61}
62
63void
Teng Liang62884862016-03-31 18:15:36 -070064StatisticsCollector::recordNack()
65{
66 m_nSent++;
67 m_nNacked++;
68}
69
70void
Eric Newberry4164b8e2015-04-23 17:29:18 -070071StatisticsCollector::recordTimeout()
72{
73 m_nSent++;
74}
75
76Statistics
77StatisticsCollector::computeStatistics()
78{
79 Statistics statistics;
80
81 statistics.prefix = m_options.prefix;
82 statistics.nSent = m_nSent;
83 statistics.nReceived = m_nReceived;
Teng Liang62884862016-03-31 18:15:36 -070084 statistics.nNacked = m_nNacked;
Eric Newberry4164b8e2015-04-23 17:29:18 -070085 statistics.pingStartTime = m_pingStartTime;
86 statistics.minRtt = m_minRtt;
87 statistics.maxRtt = m_maxRtt;
Teng Liang62884862016-03-31 18:15:36 -070088 statistics.packetLossRate = static_cast<double>(m_nSent - m_nReceived - m_nNacked) / static_cast<double>(m_nSent);
89 statistics.packetNackedRate = static_cast<double>(m_nNacked) / static_cast<double>(m_nSent);
Eric Newberry4164b8e2015-04-23 17:29:18 -070090 statistics.sumRtt = m_sumRtt;
91 statistics.avgRtt = m_sumRtt / m_nReceived;
92 statistics.stdDevRtt = std::sqrt((m_sumRttSquared / m_nReceived) - (statistics.avgRtt * statistics.avgRtt));
93
94 return statistics;
95}
96
97std::ostream&
Eric Newberrydce5a532015-05-06 10:46:52 -070098Statistics::printSummary(std::ostream& os) const
99{
100 os << nReceived << "/" << nSent << " packets, " << packetLossRate * 100.0
101 << "% loss, min/avg/max/mdev = " << minRtt << "/" << avgRtt << "/" << maxRtt << "/"
102 << stdDevRtt << " ms" << std::endl;
103
104 return os;
105}
106
107std::ostream&
Eric Newberry4164b8e2015-04-23 17:29:18 -0700108operator<<(std::ostream& os, const Statistics& statistics)
109{
110 os << "\n";
111 os << "--- " << statistics.prefix <<" ping statistics ---\n";
112 os << statistics.nSent << " packets transmitted";
113 os << ", " << statistics.nReceived << " received";
Teng Liang62884862016-03-31 18:15:36 -0700114 os << ", " << statistics.nNacked << " nacked";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700115 os << ", " << statistics.packetLossRate * 100.0 << "% packet loss";
Teng Liang62884862016-03-31 18:15:36 -0700116 os << ", " << statistics.packetNackedRate * 100.0 << "% nacked";
Eric Newberry4164b8e2015-04-23 17:29:18 -0700117 os << ", time " << statistics.sumRtt << " ms";
118 if (statistics.nReceived > 0) {
119 os << "\n";
120 os << "rtt min/avg/max/mdev = ";
121 os << statistics.minRtt << "/";
122 os << statistics.avgRtt << "/";
123 os << statistics.maxRtt << "/";
124 os << statistics.stdDevRtt << " ms";
125 }
126
127 return os;
128}
129
130} // namespace client
131} // namespace ping
132} // namespace ndn