GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/utils/statistics.cpp Lines: 0 90 0.0 %
Date: 2024-02-02 08:47:34 Branches: 0 166 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
#include <iomanip>  // std::setprecision
19
#include "tsid/utils/statistics.hpp"
20
21
using std::map;
22
using std::ostringstream;
23
using std::string;
24
25
Statistics& getStatistics() {
26
  static Statistics s;
27
  return s;
28
}
29
30
Statistics::Statistics() : active(true) {
31
  records_of = new map<string, QuantityData>();
32
}
33
34
Statistics::~Statistics() { delete records_of; }
35
36
bool Statistics::quantity_exists(string name) {
37
  return (records_of->find(name) != records_of->end());
38
}
39
40
void Statistics::store(string name, const double& value) {
41
  if (!active) return;
42
43
  // Just works if not already present
44
  records_of->insert(make_pair(name, QuantityData()));
45
46
  QuantityData& quant_info = records_of->find(name)->second;
47
48
  quant_info.stops++;
49
50
  // Update last value
51
  quant_info.last = value;
52
53
  // Update min/max
54
  if (value >= quant_info.max) quant_info.max = value;
55
  if (value <= quant_info.min || quant_info.min == 0) quant_info.min = value;
56
57
  // Update total
58
  quant_info.total += value;
59
}
60
61
void Statistics::reset_all() {
62
  if (!active) return;
63
64
  map<string, QuantityData>::iterator it;
65
66
  for (it = records_of->begin(); it != records_of->end(); ++it) {
67
    reset(it->first);
68
  }
69
}
70
71
void Statistics::report_all(int precision, std::ostream& output) {
72
  if (!active) return;
73
74
  output
75
      << "\n*** STATISTICS (min - avg - max - last - nSamples - total) ***\n";
76
  map<string, QuantityData>::iterator it;
77
  for (it = records_of->begin(); it != records_of->end(); ++it) {
78
    if (it->second.stops > 0) report(it->first, precision, output);
79
  }
80
}
81
82
void Statistics::reset(string name) {
83
  if (!active) return;
84
85
  // Try to recover Quantity data
86
  if (!quantity_exists(name))
87
    throw StatisticsException("Quantity not initialized.");
88
89
  QuantityData& quant_info = records_of->find(name)->second;
90
91
  quant_info.total = 0;
92
  quant_info.min = 0;
93
  quant_info.max = 0;
94
  quant_info.last = 0;
95
  quant_info.stops = 0;
96
}
97
98
void Statistics::turn_on() {
99
  std::cout << "Statistics active." << std::endl;
100
  active = true;
101
}
102
103
void Statistics::turn_off() {
104
  std::cout << "Statistics inactive." << std::endl;
105
  active = false;
106
}
107
108
void Statistics::report(string name, int precision, std::ostream& output) {
109
  if (!active) return;
110
111
  // Try to recover Quantity data
112
  if (!quantity_exists(name))
113
    throw StatisticsException("Quantity not initialized.");
114
115
  QuantityData& quant_info = records_of->find(name)->second;
116
117
  string pad = "";
118
  for (std::string::size_type i = name.length(); i < STATISTICS_MAX_NAME_LENGTH;
119
       i++)
120
    pad.append(" ");
121
122
  output << name << pad;
123
  output << std::fixed << std::setprecision(precision) << (quant_info.min)
124
         << "\t";
125
  output << std::fixed << std::setprecision(precision)
126
         << (quant_info.total / (long double)quant_info.stops) << "\t";
127
  output << std::fixed << std::setprecision(precision) << (quant_info.max)
128
         << "\t";
129
  output << std::fixed << std::setprecision(precision) << (quant_info.last)
130
         << "\t";
131
  output << std::fixed << std::setprecision(precision) << quant_info.stops
132
         << "\t";
133
  output << std::fixed << std::setprecision(precision) << quant_info.total
134
         << std::endl;
135
}
136
137
long double Statistics::get_total(string name) {
138
  // Try to recover Quantity data
139
  if (!quantity_exists(name))
140
    throw StatisticsException("Quantity not initialized.");
141
142
  QuantityData& quant_info = records_of->find(name)->second;
143
144
  return quant_info.total;
145
}
146
147
long double Statistics::get_average(string name) {
148
  // Try to recover Quantity data
149
  if (!quantity_exists(name))
150
    throw StatisticsException("Quantity not initialized.");
151
152
  QuantityData& quant_info = records_of->find(name)->second;
153
154
  return (quant_info.total / (long double)quant_info.stops);
155
}
156
157
long double Statistics::get_min(string name) {
158
  // Try to recover Quantity data
159
  if (!quantity_exists(name))
160
    throw StatisticsException("Quantity not initialized.");
161
162
  QuantityData& quant_info = records_of->find(name)->second;
163
164
  return quant_info.min;
165
}
166
167
long double Statistics::get_max(string name) {
168
  // Try to recover Quantity data
169
  if (!quantity_exists(name))
170
    throw StatisticsException("Quantity not initialized.");
171
172
  QuantityData& quant_info = records_of->find(name)->second;
173
174
  return quant_info.max;
175
}
176
177
long double Statistics::get_last(string name) {
178
  // Try to recover Quantity data
179
  if (!quantity_exists(name))
180
    throw StatisticsException("Quantity not initialized.");
181
182
  QuantityData& quant_info = records_of->find(name)->second;
183
184
  return quant_info.last;
185
}