2 #define TRACE_NAME "MetricManager_t"
5 #include "artdaq-utilities/Plugins/MetricManager.hh"
8 #define BOOST_TEST_MODULES MetricManager_t
9 #include "cetlib/quiet_unit_test.hpp"
10 #include "cetlib_except/exception.h"
11 #include "fhiclcpp/make_ParameterSet.h"
13 BOOST_AUTO_TEST_SUITE(MetricManager_test)
15 #define TRACE_REQUIRE_EQUAL(l, r) \
20 TLOG(TLVL_DEBUG) << __LINE__ << ": Checking if " << #l << " (" << (l) << ") equals " << #r << " (" << (r) << ")...YES!"; \
24 TLOG(TLVL_ERROR) << __LINE__ << ": Checking if " << #l << " (" << (l) << ") equals " << #r << " (" << (r) << ")...NO!"; \
26 BOOST_REQUIRE_EQUAL((l), (r)); \
29 using seconds = std::chrono::duration<double, std::ratio<1>>;
31 constexpr
double GetElapsedTime(std::chrono::steady_clock::time_point then,
32 std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now())
34 return std::chrono::duration_cast<seconds>(now - then).count();
37 BOOST_AUTO_TEST_CASE(Construct)
39 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Construct" << TLOG_ENDL;
42 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
43 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
46 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Construct" << TLOG_ENDL;
49 BOOST_AUTO_TEST_CASE(Initialize)
51 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Initialize" << TLOG_ENDL;
54 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
55 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
59 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 1.0}";
60 fhicl::ParameterSet pset;
61 fhicl::make_ParameterSet(testConfig, pset);
65 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
66 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
69 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
70 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
71 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Initialize" << TLOG_ENDL;
74 BOOST_AUTO_TEST_CASE(Initialize_WithError)
76 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Initialize_WithError" << TLOG_ENDL;
79 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
80 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
84 std::string testConfig =
"err: { level: 5 metricPluginType: nonExistentPluginType reporting_interval: 1.0}";
85 fhicl::ParameterSet pset;
86 fhicl::make_ParameterSet(testConfig, pset);
90 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
93 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
94 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
95 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Initialize_WithError" << TLOG_ENDL;
98 BOOST_AUTO_TEST_CASE(Shutdown)
100 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Shutdown" << TLOG_ENDL;
103 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
104 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
108 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 1.0}";
109 fhicl::ParameterSet pset;
110 fhicl::make_ParameterSet(testConfig, pset);
114 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
115 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
118 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
119 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
122 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
127 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Shutdown" << TLOG_ENDL;
130 BOOST_AUTO_TEST_CASE(SendMetric_String)
132 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetric_String" << TLOG_ENDL;
135 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
136 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
140 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 1.0}";
141 fhicl::ParameterSet pset;
142 fhicl::make_ParameterSet(testConfig, pset);
146 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
147 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
150 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
151 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
156 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
161 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetric_String" << TLOG_ENDL;
164 BOOST_AUTO_TEST_CASE(SendMetrics)
166 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetrics" << TLOG_ENDL;
169 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
170 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
174 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 0.5 send_zeros: false} metric_send_maximum_delay_ms: 100 metric_holdoff_us: 10000";
175 fhicl::ParameterSet pset;
176 fhicl::make_ParameterSet(testConfig, pset);
180 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
181 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
184 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
185 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
196 bool present =
false;
199 TLOG(TLVL_DEBUG) <<
"Metric: " << point.metric <<
", Value: " << point.value <<
", Units: " << point.unit;
200 if (point.metric ==
"Test Metric LastPoint")
202 TRACE_REQUIRE_EQUAL(point.value,
"5");
203 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
207 BOOST_REQUIRE(present);
208 artdaq::TestMetric::received_metrics.clear();
221 bool present =
false;
222 for (
auto& point : artdaq::TestMetric::received_metrics)
224 if (point.metric ==
"Test Metric Accumulate")
226 TRACE_REQUIRE_EQUAL(point.value,
"9");
227 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
231 BOOST_REQUIRE(present);
232 artdaq::TestMetric::received_metrics.clear();
245 bool present =
false;
246 for (
auto& point : artdaq::TestMetric::received_metrics)
248 if (point.metric ==
"Test Metric Average")
250 TRACE_REQUIRE_EQUAL(std::stof(point.value), 2);
251 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
255 BOOST_REQUIRE(present);
256 artdaq::TestMetric::received_metrics.clear();
269 bool present =
false;
270 for (
auto& point : artdaq::TestMetric::received_metrics)
272 if (point.metric ==
"Test Metric Rate")
274 TRACE_REQUIRE_EQUAL(point.unit,
"Units/s");
278 BOOST_REQUIRE(present);
279 artdaq::TestMetric::received_metrics.clear();
293 for (
auto& point : artdaq::TestMetric::received_metrics)
295 if (point.metric ==
"Test Metric AccumulateAndRate - Total")
297 TRACE_REQUIRE_EQUAL(point.value,
"9");
298 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
301 if (point.metric ==
"Test Metric AccumulateAndRate - Rate")
303 TRACE_REQUIRE_EQUAL(point.unit,
"Units/s");
307 TRACE_REQUIRE_EQUAL(present, 2);
308 artdaq::TestMetric::received_metrics.clear();
313 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
318 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetrics" << TLOG_ENDL;
321 BOOST_AUTO_TEST_CASE(SendMetrics_Levels)
323 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetrics_Levels" << TLOG_ENDL;
326 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
327 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
331 std::string testConfig =
"msgFac: { level: 0 metric_levels: [ 1, 2 ] level_string: \"3-5,9,7\" metricPluginType: test reporting_interval: 0.1 send_zeros: false} metric_send_maximum_delay_ms: 100";
332 fhicl::ParameterSet pset;
333 fhicl::make_ParameterSet(testConfig, pset);
337 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
338 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
341 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
342 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
355 std::bitset<11> received_metrics_;
364 for (
auto& point : artdaq::TestMetric::received_metrics)
366 if (point.metric ==
"Test Metric 0")
368 TRACE_REQUIRE_EQUAL(point.value,
"0");
369 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
370 received_metrics_[0] =
true;
372 if (point.metric ==
"Test Metric 1")
374 TRACE_REQUIRE_EQUAL(point.value,
"1");
375 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
376 received_metrics_[1] =
true;
378 if (point.metric ==
"Test Metric 2")
380 TRACE_REQUIRE_EQUAL(point.value,
"2");
381 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
382 received_metrics_[2] =
true;
384 if (point.metric ==
"Test Metric 3")
386 TRACE_REQUIRE_EQUAL(point.value,
"3");
387 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
388 received_metrics_[3] =
true;
390 if (point.metric ==
"Test Metric 4")
392 TRACE_REQUIRE_EQUAL(point.value,
"4");
393 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
394 received_metrics_[4] =
true;
396 if (point.metric ==
"Test Metric 5")
398 TRACE_REQUIRE_EQUAL(point.value,
"5");
399 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
400 received_metrics_[5] =
true;
402 if (point.metric ==
"Test Metric 6")
404 BOOST_TEST_FAIL(
"Metric level 6 should not have been sent!");
405 received_metrics_[6] =
true;
407 if (point.metric ==
"Test Metric 7")
409 TRACE_REQUIRE_EQUAL(point.value,
"7");
410 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
411 received_metrics_[7] =
true;
413 if (point.metric ==
"Test Metric 8")
415 BOOST_TEST_FAIL(
"Metric level 8 should not have been sent!");
416 received_metrics_[8] =
true;
418 if (point.metric ==
"Test Metric 9")
420 TRACE_REQUIRE_EQUAL(point.value,
"9");
421 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
422 received_metrics_[9] =
true;
424 if (point.metric ==
"Test Metric 10")
426 BOOST_TEST_FAIL(
"Metric level 10 should not have been sent!");
427 received_metrics_[10] =
true;
430 TRACE_REQUIRE_EQUAL(received_metrics_.to_ulong(), 0x2BF);
431 artdaq::TestMetric::received_metrics.clear();
436 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
441 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetrics_Levels" << TLOG_ENDL;
444 BOOST_AUTO_TEST_CASE(MetricFlood)
446 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST MetricFlood" << TLOG_ENDL;
449 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
450 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
454 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 0.1 send_zeros: false} metric_send_maximum_delay_ms: 100";
455 fhicl::ParameterSet pset;
456 fhicl::make_ParameterSet(testConfig, pset);
460 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
461 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
464 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
465 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
467 auto beforeOne = std::chrono::steady_clock::now();
469 auto afterOne = std::chrono::steady_clock::now();
476 auto beforeTen = std::chrono::steady_clock::now();
477 for (
auto ii = 1; ii <= 10; ++ii)
481 auto afterTen = std::chrono::steady_clock::now();
488 auto beforeOneHundred = std::chrono::steady_clock::now();
489 for (
auto ii = 1; ii <= 100; ++ii)
493 auto afterOneHundred = std::chrono::steady_clock::now();
500 auto beforeOneThousand = std::chrono::steady_clock::now();
501 for (
auto ii = 1; ii <= 1000; ++ii)
505 auto afterOneThousand = std::chrono::steady_clock::now();
512 auto beforeTenThousand = std::chrono::steady_clock::now();
513 for (
auto ii = 1; ii <= 10000; ++ii)
517 auto afterTenThousand = std::chrono::steady_clock::now();
519 auto beforeStop = std::chrono::steady_clock::now();
521 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
523 auto afterStop = std::chrono::steady_clock::now();
525 TLOG_INFO(
"MetricManager_t") <<
"Time for One Metric: " << GetElapsedTime(beforeOne, afterOne) <<
" s." << TLOG_ENDL;
526 TLOG_INFO(
"MetricManager_t") <<
"Time for Ten Metrics: " << GetElapsedTime(beforeTen, afterTen) <<
" s." << TLOG_ENDL;
527 TLOG_INFO(
"MetricManager_t") <<
"Time for One Hundred Metrics: " << GetElapsedTime(beforeOneHundred, afterOneHundred)
528 <<
" s." << TLOG_ENDL;
529 TLOG_INFO(
"MetricManager_t") <<
"Time for One Thousand Metrics: "
530 << GetElapsedTime(beforeOneThousand, afterOneThousand) <<
" s." << TLOG_ENDL;
531 TLOG_INFO(
"MetricManager_t") <<
"Time for Ten Thousand Metrics: "
532 << GetElapsedTime(beforeTenThousand, afterTenThousand) <<
" s." << TLOG_ENDL;
533 TLOG_INFO(
"MetricManager_t") <<
"Time for Stop Metrics: " << GetElapsedTime(beforeStop, afterStop) <<
" s."
538 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST MetricFlood" << TLOG_ENDL;
541 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...
bool metricManagerBusy()
Determine whether the MetricManager or any of its plugins are currently processing metrics...
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.
static void LockReceivedMetricMutex()
Lock the ReceivedMetricMutex
static std::list< MetricPoint > received_metrics
List of received metric data.
bool Running()
Returns whether the MetricManager is running (accepting metric calls)
void do_stop()
Stop sending metrics to the MetricPlugin instances.
over. Use to create rates from counters.
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...
static void UnlockReceivedMetricMutex()
Unlock the ReceivedMetricMutex
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.