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 typedef std::chrono::duration<double, std::ratio<1>> seconds;
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);
193 bool present =
false;
196 TLOG(TLVL_DEBUG) <<
"Metric: " << point.metric <<
", Value: " << point.value <<
", Units: " << point.unit;
197 if (point.metric ==
"Test Metric LastPoint")
199 TRACE_REQUIRE_EQUAL(point.value,
"5");
200 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
204 BOOST_REQUIRE(present);
205 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();
236 bool present =
false;
237 for (
auto& point : artdaq::TestMetric::received_metrics)
239 if (point.metric ==
"Test Metric Average")
241 TRACE_REQUIRE_EQUAL(std::stof(point.value), 2);
242 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
246 BOOST_REQUIRE(present);
247 artdaq::TestMetric::received_metrics.clear();
257 bool present =
false;
258 for (
auto& point : artdaq::TestMetric::received_metrics)
260 if (point.metric ==
"Test Metric Rate")
262 TRACE_REQUIRE_EQUAL(point.unit,
"Units/s");
266 BOOST_REQUIRE(present);
267 artdaq::TestMetric::received_metrics.clear();
278 for (
auto& point : artdaq::TestMetric::received_metrics)
280 if (point.metric ==
"Test Metric AccumulateAndRate - Total")
282 TRACE_REQUIRE_EQUAL(point.value,
"9");
283 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
286 if (point.metric ==
"Test Metric AccumulateAndRate - Rate")
288 TRACE_REQUIRE_EQUAL(point.unit,
"Units/s");
292 TRACE_REQUIRE_EQUAL(present, 2);
293 artdaq::TestMetric::received_metrics.clear();
298 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
303 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetrics" << TLOG_ENDL;
306 BOOST_AUTO_TEST_CASE(SendMetrics_Levels)
308 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST SendMetrics_Levels" << TLOG_ENDL;
311 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
312 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
316 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";
317 fhicl::ParameterSet pset;
318 fhicl::make_ParameterSet(testConfig, pset);
322 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
323 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
326 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
327 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
340 std::bitset<11> received_metrics_;
346 for (
auto& point : artdaq::TestMetric::received_metrics)
348 if (point.metric ==
"Test Metric 0")
350 TRACE_REQUIRE_EQUAL(point.value,
"0");
351 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
352 received_metrics_[0] =
true;
354 if (point.metric ==
"Test Metric 1")
356 TRACE_REQUIRE_EQUAL(point.value,
"1");
357 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
358 received_metrics_[1] =
true;
360 if (point.metric ==
"Test Metric 2")
362 TRACE_REQUIRE_EQUAL(point.value,
"2");
363 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
364 received_metrics_[2] =
true;
366 if (point.metric ==
"Test Metric 3")
368 TRACE_REQUIRE_EQUAL(point.value,
"3");
369 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
370 received_metrics_[3] =
true;
372 if (point.metric ==
"Test Metric 4")
374 TRACE_REQUIRE_EQUAL(point.value,
"4");
375 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
376 received_metrics_[4] =
true;
378 if (point.metric ==
"Test Metric 5")
380 TRACE_REQUIRE_EQUAL(point.value,
"5");
381 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
382 received_metrics_[5] =
true;
384 if (point.metric ==
"Test Metric 6")
386 BOOST_TEST_FAIL(
"Metric level 6 should not have been sent!");
387 received_metrics_[6] =
true;
389 if (point.metric ==
"Test Metric 7")
391 TRACE_REQUIRE_EQUAL(point.value,
"7");
392 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
393 received_metrics_[7] =
true;
395 if (point.metric ==
"Test Metric 8")
397 BOOST_TEST_FAIL(
"Metric level 8 should not have been sent!");
398 received_metrics_[8] =
true;
400 if (point.metric ==
"Test Metric 9")
402 TRACE_REQUIRE_EQUAL(point.value,
"9");
403 TRACE_REQUIRE_EQUAL(point.unit,
"Units");
404 received_metrics_[9] =
true;
406 if (point.metric ==
"Test Metric 10")
408 BOOST_TEST_FAIL(
"Metric level 10 should not have been sent!");
409 received_metrics_[10] =
true;
412 TRACE_REQUIRE_EQUAL(received_metrics_.to_ulong(), 0x2BF);
413 artdaq::TestMetric::received_metrics.clear();
418 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
423 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST SendMetrics_Levels" << TLOG_ENDL;
426 BOOST_AUTO_TEST_CASE(MetricFlood)
428 TLOG_DEBUG(
"MetricManager_t") <<
"BEGIN TEST MetricFlood" << TLOG_ENDL;
431 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
432 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
436 std::string testConfig =
"msgFac: { level: 5 metricPluginType: test reporting_interval: 0.1 send_zeros: false} metric_send_maximum_delay_ms: 100";
437 fhicl::ParameterSet pset;
438 fhicl::make_ParameterSet(testConfig, pset);
442 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
443 TRACE_REQUIRE_EQUAL(mm.
Active(),
false);
446 TRACE_REQUIRE_EQUAL(mm.
Running(),
true);
447 TRACE_REQUIRE_EQUAL(mm.
Active(),
true);
449 auto beforeOne = std::chrono::steady_clock::now();
451 auto afterOne = std::chrono::steady_clock::now();
455 auto beforeTen = std::chrono::steady_clock::now();
456 for (
auto ii = 1; ii <= 10; ++ii)
460 auto afterTen = std::chrono::steady_clock::now();
464 auto beforeOneHundred = std::chrono::steady_clock::now();
465 for (
auto ii = 1; ii <= 100; ++ii)
469 auto afterOneHundred = std::chrono::steady_clock::now();
473 auto beforeOneThousand = std::chrono::steady_clock::now();
474 for (
auto ii = 1; ii <= 1000; ++ii)
478 auto afterOneThousand = std::chrono::steady_clock::now();
482 auto beforeTenThousand = std::chrono::steady_clock::now();
483 for (
auto ii = 1; ii <= 10000; ++ii)
487 auto afterTenThousand = std::chrono::steady_clock::now();
489 auto beforeStop = std::chrono::steady_clock::now();
491 TRACE_REQUIRE_EQUAL(mm.
Running(),
false);
493 auto afterStop = std::chrono::steady_clock::now();
495 TLOG_INFO(
"MetricManager_t") <<
"Time for One Metric: " << GetElapsedTime(beforeOne, afterOne) <<
" s." << TLOG_ENDL;
496 TLOG_INFO(
"MetricManager_t") <<
"Time for Ten Metrics: " << GetElapsedTime(beforeTen, afterTen) <<
" s." << TLOG_ENDL;
497 TLOG_INFO(
"MetricManager_t") <<
"Time for One Hundred Metrics: " << GetElapsedTime(beforeOneHundred, afterOneHundred)
498 <<
" s." << TLOG_ENDL;
499 TLOG_INFO(
"MetricManager_t") <<
"Time for One Thousand Metrics: "
500 << GetElapsedTime(beforeOneThousand, afterOneThousand) <<
" s." << TLOG_ENDL;
501 TLOG_INFO(
"MetricManager_t") <<
"Time for Ten Thousand Metrics: "
502 << GetElapsedTime(beforeTenThousand, afterTenThousand) <<
" s." << TLOG_ENDL;
503 TLOG_INFO(
"MetricManager_t") <<
"Time for Stop Metrics: " << GetElapsedTime(beforeStop, afterStop) <<
" s."
508 TLOG_DEBUG(
"MetricManager_t") <<
"END TEST MetricFlood" << TLOG_ENDL;
511 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.
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.