μHAL (v2.7.9)
Part of the IPbus software repository
TimeIntervalStats.cpp
Go to the documentation of this file.
1 
3 
4 
5 #include <boost/chrono/duration.hpp> // for operator/, operator<, operator>
6 #include <boost/chrono/time_point.hpp> // for operator-
7 #include <boost/ratio/ratio.hpp> // for ratio
8 #include <boost/ratio/ratio_fwd.hpp> // for milli
9 
10 #include <ostream> // for operator<<, ostream, basic_os...
11 
12 
13 namespace uhal {
14 
16  nMeasurements(0)
17 {
18 }
19 
20 
22 {
23 }
24 
25 
27 {
28  return nMeasurements;
29 }
30 
31 
32 const TimeIntervalStats::Clock_t::duration& TimeIntervalStats::min() const
33 {
34  return mMin;
35 }
36 
37 
38 const TimeIntervalStats::Clock_t::duration& TimeIntervalStats::max() const
39 {
40  return mMax;
41 }
42 
43 
44 TimeIntervalStats::Clock_t::duration TimeIntervalStats::mean() const
45 {
46  return mSum / nMeasurements;
47 }
48 
49 
50 const std::queue<TimeIntervalStats::Clock_t::duration>& TimeIntervalStats::getLatestMeasurements() const
51 {
52  return mLatestMeasurements;
53 }
54 
55 
56 void TimeIntervalStats::add(const Clock_t::time_point& aT1, const Clock_t::time_point& aT2)
57 {
58  const Clock_t::duration lInterval = aT2 - aT1;
59 
60  if ((nMeasurements == 0) or (lInterval < mMin))
61  mMin = lInterval;
62  if ((nMeasurements == 0) or (lInterval > mMax))
63  mMax = lInterval;
64 
65  mSum += lInterval;
66  nMeasurements += 1;
67 
68  mLatestMeasurements.push(lInterval);
69  if (mLatestMeasurements.size() > 5)
70  mLatestMeasurements.pop();
71 }
72 
73 
75 {
76  for (size_t i = 0; i < nMeasurements; i++)
77  mLatestMeasurements.pop();
78  nMeasurements = 0;
79 }
80 
81 
82 std::ostream& operator<<(std::ostream& aStream, const TimeIntervalStats& aStats)
83 {
84  if (aStats.size() == 0)
85  aStream << "no values recorded";
86  else {
87  typedef boost::chrono::duration<float, boost::milli> MilliSec_t;
88 
89  aStream << "min / mean / max = " << MilliSec_t(aStats.min()).count() << " / "
90  << MilliSec_t(aStats.mean()).count() << " / " << MilliSec_t(aStats.max()).count() << " ms";
91  std::queue<TimeIntervalStats::Clock_t::duration> lLatestMeasurements = aStats.getLatestMeasurements();
92  aStream << ", last values ";
93  while (lLatestMeasurements.size() > 0) {
94  aStream << MilliSec_t(lLatestMeasurements.front()).count() << (lLatestMeasurements.size() > 1 ? ", " : " ms");
95  lLatestMeasurements.pop();
96  }
97  }
98 
99  return aStream;
100 }
101 
102 
103 } // end ns uhal
uhal::operator<<
std::ostream & operator<<(std::ostream &aStr, const uhal::HttpResponseType &aHttpResponse)
Definition: HttpResponseGrammar.cpp:41
uhal::TimeIntervalStats::getLatestMeasurements
const std::queue< Clock_t::duration > & getLatestMeasurements() const
Definition: TimeIntervalStats.cpp:50
uhal::TimeIntervalStats::nMeasurements
size_t nMeasurements
Definition: TimeIntervalStats.hpp:43
uhal::TimeIntervalStats::mMax
Clock_t::duration mMax
Definition: TimeIntervalStats.hpp:40
uhal::TimeIntervalStats::mLatestMeasurements
std::queue< Clock_t::duration > mLatestMeasurements
Definition: TimeIntervalStats.hpp:37
uhal::TimeIntervalStats::add
void add(const Clock_t::time_point &aT1, const Clock_t::time_point &aT2)
Definition: TimeIntervalStats.cpp:56
TimeIntervalStats.hpp
uhal::TimeIntervalStats::mMin
Clock_t::duration mMin
Definition: TimeIntervalStats.hpp:39
uhal::TimeIntervalStats::size
size_t size() const
Definition: TimeIntervalStats.cpp:26
uhal
Definition: HttpResponseGrammar.hpp:49
uhal::TimeIntervalStats::mSum
Clock_t::duration mSum
Definition: TimeIntervalStats.hpp:41
uhal::TimeIntervalStats::clear
void clear()
Definition: TimeIntervalStats.cpp:74
uhal::TimeIntervalStats::mean
Clock_t::duration mean() const
Definition: TimeIntervalStats.cpp:44
uhal::TimeIntervalStats::~TimeIntervalStats
~TimeIntervalStats()
Definition: TimeIntervalStats.cpp:21
uhal::TimeIntervalStats
Definition: TimeIntervalStats.hpp:15
uhal::TimeIntervalStats::min
const Clock_t::duration & min() const
Definition: TimeIntervalStats.cpp:32
uhal::TimeIntervalStats::max
const Clock_t::duration & max() const
Definition: TimeIntervalStats.cpp:38
uhal::TimeIntervalStats::TimeIntervalStats
TimeIntervalStats()
Definition: TimeIntervalStats.cpp:15