hpp-statistics 6.0.0
Classes for doing statistics.
Loading...
Searching...
No Matches
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"
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
33namespace hpp {
34namespace statistics {
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
106 public:
108
110 SuccessStatistics(const std::string name = "",
111 const std::size_t& logRatio = 2)
112 : name_(name), logRatio_(logRatio) {}
113
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
Definition bin.hh:35
Definition bin.hh:75
This class count the number of success and failure.
Definition success-bin.hh:36
bool isSuccess() const
Definition success-bin.hh:58
bool operator==(const SuccessBin &other) const
Definition success-bin.hh:69
SuccessBin(const bool success, const Reason &r=REASON_UNKNOWN)
Constructor.
Definition success-bin.hh:51
static const Reason REASON_UNKNOWN
The default reason for 'failure'.
Definition success-bin.hh:48
bool operator<(const SuccessBin &other) const
Definition success-bin.hh:76
static Reason createReason(const std::string &what)
Definition success-bin.hh:82
const Reason & reason() const
If this bin represents 'failure', returns the reason.
Definition success-bin.hh:61
const std::string & reasonString() const
If this bin represents 'failure', returns the reason as a string.
Definition success-bin.hh:105
std::size_t logRatio_
If nbSuccess() * logRatio < numberOfObservations(), write to log.
Definition success-bin.hh:152
Statistics< SuccessBin > Parent
Definition success-bin.hh:107
bool isLowRatio(const bool autoPrint=false) const
Definition success-bin.hh:132
SuccessStatistics(const std::string name="", const std::size_t &logRatio=2)
Constructor.
Definition success-bin.hh:110
std::size_t nbSuccess() const
Count the number of success.
Definition success-bin.hh:139
void addSuccess()
Add a 'success'.
Definition success-bin.hh:119
void addFailure(const SuccessBin::Reason &r=SuccessBin::REASON_UNKNOWN)
Definition success-bin.hh:125
std::string name_
Definition success-bin.hh:149
std::size_t nbFailure() const
Count the number of failure, in total.
Definition success-bin.hh:142
SuccessStatistics(const SuccessStatistics &other)
Copy Constructor.
Definition success-bin.hh:115
std::size_t nbFailure(const SuccessBin::Reason &r) const
Count the number of a particular failure.
Definition success-bin.hh:145
#define HPP_STATISTICS_DLLAPI
Definition config.hh:88
Implementation.
Definition main.hh:17
Definition success-bin.hh:41
std::string what
Definition success-bin.hh:43
Reason(std::size_t a_id, std::string a_what)
Definition success-bin.hh:44
std::size_t id
Definition success-bin.hh:42