MetroCollect  2.3.4
MetroCollectFile.cc
Go to the documentation of this file.
1 //
2 // MetroCollectStats.cc
3 //
4 // Created on July 26th 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #include <fstream>
22 #include <iostream>
23 #include <iomanip>
24 #include <sstream>
25 
27 
28 
40 template<typename T, typename S, typename U>
41 void printMetricsValues(const std::vector<std::pair<std::string, std::string>>& names, const MetroCollect::MetricsArray<T>& m1, const MetroCollect::MetricsArray<S>& m2, const MetroCollect::MetricsArray<U>& d) {
42  std::cout << std::setw(54) << std::left << "" << std::setw(16) << "First value" << "\t" << std::setw(16) << "Second value" << "\t" << std::setw(16) << "Evolution" << "Unit" << std::endl;
43  for (size_t i = 0; i < d.size(); i++)
44  std::cout << std::setw(54) << std::left << names[i].first << std::setw(16) << m1[i] << "\t" << std::setw(16) << m2[i] << "\t" << std::setw(16) << d[i] << names[i].second << std::endl;
45  std::cout << std::endl << std::endl;
46 }
47 
48 
55 void printMetricsStats(const std::vector<std::pair<std::string, std::string>>& names, const MetroCollect::MetricsController::MetricsStats& s) {
56  std::cout << std::setw(54) << " ";
57  for (const auto& st : MetroCollect::Statistics::names)
58  std::cout << std::setw(16) << std::left << st << "\t";
59  std::cout << "Unit" << std::endl;
60  for (size_t i = 0; i < s.min.size(); i++) {
61  std::cout << std::setw(54) << std::left << names[i].first;
62  s.forEach([&](const auto& st, MetroCollect::Statistics::Stats ) {
63  std::cout << std::setw(16) << st[i] << "\t";
64  });
65  std::cout << names[i].second << std::endl;
66  }
67  std::cout << std::endl << std::endl;
68 }
69 
70 
79  // (*interests)[0] = true;
80  // auto [begin, end] = request.metricsSliceForSource(0);
81  // for (auto itr = begin; itr < end; itr++)
82  for (auto itr = request.begin(); itr < request.end(); itr++)
84  return request;
85 }
86 
87 
95 template<typename T>
97  std::vector<MetroCollect::MetricsSource::FieldInfo> fields(metricsArray.fieldCount());
98  for (size_t i = 0; i < fields.size(); i++)
99  fields[i] = metricsArray.fieldInfoAtIndex(i);
100 
101  std::vector<std::pair<std::string, std::string>> names;
102  names.reserve(fields.size());
103  std::stringstream ss;
104  for (const auto& field : fields) {
105  ss.str(std::string());
106  for (const auto& part : field.name)
107  ss << "/" << part;
108  names.push_back(std::pair{ss.str(), field.unit});
109  }
110  return names;
111 }
112 
113 
118  size_t i = 0;
119  size_t iterationCount;
120  std::vector<std::pair<std::chrono::duration<double, std::milli>, std::vector<MetroCollect::DiffValueType>>> values;
121  std::vector<std::pair<std::string, std::string>> names;
122  std::chrono::time_point<std::chrono::system_clock> start;
123 
130  void set(size_t iterations, std::vector<std::pair<std::string, std::string>> metricsNames) {
131  this->iterationCount = iterations;
132  this->names = metricsNames;
133  this->values.clear();
134  this->values.resize(iterations, {std::chrono::duration<double, std::milli>::zero(), std::vector<MetroCollect::DiffValueType>(metricsNames.size())});
135  this->start = std::chrono::system_clock::now();
136  }
137 
139  this->values[i].first = diff.endTime() - this->start;
140  std::copy(diff.begin(), diff.end(), this->values[i].second.begin());
141  i++;
142  }
143 
145  }
146 
148  iterationCount--;
149  return iterationCount == 0;
150  }
151 };
152 
153 
154 
162 int main(int argc, char* argv[]) {
164  size_t iterations = 10;
165  const char* file = "output.csv";
167 
168  if (argc > 1)
169  samplingInterval = std::chrono::milliseconds(std::atol(argv[1]));
170  if (argc > 2)
171  iterations = std::atol(argv[2]);
172  if (argc > 3)
173  file = argv[3];
174 
175  auto m = generateMetrics();
176  d.set(iterations, getAllFieldNames(m));
177  MetroCollect::MetricsController controller(d);
178  controller.setSendValues(true);
179  controller.setSamplingInterval(samplingInterval);
180  controller.setProcessingWindow(1, 1);
181  controller.setConvertToUnitsPerSeconds(false);
182  controller.setRequestedMetrics(m);
183 
184  controller.collectMetrics();
185 
186  std::ofstream output;
187  output.open(file);
188 
189  output << "name,unit";
190  for (size_t j = 0; j < iterations; j++)
191  output << "," << d.values[j].first.count();
192  output << "\n";
193  for (size_t i = 0; i < d.names.size(); i++) {
194  output << d.names[i].first << "," << d.names[i].second;
195  for (size_t j = 0; j < iterations; j++)
196  output << "," << d.values[j].second[i];
197  output << "\n";
198  }
199 
200  output.close();
201  return 0;
202 }
static constexpr std::chrono::milliseconds defaultSamplingInterval
Default parameter option.
Struct to store all final metric stats.
void setRequestedMetrics(const MetricsArray< Statistics::Stats > &requestedMetrics) noexcept
Sets the list of request metrics.
Iterator begin() noexcept
Returns an iterator to the first element of the container.
void metricsContollerCollectedMetricsStats(const MetroCollect::MetricsController &, const MetroCollect::MetricsController::MetricsStats &) override
Function called every time the MetricsController has computed statistics.
Small struct to implement MetricsController delegate.
auto getAllFieldNames(const MetroCollect::MetricsArray< T > &metricsArray)
Get fields name of all metrics.
void metricsContollerCollectedMetricsValues(const MetroCollect::MetricsController &, const MetroCollect::MetricsDiffArray &diff, const MetroCollect::MetricsDataArray &, const MetroCollect::MetricsDataArray &) override
Function called every time the MetricsController has collected new values.
Generic class to store and manage metrics.
Definition: MetricsArray.h:40
Stats
Bitwise type te select statistics.
Definition: Statistics.h:64
void setSamplingInterval(std::chrono::milliseconds interval) noexcept
Sets the metrics sampling interval.
const std::chrono::system_clock::time_point & endTime() const noexcept
Get time of latest metrics.
std::vector< std::pair< std::chrono::duration< double, std::milli >, std::vector< MetroCollect::DiffValueType > > > values
Array of all collected metrics and their relative timestamps.
void collectMetrics()
Launch metric collection loop.
void setConvertToUnitsPerSeconds(bool convertToUnitsPerSecond) noexcept
Sets whether to convert metric differences to units per second.
All statistics selected.
Definition: Statistics.h:71
void printMetricsStats(const std::vector< std::pair< std::string, std::string >> &names, const MetroCollect::MetricsController::MetricsStats &s)
Print metric stats on screen.
std::chrono::time_point< std::chrono::system_clock > start
Start time of metric collection.
auto generateMetrics()
Generate metric request to give to the MetricsController.
size_t iterationCount
Number of iterations remaining to be done.
SourceInterests makeSourceInterests(bool value)
Construct a new shared pointer to a SourceInterests object.
void forEach(Function func)
Execute a function for each stats array.
bool metricsContollerShouldStopCollectingMetrics(const MetroCollect::MetricsController &) override
Function called after each iteration to ask the delegate whether the controller should stop collectin...
void set(size_t iterations, std::vector< std::pair< std::string, std::string >> metricsNames)
Update members.
void setProcessingWindow(size_t length, size_t overlap) noexcept
Sets the processing window parameters used to compute statistics.
const MetricsSource::FieldInfo fieldInfoAtIndex(size_t index) const noexcept
Get details about a specific metric field.
Class to fetch metrics from the kernel and store them.
void setSendValues(bool sendValues) noexcept
Sets whether the controller alerts the delegate after collecting new values.
Iterator end() noexcept
Returns an iterator to the element following the last element of the container.
size_t i
Iteration index.
void printMetricsValues(const std::vector< std::pair< std::string, std::string >> &names, const MetroCollect::MetricsArray< T > &m1, const MetroCollect::MetricsArray< S > &m2, const MetroCollect::MetricsArray< U > &d)
Print metrics on screen.
static constexpr std::array< std::string_view, count > names
Names of available statistics.
Definition: Statistics.h:58
int main(int argc, char *argv[])
MetroCollectFile main function.
Abstract delegate of MetricsController.
size_t fieldCount() const noexcept
Returns the total number of available metrics.
Definition: MetricsArray.cc:74
MetricsStatsArray< DiffValueType > min
Array of minimums.
std::vector< std::pair< std::string, std::string > > names
Name of collected metrics.
Class to control the collection of metrics.
Class to compute and store metric values variations.
size_t size() const noexcept
Returns the size of the underlying array.