2 #define TRACE_NAME "MetricManager_t"
5 #include "artdaq-utilities/Plugins/MakeParameterSet.hh"
6 #include "artdaq-utilities/Plugins/MetricManager.hh"
9 #define BOOST_TEST_MODULE MetricManager_t
10 #include "cetlib/quiet_unit_test.hpp"
11 #include "cetlib_except/exception.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 = artdaq::make_pset(testConfig);
64 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
65 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
68 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
69 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
70 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Initialize" << TLOG_ENDL;
73 BOOST_AUTO_TEST_CASE(Initialize_WithError)
75 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Initialize_WithError" << TLOG_ENDL;
78 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
79 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
83 std::string testConfig =
"err: { level: 5 metricPluginType: nonExistentPluginType reporting_interval: 1.0}";
84 fhicl::ParameterSet pset = artdaq::make_pset(testConfig);
88 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
91 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
92 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
93 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Initialize_WithError" << TLOG_ENDL;
96 BOOST_AUTO_TEST_CASE(Shutdown)
98 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Shutdown" << TLOG_ENDL;
101 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
102 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
106 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 1.0}";
107 fhicl::ParameterSet pset = artdaq::make_pset(testConfig);
111 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
112 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
115 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
116 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
119 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
124 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Shutdown" << TLOG_ENDL;
127 BOOST_AUTO_TEST_CASE(SendMetric_String)
129 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetric_String" << TLOG_ENDL;
132 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
133 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
137 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 1.0}";
138 fhicl::ParameterSet pset = artdaq::make_pset(testConfig);
142 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
143 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
146 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
147 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
152 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
157 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetric_String" << TLOG_ENDL;
160 BOOST_AUTO_TEST_CASE(SendMetrics)
162 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetrics" << TLOG_ENDL;
165 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
166 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
170 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";
171 fhicl::ParameterSet pset = artdaq::make_pset(testConfig);
175 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
176 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
179 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
180 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
191 bool present =
false;
194 TLOG(TLVL_DEBUG) <<
"Metric: " << point.metric <<
", Value: " << point.value <<
", Units: " << point.unit;
195 if (point.metric ==
"Test Metric LastPoint")
197 TRACE_REQUIRE_EQUAL(point.value,
"5");
198 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
202 BOOST_REQUIRE(present);
203 artdaq::TestMetric::received_metrics.clear();
216 bool present =
false;
217 for (
auto& point : artdaq::TestMetric::received_metrics)
219 if (point.metric ==
"Test Metric Accumulate")
221 TRACE_REQUIRE_EQUAL(point.value,
"9");
222 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
226 BOOST_REQUIRE(present);
227 artdaq::TestMetric::received_metrics.clear();
240 bool present =
false;
241 for (
auto& point : artdaq::TestMetric::received_metrics)
243 if (point.metric ==
"Test Metric Average")
245 TRACE_REQUIRE_EQUAL(std::stof(point.value), 2);
246 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
250 BOOST_REQUIRE(present);
251 artdaq::TestMetric::received_metrics.clear();
264 bool present =
false;
265 for (
auto& point : artdaq::TestMetric::received_metrics)
267 if (point.metric ==
"Test Metric Rate")
269 TRACE_REQUIRE_EQUAL(point.unit,
"Units/s");
273 BOOST_REQUIRE(present);
274 artdaq::TestMetric::received_metrics.clear();
288 for (
auto& point : artdaq::TestMetric::received_metrics)
290 if (point.metric ==
"Test Metric AccumulateAndRate - Total")
292 TRACE_REQUIRE_EQUAL(point.value,
"9");
293 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
296 if (point.metric ==
"Test Metric AccumulateAndRate - Rate")
298 TRACE_REQUIRE_EQUAL(point.unit,
"Units/s");
302 TRACE_REQUIRE_EQUAL(present, 2);
303 artdaq::TestMetric::received_metrics.clear();
308 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
313 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetrics" << TLOG_ENDL;
316 BOOST_AUTO_TEST_CASE(SendMetrics_Levels)
318 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetrics_Levels" << TLOG_ENDL;
321 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
322 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
326 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";
327 fhicl::ParameterSet pset = artdaq::make_pset(testConfig);
331 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
332 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
335 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
336 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
349 std::bitset<11> received_metrics_;
358 for (
auto& point : artdaq::TestMetric::received_metrics)
360 if (point.metric ==
"Test Metric 0")
362 TRACE_REQUIRE_EQUAL(point.value,
"0");
363 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
364 received_metrics_[0] =
true;
366 if (point.metric ==
"Test Metric 1")
368 TRACE_REQUIRE_EQUAL(point.value,
"1");
369 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
370 received_metrics_[1] =
true;
372 if (point.metric ==
"Test Metric 2")
374 TRACE_REQUIRE_EQUAL(point.value,
"2");
375 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
376 received_metrics_[2] =
true;
378 if (point.metric ==
"Test Metric 3")
380 TRACE_REQUIRE_EQUAL(point.value,
"3");
381 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
382 received_metrics_[3] =
true;
384 if (point.metric ==
"Test Metric 4")
386 TRACE_REQUIRE_EQUAL(point.value,
"4");
387 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
388 received_metrics_[4] =
true;
390 if (point.metric ==
"Test Metric 5")
392 TRACE_REQUIRE_EQUAL(point.value,
"5");
393 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
394 received_metrics_[5] =
true;
396 if (point.metric ==
"Test Metric 6")
398 BOOST_TEST_FAIL(
"Metric level 6 should not have been sent!");
399 received_metrics_[6] =
true;
401 if (point.metric ==
"Test Metric 7")
403 TRACE_REQUIRE_EQUAL(point.value,
"7");
404 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
405 received_metrics_[7] =
true;
407 if (point.metric ==
"Test Metric 8")
409 BOOST_TEST_FAIL(
"Metric level 8 should not have been sent!");
410 received_metrics_[8] =
true;
412 if (point.metric ==
"Test Metric 9")
414 TRACE_REQUIRE_EQUAL(point.value,
"9");
415 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
416 received_metrics_[9] =
true;
418 if (point.metric ==
"Test Metric 10")
420 BOOST_TEST_FAIL(
"Metric level 10 should not have been sent!");
421 received_metrics_[10] =
true;
424 TRACE_REQUIRE_EQUAL(received_metrics_.to_ulong(), 0x2BF);
425 artdaq::TestMetric::received_metrics.clear();
430 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
435 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetrics_Levels" << TLOG_ENDL;
438 BOOST_AUTO_TEST_CASE(MetricFlood)
440 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST MetricFlood" << TLOG_ENDL;
443 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
444 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
448 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 0.1 send_zeros: false} metric_send_maximum_delay_ms: 100";
449 fhicl::ParameterSet pset = artdaq::make_pset(testConfig);
453 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
454 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
457 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
458 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
460 auto beforeOne = std::chrono::steady_clock::now();
462 auto afterOne = std::chrono::steady_clock::now();
469 auto beforeTen = std::chrono::steady_clock::now();
470 for (
auto ii = 1; ii <= 10; ++ii)
474 auto afterTen = std::chrono::steady_clock::now();
481 auto beforeOneHundred = std::chrono::steady_clock::now();
482 for (
auto ii = 1; ii <= 100; ++ii)
486 auto afterOneHundred = std::chrono::steady_clock::now();
493 auto beforeOneThousand = std::chrono::steady_clock::now();
494 for (
auto ii = 1; ii <= 1000; ++ii)
498 auto afterOneThousand = std::chrono::steady_clock::now();
505 auto beforeTenThousand = std::chrono::steady_clock::now();
506 for (
auto ii = 1; ii <= 10000; ++ii)
510 auto afterTenThousand = std::chrono::steady_clock::now();
512 auto beforeStop = std::chrono::steady_clock::now();
514 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
516 auto afterStop = std::chrono::steady_clock::now();
518 TLOG_INFO(
"MetricManager_t") <<
"Time for One Metric: " << GetElapsedTime(beforeOne, afterOne) <<
" s." << TLOG_ENDL;
519 TLOG_INFO(
"MetricManager_t") <<
"Time for Ten Metrics: " << GetElapsedTime(beforeTen, afterTen) <<
" s." << TLOG_ENDL;
520 TLOG_INFO(
"MetricManager_t") <<
"Time for One Hundred Metrics: " << GetElapsedTime(beforeOneHundred, afterOneHundred)
521 <<
" s." << TLOG_ENDL;
522 TLOG_INFO(
"MetricManager_t") <<
"Time for One Thousand Metrics: "
523 << GetElapsedTime(beforeOneThousand, afterOneThousand) <<
" s." << TLOG_ENDL;
524 TLOG_INFO(
"MetricManager_t") <<
"Time for Ten Thousand Metrics: "
525 << GetElapsedTime(beforeTenThousand, afterTenThousand) <<
" s." << TLOG_ENDL;
526 TLOG_INFO(
"MetricManager_t") <<
"Time for Stop Metrics: " << GetElapsedTime(beforeStop, afterStop) <<
" s."
531 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST MetricFlood" << TLOG_ENDL;
534 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.