hpp-statistics: include/hpp/statistics/success-bin.hh Source File

hpp-statistics 9.0.2 Classes for doing statistics.

success-bin.hh
Go to the documentation of this file.
1 // Copyright (c) 2014, LAAS-CNRS
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4 // This file is part of hpp-statistics.
5 // hpp-statistics is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // hpp-statistics is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // hpp-statistics. If not, see <http://www.gnu.org/licenses/>.
16
17 #ifndef HPP_STATISTICS_SUCCESSBIN_HH
18 #define HPP_STATISTICS_SUCCESSBIN_HH
19
20 #include <hpp/util/debug.hh>
21 #include <iostream>
22 #include <set>
23
24 #include "hpp/statistics/bin.hh"
25 #include "hpp/statistics/config.hh"
26 #include "hpp/statistics/fwd.hh"
27
28 #define HPP_DEFINE_REASON_FAILURE(ID, STRING) \
29 const ::hpp::statistics::SuccessBin::Reason ID = \
30 ::hpp::statistics::SuccessBin::createReason(STRING); \
31 struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n
32
33 namespace hpp {
34 namespace statistics {
36 class HPP_STATISTICS_DLLAPI SuccessBin : public Bin {
37 public:
41 struct Reason {
42 std::size_t id;
43 std::string what;
44 Reason(std::size_t a_id, std::string a_what) : id(a_id), what(a_what) {}
45 };
46
48 const static Reason REASON_UNKNOWN;
49
51 SuccessBin(const bool success, const Reason& r = REASON_UNKNOWN)
52 : Bin(), success_(success), reason_(r) {
53 if (success_) reason_ = REASON_SUCCESS;
54 }
55
58 inline bool isSuccess() const { return success_; }
59
61 inline const Reason& reason() const { return reason_; }
62
64 inline const std::string& reasonString() const;
65
69 inline bool operator==(const SuccessBin& other) const {
70 return reason_.id == other.reason().id;
71 }
72
76 inline bool operator<(const SuccessBin& other) const {
77 return reason_.id < other.reason().id;
78 }
79
82 static Reason createReason(const std::string& what) {
83 return Reason(reasonID_last++, what);
84 }
85
86 private:
87 bool success_;
88 size_t freq_;
89 Reason reason_;
90
92 const static Reason REASON_SUCCESS;
93 static std::size_t reasonID_last;
94
95 inline std::ostream& printValue(std::ostream& os) const {
96 os << "Event ";
97 if (success_)
98 os << "'Success'";
99 else
100 os << "'Failure': " << reason_.what;
101 return os;
102 }
103 };
104
105 class HPP_STATISTICS_DLLAPI SuccessStatistics : public Statistics<SuccessBin> {
106 public:
107 typedef Statistics<SuccessBin> Parent;
108
110 SuccessStatistics(const std::string name = "",
111 const std::size_t& logRatio = 2)
112 : name_(name), logRatio_(logRatio) {}
113
115 SuccessStatistics(const SuccessStatistics& other)
116 : name_(other.name_), logRatio_(other.logRatio_) {}
117
119 void addSuccess() { insert(SuccessBin(true)); }
120
125 void addFailure(const SuccessBin::Reason& r = SuccessBin::REASON_UNKNOWN) {
126 insert(SuccessBin(false, r));
127 #ifdef HPP_DEBUG
128 isLowRatio(true);
129 #endif
130 }
131
132 inline bool isLowRatio(const bool autoPrint = false) const {
133 bool lowRatio = (logRatio_ * nbSuccess() < numberOfObservations());
134 if (autoPrint && lowRatio) hppDout(info, name_ << ":\n" << *this);
135 return lowRatio;
136 }
137
139 std::size_t nbSuccess() const { return freq(SuccessBin(true)); }
140
142 std::size_t nbFailure() const { return numberOfObservations() - nbSuccess(); }
143
145 std::size_t nbFailure(const SuccessBin::Reason& r) const {
146 return freq(SuccessBin(false, r));
147 }
148
149 std::string name_;
150
152 std::size_t logRatio_;
153 };
154 } // namespace statistics
155 } // namespace hpp
156
157 #endif // HPP_STATISTICS_SUCCESSBIN_HH
bin.hh
hpp::statistics::Bin
Definition: bin.hh:35
hpp::statistics::Statistics
Definition: bin.hh:75
hpp::statistics::SuccessBin
This class count the number of success and failure.
Definition: success-bin.hh:36
hpp::statistics::SuccessBin::isSuccess
bool isSuccess() const
Definition: success-bin.hh:58
hpp::statistics::SuccessBin::operator==
bool operator==(const SuccessBin &other) const
Definition: success-bin.hh:69
hpp::statistics::SuccessBin::SuccessBin
SuccessBin(const bool success, const Reason &r=REASON_UNKNOWN)
Constructor.
Definition: success-bin.hh:51
hpp::statistics::SuccessBin::REASON_UNKNOWN
static const Reason REASON_UNKNOWN
The default reason for 'failure'.
Definition: success-bin.hh:48
hpp::statistics::SuccessBin::reason
const Reason & reason() const
If this bin represents 'failure', returns the reason.
Definition: success-bin.hh:61
hpp::statistics::SuccessBin::operator<
bool operator<(const SuccessBin &other) const
Definition: success-bin.hh:76
hpp::statistics::SuccessBin::createReason
static Reason createReason(const std::string &what)
Definition: success-bin.hh:82
hpp::statistics::SuccessBin::reasonString
const std::string & reasonString() const
If this bin represents 'failure', returns the reason as a string.
hpp::statistics::SuccessStatistics
Definition: success-bin.hh:105
hpp::statistics::SuccessStatistics::logRatio_
std::size_t logRatio_
If nbSuccess() * logRatio < numberOfObservations(), write to log.
Definition: success-bin.hh:152
hpp::statistics::SuccessStatistics::Parent
Statistics< SuccessBin > Parent
Definition: success-bin.hh:107
hpp::statistics::SuccessStatistics::isLowRatio
bool isLowRatio(const bool autoPrint=false) const
Definition: success-bin.hh:132
hpp::statistics::SuccessStatistics::SuccessStatistics
SuccessStatistics(const std::string name="", const std::size_t &logRatio=2)
Constructor.
Definition: success-bin.hh:110
hpp::statistics::SuccessStatistics::nbSuccess
std::size_t nbSuccess() const
Count the number of success.
Definition: success-bin.hh:139
hpp::statistics::SuccessStatistics::addSuccess
void addSuccess()
Add a 'success'.
Definition: success-bin.hh:119
hpp::statistics::SuccessStatistics::addFailure
void addFailure(const SuccessBin::Reason &r=SuccessBin::REASON_UNKNOWN)
Definition: success-bin.hh:125
hpp::statistics::SuccessStatistics::name_
std::string name_
Definition: success-bin.hh:149
hpp::statistics::SuccessStatistics::nbFailure
std::size_t nbFailure() const
Count the number of failure, in total.
Definition: success-bin.hh:142
hpp::statistics::SuccessStatistics::SuccessStatistics
SuccessStatistics(const SuccessStatistics &other)
Copy Constructor.
Definition: success-bin.hh:115
hpp::statistics::SuccessStatistics::nbFailure
std::size_t nbFailure(const SuccessBin::Reason &r) const
Count the number of a particular failure.
Definition: success-bin.hh:145
config.hh
HPP_STATISTICS_DLLAPI
#define HPP_STATISTICS_DLLAPI
Definition: config.hh:88
debug.hh
fwd.hh
hppDout
#define hppDout(channel, data)
info
constexpr int info
hpp
Implementation.
hpp::statistics::SuccessBin::Reason
Definition: success-bin.hh:41
hpp::statistics::SuccessBin::Reason::what
std::string what
Definition: success-bin.hh:43
hpp::statistics::SuccessBin::Reason::Reason
Reason(std::size_t a_id, std::string a_what)
Definition: success-bin.hh:44
hpp::statistics::SuccessBin::Reason::id
std::size_t id
Definition: success-bin.hh:42