GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/tsid/utils/statistics.hpp Lines: 0 2 0.0 %
Date: 2024-02-02 08:47:34 Branches: 0 0 - %

Line Branch Exec Source
1
//
2
// Copyright (c) 2017 CNRS
3
//
4
// This file is part of tsid
5
// tsid 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
// tsid is distributed in the hope that it will be
10
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
11
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
// General Lesser Public License for more details. You should have
13
// received a copy of the GNU Lesser General Public License along with
14
// tsid If not, see
15
// <http://www.gnu.org/licenses/>.
16
//
17
18
#ifndef __invdyn_statistics_H__
19
#define __invdyn_statistics_H__
20
21
#include <iostream>
22
#include <map>
23
#include <sstream>
24
25
#define STATISTICS_MAX_NAME_LENGTH 60
26
27
// Generic statistics exception class
28
struct StatisticsException {
29
 public:
30
  StatisticsException(std::string error) : error(error) {}
31
  std::string error;
32
};
33
34
/**
35
    @brief A class to compute statistics about quantities of interest.
36
37
    @code
38
    Statistics stat();
39
    @endcode
40
41
    The Statistics class can be used to keep track of the minimum,
42
    maximum, average of quantities of interest.
43
44
    To report the results just call:
45
46
    @code
47
    stat.report("Code ID");
48
    @endcode
49
50
    Thou can also provide an additional std::ostream& parameter to report() to
51
    redirect the logging on a different output. Also, you can use the
52
    get_total/min/max/average() methods to get the individual numeric data,
53
    without all the details of the logging. You can also extend Statistics to
54
    implement your own logging syntax.
55
56
    To report all the measurements:
57
58
    @code
59
    stat.report_all();
60
    @endcode
61
62
    Same as above, you can redirect the output by providing a std::ostream&
63
    parameter.
64
65
*/
66
class Statistics {
67
 public:
68
  /** Constructor */
69
  Statistics();
70
71
  /** Destructor */
72
  ~Statistics();
73
74
  /** Tells if a quantity with a certain name exists */
75
  bool quantity_exists(std::string name);
76
77
  /** Record the value of the specified quantity */
78
  void store(std::string name, const double& value);
79
80
  /** Reset a certain quantity record */
81
  void reset(std::string name);
82
83
  /** Resets all the quantity records */
84
  void reset_all();
85
86
  /** Dump the data of a certain quantity record */
87
  void report(std::string name, int precision = 2,
88
              std::ostream& output = std::cout);
89
90
  /** Dump the data of all the quantity records */
91
  void report_all(int precision = 2, std::ostream& output = std::cout);
92
93
  /** Returns total execution of a certain quantity */
94
  long double get_total(std::string name);
95
96
  /** Returns average execution of a certain quantity */
97
  long double get_average(std::string name);
98
99
  /** Returns minimum execution of a certain quantity */
100
  long double get_min(std::string name);
101
102
  /** Returns maximum execution of a certain quantity */
103
  long double get_max(std::string name);
104
105
  /** Return last measurement of a certain quantity */
106
  long double get_last(std::string name);
107
108
  /**	Turn off statistics, all the Statistics::* methods return without doing
109
        anything after this method is called. */
110
  void turn_off();
111
112
  /** Turn on statistics, restore operativity after a turn_off(). */
113
  void turn_on();
114
115
 protected:
116
  /** Struct to hold the quantity data */
117
  struct QuantityData {
118
    QuantityData() : total(0), min(0), max(0), last(0), stops(0) {}
119
120
    /** Cumulative total value */
121
    long double total;
122
123
    /** Minimum value */
124
    long double min;
125
126
    /** Maximum value */
127
    long double max;
128
129
    /** Last value */
130
    long double last;
131
132
    /** How many times have this quantity been stored? */
133
    int stops;
134
  };
135
136
  /** Flag to hold the statistics status */
137
  bool active;
138
139
  /** Pointer to the dynamic structure which holds the collection of quantity
140
      data */
141
  std::map<std::string, QuantityData>* records_of;
142
};
143
144
Statistics& getStatistics();
145
146
#endif