Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2015, Arizona Board of Regents. |
| 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 | * |
| 19 | * @author: Eric Newberry <enewberry@email.arizona.edu> |
| 20 | * @author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 21 | */ |
| 22 | |
| 23 | #include "statistics-collector.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ping { |
| 27 | namespace client { |
| 28 | |
| 29 | StatisticsCollector::StatisticsCollector(Ping& ping, const Options& options) |
| 30 | : m_ping(ping) |
| 31 | , m_options(options) |
| 32 | , m_nSent(0) |
| 33 | , m_nReceived(0) |
| 34 | , m_pingStartTime(time::steady_clock::now()) |
| 35 | , m_minRtt(std::numeric_limits<double>::max()) |
| 36 | , m_maxRtt(0.0) |
| 37 | , m_sumRtt(0.0) |
| 38 | , m_sumRttSquared(0.0) |
| 39 | { |
| 40 | m_ping.afterResponse.connect(bind(&StatisticsCollector::recordResponse, this, _2)); |
| 41 | m_ping.afterTimeout.connect(bind(&StatisticsCollector::recordTimeout, this)); |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | StatisticsCollector::recordResponse(Rtt rtt) |
| 46 | { |
| 47 | m_nSent++; |
| 48 | m_nReceived++; |
| 49 | |
| 50 | double rttMs = rtt.count(); |
| 51 | |
| 52 | m_minRtt = std::min(m_minRtt, rttMs); |
| 53 | |
| 54 | m_maxRtt = std::max(m_maxRtt, rttMs); |
| 55 | |
| 56 | m_sumRtt += rttMs; |
| 57 | m_sumRttSquared += rttMs * rttMs; |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | StatisticsCollector::recordTimeout() |
| 62 | { |
| 63 | m_nSent++; |
| 64 | } |
| 65 | |
| 66 | Statistics |
| 67 | StatisticsCollector::computeStatistics() |
| 68 | { |
| 69 | Statistics statistics; |
| 70 | |
| 71 | statistics.prefix = m_options.prefix; |
| 72 | statistics.nSent = m_nSent; |
| 73 | statistics.nReceived = m_nReceived; |
| 74 | statistics.pingStartTime = m_pingStartTime; |
| 75 | statistics.minRtt = m_minRtt; |
| 76 | statistics.maxRtt = m_maxRtt; |
| 77 | statistics.packetLossRate = (double)(m_nSent - m_nReceived) / (double)m_nSent; |
| 78 | statistics.sumRtt = m_sumRtt; |
| 79 | statistics.avgRtt = m_sumRtt / m_nReceived; |
| 80 | statistics.stdDevRtt = std::sqrt((m_sumRttSquared / m_nReceived) - (statistics.avgRtt * statistics.avgRtt)); |
| 81 | |
| 82 | return statistics; |
| 83 | } |
| 84 | |
| 85 | std::ostream& |
Eric Newberry | dce5a53 | 2015-05-06 10:46:52 -0700 | [diff] [blame] | 86 | Statistics::printSummary(std::ostream& os) const |
| 87 | { |
| 88 | os << nReceived << "/" << nSent << " packets, " << packetLossRate * 100.0 |
| 89 | << "% loss, min/avg/max/mdev = " << minRtt << "/" << avgRtt << "/" << maxRtt << "/" |
| 90 | << stdDevRtt << " ms" << std::endl; |
| 91 | |
| 92 | return os; |
| 93 | } |
| 94 | |
| 95 | std::ostream& |
Eric Newberry | 4164b8e | 2015-04-23 17:29:18 -0700 | [diff] [blame] | 96 | operator<<(std::ostream& os, const Statistics& statistics) |
| 97 | { |
| 98 | os << "\n"; |
| 99 | os << "--- " << statistics.prefix <<" ping statistics ---\n"; |
| 100 | os << statistics.nSent << " packets transmitted"; |
| 101 | os << ", " << statistics.nReceived << " received"; |
| 102 | os << ", " << statistics.packetLossRate * 100.0 << "% packet loss"; |
| 103 | os << ", time " << statistics.sumRtt << " ms"; |
| 104 | if (statistics.nReceived > 0) { |
| 105 | os << "\n"; |
| 106 | os << "rtt min/avg/max/mdev = "; |
| 107 | os << statistics.minRtt << "/"; |
| 108 | os << statistics.avgRtt << "/"; |
| 109 | os << statistics.maxRtt << "/"; |
| 110 | os << statistics.stdDevRtt << " ms"; |
| 111 | } |
| 112 | |
| 113 | return os; |
| 114 | } |
| 115 | |
| 116 | } // namespace client |
| 117 | } // namespace ping |
| 118 | } // namespace ndn |