2 #define TRACE_NAME "MetricManager_t"
5 #include "artdaq-utilities/Plugins/MetricManager.hh"
8 #define BOOST_TEST_MODULE MetricManager_t
9 #include "cetlib/quiet_unit_test.hpp"
10 #include "cetlib_except/exception.h"
12 BOOST_AUTO_TEST_SUITE(MetricManager_test)
14 #define TRACE_REQUIRE_EQUAL(l, r) \
19 TLOG(TLVL_DEBUG) << __LINE__ << ": Checking if " << #l << " (" << (l) << ") equals " << #r << " (" << (r) << ")...YES!"; \
23 TLOG(TLVL_ERROR) << __LINE__ << ": Checking if " << #l << " (" << (l) << ") equals " << #r << " (" << (r) << ")...NO!"; \
25 BOOST_REQUIRE_EQUAL((l), (r)); \
28 using seconds = std::chrono::duration<double, std::ratio<1>>;
30 constexpr
double GetElapsedTime(std::chrono::steady_clock::time_point then,
31 std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now())
33 return std::chrono::duration_cast<seconds>(now - then).count();
36 BOOST_AUTO_TEST_CASE(Construct)
38 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Construct" << TLOG_ENDL;
41 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
42 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
45 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Construct" << TLOG_ENDL;
48 BOOST_AUTO_TEST_CASE(Initialize)
50 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Initialize" << TLOG_ENDL;
53 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
54 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
58 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 1.0}";
59 fhicl::ParameterSet pset = fhicl::ParameterSet::make(testConfig);
63 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
64 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
67 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
68 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
69 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Initialize" << TLOG_ENDL;
72 BOOST_AUTO_TEST_CASE(Initialize_WithError)
74 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Initialize_WithError" << TLOG_ENDL;
77 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
78 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
82 std::string testConfig =
"err: { level: 5 metricPluginType: nonExistentPluginType reporting_interval: 1.0}";
83 fhicl::ParameterSet pset = fhicl::ParameterSet::make(testConfig);
87 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
90 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
91 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
92 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Initialize_WithError" << TLOG_ENDL;
95 BOOST_AUTO_TEST_CASE(Shutdown)
97 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST Shutdown" << TLOG_ENDL;
100 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
101 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
105 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 1.0}";
106 fhicl::ParameterSet pset = fhicl::ParameterSet::make(testConfig);
110 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
111 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
114 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
115 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
118 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
123 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST Shutdown" << TLOG_ENDL;
126 BOOST_AUTO_TEST_CASE(SendMetric_String)
128 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetric_String" << TLOG_ENDL;
131 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
132 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
136 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 1.0}";
137 fhicl::ParameterSet pset = fhicl::ParameterSet::make(testConfig);
141 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
142 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
145 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
146 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
151 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
156 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetric_String" << TLOG_ENDL;
159 BOOST_AUTO_TEST_CASE(SendMetrics)
161 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetrics" << TLOG_ENDL;
164 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
165 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
169 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";
170 fhicl::ParameterSet pset = fhicl::ParameterSet::make(testConfig);
174 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
175 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
178 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
179 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
190 bool present =
false;
193 TLOG(TLVL_DEBUG) <<
"Metric: " << point.metric <<
", Value: " << point.value <<
", Units: " << point.unit;
194 if (point.metric ==
"Test Metric LastPoint")
196 TRACE_REQUIRE_EQUAL(point.value,
"5");
197 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
201 BOOST_REQUIRE(present);
202 artdaq::TestMetric::received_metrics.clear();
215 bool present =
false;
216 for (
auto& point : artdaq::TestMetric::received_metrics)
218 if (point.metric ==
"Test Metric Accumulate")
220 TRACE_REQUIRE_EQUAL(point.value,
"9");
221 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
225 BOOST_REQUIRE(present);
226 artdaq::TestMetric::received_metrics.clear();
239 bool present =
false;
240 for (
auto& point : artdaq::TestMetric::received_metrics)
242 if (point.metric ==
"Test Metric Average")
244 TRACE_REQUIRE_EQUAL(std::stof(point.value), 2);
245 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
249 BOOST_REQUIRE(present);
250 artdaq::TestMetric::received_metrics.clear();
263 bool present =
false;
264 for (
auto& point : artdaq::TestMetric::received_metrics)
266 if (point.metric ==
"Test Metric Rate")
268 TRACE_REQUIRE_EQUAL(point.unit,
"Units/s");
272 BOOST_REQUIRE(present);
273 artdaq::TestMetric::received_metrics.clear();
287 for (
auto& point : artdaq::TestMetric::received_metrics)
289 if (point.metric ==
"Test Metric AccumulateAndRate - Total")
291 TRACE_REQUIRE_EQUAL(point.value,
"9");
292 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
295 if (point.metric ==
"Test Metric AccumulateAndRate - Rate")
297 TRACE_REQUIRE_EQUAL(point.unit,
"Units/s");
301 TRACE_REQUIRE_EQUAL(present, 2);
302 artdaq::TestMetric::received_metrics.clear();
307 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
312 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetrics" << TLOG_ENDL;
315 BOOST_AUTO_TEST_CASE(SendMetrics_Levels)
317 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetrics_Levels" << TLOG_ENDL;
320 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
321 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
325 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";
326 fhicl::ParameterSet pset = fhicl::ParameterSet::make(testConfig);
330 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
331 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
334 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
335 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
348 std::bitset<11> received_metrics_;
357 for (
auto& point : artdaq::TestMetric::received_metrics)
359 if (point.metric ==
"Test Metric 0")
361 TRACE_REQUIRE_EQUAL(point.value,
"0");
362 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
363 received_metrics_[0] =
true;
365 if (point.metric ==
"Test Metric 1")
367 TRACE_REQUIRE_EQUAL(point.value,
"1");
368 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
369 received_metrics_[1] =
true;
371 if (point.metric ==
"Test Metric 2")
373 TRACE_REQUIRE_EQUAL(point.value,
"2");
374 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
375 received_metrics_[2] =
true;
377 if (point.metric ==
"Test Metric 3")
379 TRACE_REQUIRE_EQUAL(point.value,
"3");
380 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
381 received_metrics_[3] =
true;
383 if (point.metric ==
"Test Metric 4")
385 TRACE_REQUIRE_EQUAL(point.value,
"4");
386 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
387 received_metrics_[4] =
true;
389 if (point.metric ==
"Test Metric 5")
391 TRACE_REQUIRE_EQUAL(point.value,
"5");
392 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
393 received_metrics_[5] =
true;
395 if (point.metric ==
"Test Metric 6")
397 BOOST_TEST_FAIL(
"Metric level 6 should not have been sent!");
398 received_metrics_[6] =
true;
400 if (point.metric ==
"Test Metric 7")
402 TRACE_REQUIRE_EQUAL(point.value,
"7");
403 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
404 received_metrics_[7] =
true;
406 if (point.metric ==
"Test Metric 8")
408 BOOST_TEST_FAIL(
"Metric level 8 should not have been sent!");
409 received_metrics_[8] =
true;
411 if (point.metric ==
"Test Metric 9")
413 TRACE_REQUIRE_EQUAL(point.value,
"9");
414 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
415 received_metrics_[9] =
true;
417 if (point.metric ==
"Test Metric 10")
419 BOOST_TEST_FAIL(
"Metric level 10 should not have been sent!");
420 received_metrics_[10] =
true;
423 TRACE_REQUIRE_EQUAL(received_metrics_.to_ulong(), 0x2BF);
424 artdaq::TestMetric::received_metrics.clear();
429 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
434 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetrics_Levels" << TLOG_ENDL;
437 BOOST_AUTO_TEST_CASE(MetricFlood)
439 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST MetricFlood" << TLOG_ENDL;
442 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
443 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
447 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 0.1 send_zeros: false} metric_send_maximum_delay_ms: 100";
448 fhicl::ParameterSet pset = fhicl::ParameterSet::make(testConfig);
452 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
453 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
456 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
457 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
459 auto beforeOne = std::chrono::steady_clock::now();
461 auto afterOne = std::chrono::steady_clock::now();
468 auto beforeTen = std::chrono::steady_clock::now();
469 for (
auto ii = 1; ii <= 10; ++ii)
473 auto afterTen = std::chrono::steady_clock::now();
480 auto beforeOneHundred = std::chrono::steady_clock::now();
481 for (
auto ii = 1; ii <= 100; ++ii)
485 auto afterOneHundred = std::chrono::steady_clock::now();
492 auto beforeOneThousand = std::chrono::steady_clock::now();
493 for (
auto ii = 1; ii <= 1000; ++ii)
497 auto afterOneThousand = std::chrono::steady_clock::now();
504 auto beforeTenThousand = std::chrono::steady_clock::now();
505 for (
auto ii = 1; ii <= 10000; ++ii)
509 auto afterTenThousand = std::chrono::steady_clock::now();
511 auto beforeStop = std::chrono::steady_clock::now();
513 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
515 auto afterStop = std::chrono::steady_clock::now();
517 TLOG_INFO(
"MetricManager_t") <<
"Time for One Metric: " << GetElapsedTime(beforeOne, afterOne) <<
" s." << TLOG_ENDL;
518 TLOG_INFO(
"MetricManager_t") <<
"Time for Ten Metrics: " << GetElapsedTime(beforeTen, afterTen) <<
" s." << TLOG_ENDL;
519 TLOG_INFO(
"MetricManager_t") <<
"Time for One Hundred Metrics: " << GetElapsedTime(beforeOneHundred, afterOneHundred)
520 <<
" s." << TLOG_ENDL;
521 TLOG_INFO(
"MetricManager_t") <<
"Time for One Thousand Metrics: "
522 << GetElapsedTime(beforeOneThousand, afterOneThousand) <<
" s." << TLOG_ENDL;
523 TLOG_INFO(
"MetricManager_t") <<
"Time for Ten Thousand Metrics: "
524 << GetElapsedTime(beforeTenThousand, afterTenThousand) <<
" s." << TLOG_ENDL;
525 TLOG_INFO(
"MetricManager_t") <<
"Time for Stop Metrics: " << GetElapsedTime(beforeStop, afterStop) <<
" s."
530 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST MetricFlood" << TLOG_ENDL;
533 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.