artdaq_utilities  v1_05_00
MetricPlugin_t.cc
1 #include "artdaq-utilities/Plugins/MetricPlugin.hh"
2 #include "artdaq-utilities/Plugins/TestMetric.hh"
3 
4 #define BOOST_TEST_MODULES MetricPlugin_t
5 #include "cetlib/quiet_unit_test.hpp"
6 #include "cetlib_except/exception.h"
7 #include "fhiclcpp/make_ParameterSet.h"
8 
9 #include "trace.h"
10 
11 namespace artdaqtest {
13 {
14 public:
15  explicit MetricPluginTestAdapter(fhicl::ParameterSet ps)
16  : artdaq::MetricPlugin(ps, "MetricPlugin_t")
17  , sendMetric_string_calls(0)
18  , sendMetric_int_calls(0)
19  , sendMetric_double_calls(0)
20  , sendMetric_float_calls(0)
21  , sendMetric_unsigned_calls(0)
22  , startMetrics_calls(0)
23  , stopMetrics_calls(0)
24  {}
25 
26  virtual void sendMetric_(const std::string&, const std::string&, const std::string&) override { sendMetric_string_calls++; }
27  virtual void sendMetric_(const std::string&, const int&, const std::string&) override { sendMetric_int_calls++; }
28  virtual void sendMetric_(const std::string&, const double&, const std::string&) override { sendMetric_double_calls++; }
29  virtual void sendMetric_(const std::string&, const float&, const std::string&) override { sendMetric_float_calls++; }
30  virtual void sendMetric_(const std::string&, const long unsigned int&, const std::string&) override { sendMetric_unsigned_calls++; }
31 
32  virtual void startMetrics_() override { startMetrics_calls++; }
33  virtual void stopMetrics_() override { stopMetrics_calls++; }
34 
35  size_t sendMetric_string_calls;
36  size_t sendMetric_int_calls;
37  size_t sendMetric_double_calls;
38  size_t sendMetric_float_calls;
39  size_t sendMetric_unsigned_calls;
40  size_t startMetrics_calls;
41  size_t stopMetrics_calls;
42 
43  // Getters for protected members
44  fhicl::ParameterSet get_pset() { return pset; }
45  double get_accumulationTime_() { return accumulationTime_; }
46  std::string get_app_name_() { return app_name_; }
47  bool get_inhibit_() { return inhibit_; }
48  std::bitset<64> get_level_mask_() { return level_mask_; }
49 };
50 } // namespace artdaqtest
51 
52 BOOST_AUTO_TEST_SUITE(MetricPlugin_test)
53 
54 BOOST_AUTO_TEST_CASE(Constructor)
55 {
56  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case Constructor BEGIN";
57  std::string testConfig = "reporting_interval: 0 level: 4 metric_levels: [7,9,11] level_string: \"13-15,17,19-21,7-9\"";
58  fhicl::ParameterSet pset;
59  fhicl::make_ParameterSet(testConfig, pset);
61  BOOST_REQUIRE_EQUAL(mpta.get_pset().to_string(), pset.to_string());
62  BOOST_REQUIRE_EQUAL(mpta.get_accumulationTime_(), 0.0);
63  BOOST_REQUIRE_EQUAL(mpta.get_level_mask_()[8], true);
64  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(8), true);
65  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case Constructor END";
66 }
67 
68 BOOST_AUTO_TEST_CASE(IsLevelEnabled)
69 {
70  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case IsLevelEnabled BEGIN";
71  std::string testConfig = "reporting_interval: 0 level: 4 metric_levels: [7,9,11] level_string: \"13-15,17,19-21,7-9\"";
72  fhicl::ParameterSet pset;
73  fhicl::make_ParameterSet(testConfig, pset);
75 
76  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(0), true);
77  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(1), true);
78  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(2), true);
79  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(3), true);
80  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(4), true);
81  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(5), false);
82  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(6), false);
83  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(7), true);
84  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(8), true);
85  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(9), true);
86  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(10), false);
87  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(11), true);
88  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(12), false);
89  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(13), true);
90  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(14), true);
91  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(15), true);
92  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(16), false);
93  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(17), true);
94  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(18), false);
95  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(19), true);
96  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(20), true);
97  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(21), true);
98  BOOST_REQUIRE_EQUAL(mpta.IsLevelEnabled(22), false);
99 
100  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case IsLevelEnabled END";
101 }
102 
103 BOOST_AUTO_TEST_CASE(LibraryName)
104 {
105  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case LibraryName BEGIN";
106  std::string testConfig = "reporting_interval: 0 level: 4 metric_levels: [7,9,11] level_string: \"13-15,17,19-21,7-9\"";
107  fhicl::ParameterSet pset;
108  fhicl::make_ParameterSet(testConfig, pset);
110 
111  BOOST_REQUIRE_EQUAL(mpta.getLibName(), "ERROR");
112  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case LibraryName END";
113 }
114 
115 BOOST_AUTO_TEST_CASE(AddMetricData)
116 {
117  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case AddMetricData BEGIN";
118  std::string testConfig = "reporting_interval: 1.0 level: 4 metric_levels: [7,9,11] level_string: \"13-15,17,19-21,7-9\"";
119  fhicl::ParameterSet pset;
120  fhicl::make_ParameterSet(testConfig, pset);
122 
123  auto smd = std::make_unique<artdaq::MetricData>("String Metric", "Test Value", "Units", 1, artdaq::MetricMode::LastPoint, "", false);
124  auto imd = std::make_unique<artdaq::MetricData>("Int Metric", 2, "Units", 1, artdaq::MetricMode::LastPoint, "", false);
125  auto fmd = std::make_unique<artdaq::MetricData>("Float Metric", 3.5f, "Units", 1, artdaq::MetricMode::LastPoint, "", false);
126  auto dmd = std::make_unique<artdaq::MetricData>("Double Metric", 4.5, "Units", 1, artdaq::MetricMode::LastPoint, "", false);
127  auto umd = std::make_unique<artdaq::MetricData>("Unsigned Metric", 5UL, "Units", 1, artdaq::MetricMode::LastPoint, "", false);
128 
129  mpta.addMetricData(smd);
130  mpta.addMetricData(imd);
131  mpta.addMetricData(fmd);
132  mpta.addMetricData(dmd);
133  mpta.addMetricData(umd);
134 
135  BOOST_REQUIRE_EQUAL(mpta.sendMetric_string_calls, 1);
136  BOOST_REQUIRE_EQUAL(mpta.sendMetric_int_calls, 0);
137  BOOST_REQUIRE_EQUAL(mpta.sendMetric_float_calls, 0);
138  BOOST_REQUIRE_EQUAL(mpta.sendMetric_double_calls, 0);
139  BOOST_REQUIRE_EQUAL(mpta.sendMetric_unsigned_calls, 0);
140 
141  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case AddMetricData END";
142 }
143 
144 BOOST_AUTO_TEST_CASE(SendMetrics)
145 {
146  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case SendMetrics BEGIN";
147  std::string testConfig = "reporting_interval: 0.01 level: 4 metric_levels: [7,9,11] level_string: \"13-15,17,19-21,7-9\"";
148  fhicl::ParameterSet pset;
149  fhicl::make_ParameterSet(testConfig, pset);
151 
152  auto smd = std::make_unique<artdaq::MetricData>("String Metric", "Test Value", "Units", 1, artdaq::MetricMode::LastPoint, "", false);
153  auto imd = std::make_unique<artdaq::MetricData>("Int Metric", 2, "Units", 1, artdaq::MetricMode::LastPoint, "", false);
154  auto fmd = std::make_unique<artdaq::MetricData>("Float Metric", 3.5f, "Units", 1, artdaq::MetricMode::LastPoint, "", false);
155  auto dmd = std::make_unique<artdaq::MetricData>("Double Metric", 4.5, "Units", 1, artdaq::MetricMode::LastPoint, "", false);
156  auto umd = std::make_unique<artdaq::MetricData>("Unsigned Metric", 5UL, "Units", 1, artdaq::MetricMode::LastPoint, "", false);
157 
158  mpta.addMetricData(smd);
159  mpta.addMetricData(imd);
160  mpta.addMetricData(fmd);
161  mpta.addMetricData(dmd);
162  mpta.addMetricData(umd);
163 
164  BOOST_REQUIRE_EQUAL(mpta.sendMetric_string_calls, 1);
165  BOOST_REQUIRE_EQUAL(mpta.sendMetric_int_calls, 0);
166  BOOST_REQUIRE_EQUAL(mpta.sendMetric_float_calls, 0);
167  BOOST_REQUIRE_EQUAL(mpta.sendMetric_double_calls, 0);
168  BOOST_REQUIRE_EQUAL(mpta.sendMetric_unsigned_calls, 0);
169 
170  mpta.sendMetrics();
171 
172  BOOST_REQUIRE_EQUAL(mpta.sendMetric_string_calls, 1);
173  BOOST_REQUIRE_EQUAL(mpta.sendMetric_int_calls, 1);
174  BOOST_REQUIRE_EQUAL(mpta.sendMetric_float_calls, 1);
175  BOOST_REQUIRE_EQUAL(mpta.sendMetric_double_calls, 1);
176  BOOST_REQUIRE_EQUAL(mpta.sendMetric_unsigned_calls, 1);
177 
178  mpta.addMetricData(smd);
179  mpta.addMetricData(imd);
180  mpta.addMetricData(fmd);
181  mpta.addMetricData(dmd);
182  mpta.addMetricData(umd);
183 
184  BOOST_REQUIRE_EQUAL(mpta.sendMetric_string_calls, 2);
185  BOOST_REQUIRE_EQUAL(mpta.sendMetric_int_calls, 1);
186  BOOST_REQUIRE_EQUAL(mpta.sendMetric_float_calls, 1);
187  BOOST_REQUIRE_EQUAL(mpta.sendMetric_double_calls, 1);
188  BOOST_REQUIRE_EQUAL(mpta.sendMetric_unsigned_calls, 1);
189 
190  mpta.sendMetrics();
191 
192  BOOST_REQUIRE_EQUAL(mpta.sendMetric_string_calls, 2);
193  BOOST_REQUIRE_EQUAL(mpta.sendMetric_int_calls, 1);
194  BOOST_REQUIRE_EQUAL(mpta.sendMetric_float_calls, 1);
195  BOOST_REQUIRE_EQUAL(mpta.sendMetric_double_calls, 1);
196  BOOST_REQUIRE_EQUAL(mpta.sendMetric_unsigned_calls, 1);
197 
198  usleep(200000);
199 
200  mpta.sendMetrics();
201 
202  BOOST_REQUIRE_EQUAL(mpta.sendMetric_string_calls, 2);
203  BOOST_REQUIRE_EQUAL(mpta.sendMetric_int_calls, 2);
204  BOOST_REQUIRE_EQUAL(mpta.sendMetric_float_calls, 2);
205  BOOST_REQUIRE_EQUAL(mpta.sendMetric_double_calls, 2);
206  BOOST_REQUIRE_EQUAL(mpta.sendMetric_unsigned_calls, 2);
207 
208  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case SendMetrics END";
209 }
210 
211 BOOST_AUTO_TEST_CASE(StartMetrics)
212 {
213  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case StartMetrics BEGIN";
214  std::string testConfig = "reporting_interval: 0 level: 4 metric_levels: [7,9,11] level_string: \"13-15,17,19-21,7-9\"";
215  fhicl::ParameterSet pset;
216  fhicl::make_ParameterSet(testConfig, pset);
218 
219  mpta.startMetrics();
220  BOOST_REQUIRE_EQUAL(mpta.startMetrics_calls, 1);
221  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case StartMetrics END";
222 }
223 
224 BOOST_AUTO_TEST_CASE(StopMetrics)
225 {
226  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case StopMetrics BEGIN";
227  std::string testConfig = "reporting_interval: 1.0 level: 4 metric_levels: [7,9,11] level_string: \"13-15,17,19-21,7-9\"";
228  fhicl::ParameterSet pset;
229  fhicl::make_ParameterSet(testConfig, pset);
231 
232  auto smd = std::make_unique<artdaq::MetricData>("String Metric", "Test Value", "Units", 1, artdaq::MetricMode::LastPoint, "", false);
233  auto imd = std::make_unique<artdaq::MetricData>("Int Metric", 2, "Units", 1, artdaq::MetricMode::LastPoint, "", false);
234  auto fmd = std::make_unique<artdaq::MetricData>("Float Metric", 3.5f, "Units", 1, artdaq::MetricMode::LastPoint, "", false);
235  auto dmd = std::make_unique<artdaq::MetricData>("Double Metric", 4.5, "Units", 1, artdaq::MetricMode::LastPoint, "", false);
236  auto umd = std::make_unique<artdaq::MetricData>("Unsigned Metric", 5UL, "Units", 1, artdaq::MetricMode::LastPoint, "", false);
237 
238  mpta.addMetricData(smd);
239  mpta.addMetricData(imd);
240  mpta.addMetricData(fmd);
241  mpta.addMetricData(dmd);
242  mpta.addMetricData(umd);
243 
244  BOOST_REQUIRE_EQUAL(mpta.sendMetric_string_calls, 1);
245  BOOST_REQUIRE_EQUAL(mpta.sendMetric_int_calls, 0);
246  BOOST_REQUIRE_EQUAL(mpta.sendMetric_float_calls, 0);
247  BOOST_REQUIRE_EQUAL(mpta.sendMetric_double_calls, 0);
248  BOOST_REQUIRE_EQUAL(mpta.sendMetric_unsigned_calls, 0);
249 
250  mpta.stopMetrics();
251 
252  BOOST_REQUIRE_EQUAL(mpta.stopMetrics_calls, 1);
253  BOOST_REQUIRE_EQUAL(mpta.sendMetric_string_calls, 1);
254  BOOST_REQUIRE_EQUAL(mpta.sendMetric_int_calls, 2);
255  BOOST_REQUIRE_EQUAL(mpta.sendMetric_float_calls, 2);
256  BOOST_REQUIRE_EQUAL(mpta.sendMetric_double_calls, 2);
257  BOOST_REQUIRE_EQUAL(mpta.sendMetric_unsigned_calls, 2);
258 
259  TLOG(TLVL_INFO, "MetricPlugin_t") << "Test Case StopMetrics END";
260 }
261 
262 BOOST_AUTO_TEST_SUITE_END()
virtual void sendMetric_(const std::string &, const float &, const std::string &) override
Send a metric to the underlying metric storage (file, Graphite, Ganglia, etc.)
The MetricPlugin class defines the interface that MetricManager uses to send metric data to the vario...
Definition: MetricPlugin.hh:29
virtual void sendMetric_(const std::string &, const std::string &, const std::string &) override
Send a metric to the underlying metric storage (file, Graphite, Ganglia, etc.)
virtual void sendMetric_(const std::string &, const int &, const std::string &) override
Send a metric to the underlying metric storage (file, Graphite, Ganglia, etc.)
fhicl::ParameterSet pset
The ParameterSet used to configure the MetricPlugin.
virtual void sendMetric_(const std::string &, const long unsigned int &, const std::string &) override
Send a metric to the underlying metric storage (file, Graphite, Ganglia, etc.)
virtual void startMetrics_() override
Perform any start-up actions necessary for the metric plugin.
std::string app_name_
Name of the application which is sending metrics to this plugin.
Report only the last value recorded. Useful for event counters, run numbers, etc. ...
virtual void sendMetric_(const std::string &, const double &, const std::string &) override
Send a metric to the underlying metric storage (file, Graphite, Ganglia, etc.)
std::bitset< 64 > level_mask_
Bitset indicating for each possible metric level, whether this plugin will receive those metrics...
virtual void stopMetrics_() override
Perform any shutdown actions necessary for the metric plugin.
double accumulationTime_
The amount of time to average metric values; except for accumulate=false metrics, will be the interva...
bool inhibit_
Flag to indicate that the MetricPlugin is being stopped, and any metric back-ends which do not have a...