1 #include "artdaq-utilities/Plugins/MetricManager.hh"
2 #include "artdaq-utilities/Plugins/TestMetric.hh"
4 #define BOOST_TEST_MODULES MetricManager_t
5 #include "cetlib/quiet_unit_test.hpp"
6 #include "cetlib_except/exception.h"
7 #include "fhiclcpp/make_ParameterSet.h"
11 BOOST_AUTO_TEST_SUITE(MetricManager_test)
13 typedef std::chrono::duration<
double, std::ratio<1>> seconds;
15 constexpr
double GetElapsedTime(std::chrono::steady_clock::time_point then,
16 std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now())
18 return std::chrono::duration_cast<seconds>(now - then).count();
21 BOOST_AUTO_TEST_CASE(Construct)
23 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Construct" << TLOG_ENDL;
26 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
27 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
30 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Construct" << TLOG_ENDL;
33 BOOST_AUTO_TEST_CASE(Initialize)
35 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Initialize" << TLOG_ENDL;
38 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
39 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
43 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 1.0}";
44 fhicl::ParameterSet pset;
45 fhicl::make_ParameterSet(testConfig, pset);
49 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
50 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
53 BOOST_REQUIRE_EQUAL(mm.
Running(),
true);
54 BOOST_REQUIRE_EQUAL(mm.
Active(),
true);
55 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Initialize" << TLOG_ENDL;
58 BOOST_AUTO_TEST_CASE(Initialize_WithError)
60 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Initialize_WithError" << TLOG_ENDL;
63 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
64 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
68 std::string testConfig =
"err: { level: 5 metricPluginType: nonExistentPluginType reporting_interval: 1.0}";
69 fhicl::ParameterSet pset;
70 fhicl::make_ParameterSet(testConfig, pset);
74 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
77 BOOST_REQUIRE_EQUAL(mm.
Running(),
true);
78 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
79 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Initialize_WithError" << TLOG_ENDL;
82 BOOST_AUTO_TEST_CASE(Shutdown)
84 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Shutdown" << TLOG_ENDL;
87 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
88 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
92 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 1.0}";
93 fhicl::ParameterSet pset;
94 fhicl::make_ParameterSet(testConfig, pset);
98 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
99 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
102 BOOST_REQUIRE_EQUAL(mm.
Running(),
true);
103 BOOST_REQUIRE_EQUAL(mm.
Active(),
true);
106 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
111 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Shutdown" << TLOG_ENDL;
114 BOOST_AUTO_TEST_CASE(SendMetric_String)
116 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetric_String" << TLOG_ENDL;
119 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
120 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
124 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 1.0}";
125 fhicl::ParameterSet pset;
126 fhicl::make_ParameterSet(testConfig, pset);
130 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
131 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
134 BOOST_REQUIRE_EQUAL(mm.
Running(),
true);
135 BOOST_REQUIRE_EQUAL(mm.
Active(),
true);
140 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
145 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetric_String" << TLOG_ENDL;
148 BOOST_AUTO_TEST_CASE(SendMetrics)
150 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetrics" << TLOG_ENDL;
153 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
154 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
158 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 0.01}";
159 fhicl::ParameterSet pset;
160 fhicl::make_ParameterSet(testConfig, pset);
164 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
165 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
168 BOOST_REQUIRE_EQUAL(mm.
Running(),
true);
169 BOOST_REQUIRE_EQUAL(mm.
Active(),
true);
175 std::lock_guard<std::mutex> lk(artdaq::TestMetric::received_metrics_mutex);
176 bool present =
false;
177 for (
auto& point : artdaq::TestMetric::received_metrics)
179 TLOG(TLVL_DEBUG) <<
"Metric: " << point.metric <<
", Value: " << point.value <<
", Units: " << point.unit;
180 if (point.metric ==
"Test Metric LastPoint")
182 BOOST_REQUIRE_EQUAL(point.value,
"5");
183 BOOST_REQUIRE_EQUAL(point.unit,
"Units");
187 BOOST_REQUIRE(present);
188 artdaq::TestMetric::received_metrics.clear();
195 std::lock_guard<std::mutex> lk(artdaq::TestMetric::received_metrics_mutex);
196 bool present =
false;
197 for (
auto& point : artdaq::TestMetric::received_metrics)
199 if (point.metric ==
"Test Metric Accumulate")
201 BOOST_REQUIRE_EQUAL(point.value,
"9");
202 BOOST_REQUIRE_EQUAL(point.unit,
"Units");
206 BOOST_REQUIRE(present);
207 artdaq::TestMetric::received_metrics.clear();
214 std::lock_guard<std::mutex> lk(artdaq::TestMetric::received_metrics_mutex);
215 bool present =
false;
216 for (
auto& point : artdaq::TestMetric::received_metrics)
218 if (point.metric ==
"Test Metric Average")
220 BOOST_REQUIRE_EQUAL(std::stof(point.value), 2);
221 BOOST_REQUIRE_EQUAL(point.unit,
"Units");
225 BOOST_REQUIRE(present);
226 artdaq::TestMetric::received_metrics.clear();
233 std::lock_guard<std::mutex> lk(artdaq::TestMetric::received_metrics_mutex);
234 bool present =
false;
235 for (
auto& point : artdaq::TestMetric::received_metrics)
237 if (point.metric ==
"Test Metric Rate")
239 BOOST_REQUIRE_EQUAL(point.unit,
"Units/s");
243 BOOST_REQUIRE(present);
244 artdaq::TestMetric::received_metrics.clear();
251 std::lock_guard<std::mutex> lk(artdaq::TestMetric::received_metrics_mutex);
253 for (
auto& point : artdaq::TestMetric::received_metrics)
255 if (point.metric ==
"Test Metric AccumulateAndRate - Total")
257 BOOST_REQUIRE_EQUAL(point.value,
"9");
258 BOOST_REQUIRE_EQUAL(point.unit,
"Units");
261 if (point.metric ==
"Test Metric AccumulateAndRate - Rate")
263 BOOST_REQUIRE_EQUAL(point.unit,
"Units/s");
267 BOOST_REQUIRE_EQUAL(present, 2);
268 artdaq::TestMetric::received_metrics.clear();
272 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
277 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetrics" << TLOG_ENDL;
280 BOOST_AUTO_TEST_CASE(SendMetrics_Levels)
282 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetrics_Levels" << TLOG_ENDL;
285 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
286 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
290 std::string testConfig =
"msgFac: { level: 0 metric_levels: [ 1, 2 ] level_string: \"3-5,9,7\" metricPluginType: test reporting_interval: 0.01}";
291 fhicl::ParameterSet pset;
292 fhicl::make_ParameterSet(testConfig, pset);
296 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
297 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
300 BOOST_REQUIRE_EQUAL(mm.
Running(),
true);
301 BOOST_REQUIRE_EQUAL(mm.
Active(),
true);
314 std::bitset<11> received_metrics_;
317 std::lock_guard<std::mutex> lk(artdaq::TestMetric::received_metrics_mutex);
318 for (
auto& point : artdaq::TestMetric::received_metrics)
320 if (point.metric ==
"Test Metric 0")
322 BOOST_REQUIRE_EQUAL(point.value,
"0");
323 BOOST_REQUIRE_EQUAL(point.unit,
"Units");
324 received_metrics_[0] =
true;
326 if (point.metric ==
"Test Metric 1")
328 BOOST_REQUIRE_EQUAL(point.value,
"1");
329 BOOST_REQUIRE_EQUAL(point.unit,
"Units");
330 received_metrics_[1] =
true;
332 if (point.metric ==
"Test Metric 2")
334 BOOST_REQUIRE_EQUAL(point.value,
"2");
335 BOOST_REQUIRE_EQUAL(point.unit,
"Units");
336 received_metrics_[2] =
true;
338 if (point.metric ==
"Test Metric 3")
340 BOOST_REQUIRE_EQUAL(point.value,
"3");
341 BOOST_REQUIRE_EQUAL(point.unit,
"Units");
342 received_metrics_[3] =
true;
344 if (point.metric ==
"Test Metric 4")
346 BOOST_REQUIRE_EQUAL(point.value,
"4");
347 BOOST_REQUIRE_EQUAL(point.unit,
"Units");
348 received_metrics_[4] =
true;
350 if (point.metric ==
"Test Metric 5")
352 BOOST_REQUIRE_EQUAL(point.value,
"5");
353 BOOST_REQUIRE_EQUAL(point.unit,
"Units");
354 received_metrics_[5] =
true;
356 if (point.metric ==
"Test Metric 6")
358 BOOST_TEST_FAIL(
"Metric level 6 should not have been sent!");
359 received_metrics_[6] =
true;
361 if (point.metric ==
"Test Metric 7")
363 BOOST_REQUIRE_EQUAL(point.value,
"7");
364 BOOST_REQUIRE_EQUAL(point.unit,
"Units");
365 received_metrics_[7] =
true;
367 if (point.metric ==
"Test Metric 8")
369 BOOST_TEST_FAIL(
"Metric level 8 should not have been sent!");
370 received_metrics_[8] =
true;
372 if (point.metric ==
"Test Metric 9")
374 BOOST_REQUIRE_EQUAL(point.value,
"9");
375 BOOST_REQUIRE_EQUAL(point.unit,
"Units");
376 received_metrics_[9] =
true;
378 if (point.metric ==
"Test Metric 10")
380 BOOST_TEST_FAIL(
"Metric level 10 should not have been sent!");
381 received_metrics_[10] =
true;
384 BOOST_REQUIRE_EQUAL(received_metrics_.to_ulong(), 0x2BF);
385 artdaq::TestMetric::received_metrics.clear();
389 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
394 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetrics_Levels" << TLOG_ENDL;
397 BOOST_AUTO_TEST_CASE(MetricFlood)
399 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST MetricFlood" << TLOG_ENDL;
402 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
403 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
407 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 1.0}";
408 fhicl::ParameterSet pset;
409 fhicl::make_ParameterSet(testConfig, pset);
413 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
414 BOOST_REQUIRE_EQUAL(mm.
Active(),
false);
417 BOOST_REQUIRE_EQUAL(mm.
Running(),
true);
418 BOOST_REQUIRE_EQUAL(mm.
Active(),
true);
420 auto beforeOne = std::chrono::steady_clock::now();
422 auto afterOne = std::chrono::steady_clock::now();
426 auto beforeTen = std::chrono::steady_clock::now();
427 for (
auto ii = 1; ii <= 10; ++ii)
431 auto afterTen = std::chrono::steady_clock::now();
435 auto beforeOneHundred = std::chrono::steady_clock::now();
436 for (
auto ii = 1; ii <= 100; ++ii)
440 auto afterOneHundred = std::chrono::steady_clock::now();
444 auto beforeOneThousand = std::chrono::steady_clock::now();
445 for (
auto ii = 1; ii <= 1000; ++ii)
449 auto afterOneThousand = std::chrono::steady_clock::now();
453 auto beforeTenThousand = std::chrono::steady_clock::now();
454 for (
auto ii = 1; ii <= 10000; ++ii)
458 auto afterTenThousand = std::chrono::steady_clock::now();
460 auto beforeStop = std::chrono::steady_clock::now();
462 BOOST_REQUIRE_EQUAL(mm.
Running(),
false);
464 auto afterStop = std::chrono::steady_clock::now();
466 TLOG_INFO(
"MetricManager_t") <<
"Time for One Metric: " << GetElapsedTime(beforeOne, afterOne) <<
" s." << TLOG_ENDL;
467 TLOG_INFO(
"MetricManager_t") <<
"Time for Ten Metrics: " << GetElapsedTime(beforeTen, afterTen) <<
" s." << TLOG_ENDL;
468 TLOG_INFO(
"MetricManager_t") <<
"Time for One Hundred Metrics: " << GetElapsedTime(beforeOneHundred, afterOneHundred)
469 <<
" s." << TLOG_ENDL;
470 TLOG_INFO(
"MetricManager_t") <<
"Time for One Thousand Metrics: "
471 << GetElapsedTime(beforeOneThousand, afterOneThousand) <<
" s." << TLOG_ENDL;
472 TLOG_INFO(
"MetricManager_t") <<
"Time for Ten Thousand Metrics: "
473 << GetElapsedTime(beforeTenThousand, afterTenThousand) <<
" s." << TLOG_ENDL;
474 TLOG_INFO(
"MetricManager_t") <<
"Time for Stop Metrics: " << GetElapsedTime(beforeStop, afterStop) <<
" s."
479 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST MetricFlood" << TLOG_ENDL;
482 BOOST_AUTO_TEST_SUITE_END()
void shutdown()
Call the destructors for all configured MetricPlugin instances.
void initialize(fhicl::ParameterSet const &pset, std::string const &prefix="")
Initialize the MetricPlugin instances.
bool Initialized()
Returns whether the MetricManager has been initialized (configured)
void sendMetric(std::string const &name, std::string const &value, std::string const &unit, int level, MetricMode mode, std::string const &metricPrefix="", bool useNameOverride=false)
Send a metric with the given parameters to any MetricPlugins with a threshold level >= to level...
size_t metricQueueSize(std::string const &name="")
Return the size of the named metric queue
Report the sum of all values. Use for counters to report accurate results.
void do_start()
Perform startup actions for each configured MetricPlugin.
bool Running()
Returns whether the MetricManager is running (accepting metric calls)
void do_stop()
Stop sending metrics to the MetricPlugin instances.
Report only the last value recorded. Useful for event counters, run numbers, etc. ...
The MetricManager class handles loading metric plugins and asynchronously sending metric data to them...
bool Active()
Returns whether any Metric Plugins are defined and configured
bool metricQueueEmpty()
Returns whether the metric queue is completely empty
Report the average of all values. Use for rates to report accurate results.