Remove dynamic exceptions
[anna.git] / include / anna / test / Statistic.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_test_Statistic_hpp
10 #define anna_test_Statistic_hpp
11
12 #include <anna/core/util/Average.hpp>
13 #include <anna/core/functions.hpp>
14 #include <anna/core/mt/Guard.hpp>
15 #include <anna/core/mt/Mutex.hpp>
16 #include <anna/core/RuntimeException.hpp>
17
18 namespace test {
19
20 using namespace anna;
21
22 class Statistic : public anna::Mutex {
23 public:
24    Statistic () :
25       a_messageCounter (0),
26       a_successCounter (0),
27       a_initTime (0),
28       a_avgDelay ("AvgDelay"),
29       a_errorCounter (0)
30    {}
31
32    void initTime () noexcept(false) {
33       if (a_initTime == 0) {
34          Guard guard (this, "Statistic::initTime");
35          if (a_initTime == 0)
36             a_initTime = anna::functions::millisecond ();
37       }
38    }
39
40    Millisecond getInitTime () const { return a_initTime; }
41    int getMessageCounter () const { return a_messageCounter; }
42    int getSuccessCounter () const { return a_successCounter; }
43
44    void countMessage () { a_messageCounter ++; }
45    void countSuccess () { a_successCounter ++; }
46    int countError () noexcept(false) {
47       Guard guard (this, "Statistic::countError");
48       return ++ a_errorCounter;
49    }
50    void resetError () noexcept(false) {
51       if (a_errorCounter > 0) {
52          Guard guard (this, "Statistic::countError");
53          a_errorCounter = 0;
54       }
55    }
56
57    void countDelay (const Millisecond value) noexcept(false) {
58       Guard guard (this, "Statistic::countDelay");
59       a_avgDelay += value;
60    }
61
62    const Average <Microsecond>& getAvgDelay () const { return a_avgDelay; }
63
64    int getMessage () const { return a_messageCounter; }
65
66 private:
67    Millisecond a_initTime;
68    int a_messageCounter;
69    int a_successCounter;
70    Average <Microsecond> a_avgDelay;
71    int a_errorCounter;
72
73 };
74
75 }
76
77 #endif
78