From 10c96024346168cba6ea93c1a460cabc1ed4f2ed Mon Sep 17 00:00:00 2001 From: Adam Wegrzynek Date: Mon, 12 Apr 2021 12:24:55 +0200 Subject: [PATCH] [OMON-443] Make derived metric tests deterministic (#240) --- test/testDerived.cxx | 165 ++++++++++++++++++++++++------------------- 1 file changed, 93 insertions(+), 72 deletions(-) diff --git a/test/testDerived.cxx b/test/testDerived.cxx index beee4b35c..8dfe4b364 100644 --- a/test/testDerived.cxx +++ b/test/testDerived.cxx @@ -13,7 +13,6 @@ #include "../src/VariantVisitorAdd.h" #include "../src/VariantVisitorRate.h" #include -#include #include #include @@ -28,100 +27,134 @@ namespace monitoring namespace Test { -BOOST_AUTO_TEST_CASE(derivedRateInt) +using namespace o2::monitoring; + +struct ResultsKv { + unsigned short int value; + unsigned short int result; +}; + +BOOST_AUTO_TEST_CASE(testDerivedRateForInts) { - struct RateResults { - int value; - int rate; - }; - std::vector results = {{10, 0}, {20, 100}, {30, 100}, {50, 200}, {60, 100}, {65, 50}, {70, 50}, {80, 100}, {90, 100}, {100, 100}}; - o2::monitoring::DerivedMetrics derivedHandler; + auto timestamp = std::chrono::system_clock::now(); + std::vector valResKv = {{10, 0}, {20, 10}, {30, 10}, {50, 20}, {60, 10}, {65, 5}, {70, 5}, {80, 10}, {90, 10}}; + DerivedMetrics derivedHandler; std::string name("metricInt"); - for (auto const result : results) { + for (auto const valRes : valResKv) { try { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - o2::monitoring::Metric metric(result.value, name); + Metric metric(name, Metric::DefaultVerbosity, timestamp); + metric.addValue(valRes.value, "value"); derivedHandler.process(metric, DerivedMetricMode::RATE); BOOST_CHECK_EQUAL(metric.getName(), "metricInt"); BOOST_CHECK_EQUAL(metric.getValues().back().first, "value_rate"); - BOOST_WARN_CLOSE(std::get(metric.getValues().back().second), result.rate, 1.0); + BOOST_CHECK_EQUAL(std::get(metric.getValues().back().second), valRes.result); + timestamp += std::chrono::seconds(1); } catch (MonitoringException& e) { BOOST_CHECK_EQUAL(e.what(), std::string("Not enough values")); } } } -BOOST_AUTO_TEST_CASE(derivedRateInt_newRun) +BOOST_AUTO_TEST_CASE(testDerivedRateForIntsAndResetValue) { - struct RateResults { - int value; - int rate; - }; - std::vector results = {{10, 0}, {20, 100}, {30, 100}, {50, 200}, {0, 0}, {10, 100}, {20, 100}, {30, 100}, {50, 200}}; - o2::monitoring::DerivedMetrics derivedHandler; + std::vector valResKv = {{10, 0}, {20, 10}, {30, 10}, {50, 20}, {0, 0}, {10, 10}, {20, 10}, {30, 10}, {50, 20}}; + DerivedMetrics derivedHandler; std::string name("metricInt"); + auto timestamp = std::chrono::system_clock::now(); - for (auto const result : results) { + for (auto const valRes : valResKv) { try { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - o2::monitoring::Metric metric(result.value, name); + Metric metric(name, Metric::DefaultVerbosity, timestamp); + metric.addValue(valRes.value, "value"); derivedHandler.process(metric, DerivedMetricMode::RATE); BOOST_CHECK_EQUAL(metric.getName(), "metricInt"); BOOST_CHECK_EQUAL(metric.getValues().back().first, "value_rate"); - BOOST_WARN_CLOSE(std::get(metric.getValues().back().second), result.rate, 1.0); + BOOST_CHECK_EQUAL(std::get(metric.getValues().back().second), valRes.result); + timestamp += std::chrono::seconds(1); } catch (MonitoringException& e) { BOOST_CHECK_EQUAL(e.what(), std::string("Not enough values")); } } } -BOOST_AUTO_TEST_CASE(derivedRateDouble) +struct ResultsKvDouble { + double value; + double result; +}; + +BOOST_AUTO_TEST_CASE(testDerivedRateForDoubles) { - struct RateResults { - double value; - double rate; - }; - std::vector results = {{1.2, 0}, {11.2, 100}, {21.2, 100}, {41.2, 200}, {51.2, 100}, {61, 98}}; - std::vector resultsTagged = {{0.5, 0}, {5.5, 50}, {10.5, 50}, {20.5, 100}, {40.5, 200}, {45.5, 50}}; + std::vector valResKv = {{1.2, 0}, {11.2, 10}, {21.2, 10}, {41.2, 20}, {51.2, 10}, {61, 9.8}}; o2::monitoring::DerivedMetrics derivedHandler; std::string name("metricDouble"); - for (std::size_t i = 0; i < results.size(); i++) { + auto timestamp = std::chrono::system_clock::now(); + + for (auto const valRes : valResKv) { try { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - o2::monitoring::Metric metric(results[i].value, name); - o2::monitoring::Metric metricTagged = Metric{resultsTagged[i].value, name}.addTag(o2::monitoring::tags::Key::Subsystem, o2::monitoring::tags::Value::Readout); + Metric metric(name, Metric::DefaultVerbosity, timestamp); + metric.addValue(valRes.value, "value"); derivedHandler.process(metric, DerivedMetricMode::RATE); - derivedHandler.process(metricTagged, DerivedMetricMode::RATE); BOOST_CHECK_EQUAL(metric.getName(), "metricDouble"); - BOOST_WARN_CLOSE(std::get(metric.getValues().back().second), results[i].rate, 1.0); + BOOST_CHECK_CLOSE(std::get(metric.getValues().back().second), valRes.result, 0.1); BOOST_CHECK_EQUAL(metric.getValues().back().first, "value_rate"); - BOOST_WARN_CLOSE(std::get(metricTagged.getValues().back().second), resultsTagged[i].rate, 1.0); - BOOST_CHECK_EQUAL(metricTagged.getValues().back().first, "value_rate"); + timestamp += std::chrono::seconds(1); } catch (MonitoringException& e) { BOOST_CHECK_EQUAL(e.what(), std::string("Not enough values")); } } } -BOOST_AUTO_TEST_CASE(derivedRateUint64_t) +BOOST_AUTO_TEST_CASE(testDerivedRateForDoublesWhenUsingTags) { - struct RateResults { - uint64_t value; - uint64_t rate; - }; - std::vector results = {{165535, 0}, {165545, 100}, {165555, 100}, {165575, 200}, {165585, 100}, {165590, 50}}; + std::vector valResKv1 = {{1.2, 0}, {11.2, 10}, {21.2, 10}, {41.2, 20}, {51.2, 10}, {61, 9.8}}; + std::vector valResKv2 = {{0.5, 0}, {5.5, 5}, {10.5, 5}, {20.5, 10}, {40.5, 20}, {45.5, 5}}; o2::monitoring::DerivedMetrics derivedHandler; + std::string name("metricDouble"); + auto timestamp = std::chrono::system_clock::now(); + + for (std::size_t i = 0; i < valResKv1.size(); i++) { + try { + Metric metric1(name, Metric::DefaultVerbosity, timestamp); + metric1.addValue(valResKv1[i].value, "value"); + + Metric metric2(name, Metric::DefaultVerbosity, timestamp); + metric2.addValue(valResKv2[i].value, "value"); + metric2.addTag(o2::monitoring::tags::Key::Subsystem, o2::monitoring::tags::Value::Readout); + + derivedHandler.process(metric1, DerivedMetricMode::RATE); + derivedHandler.process(metric2, DerivedMetricMode::RATE); + + BOOST_CHECK_CLOSE(std::get(metric1.getValues().back().second), valResKv1[i].result, 0.1); + BOOST_CHECK_CLOSE(std::get(metric2.getValues().back().second), valResKv2[i].result, 0.1); + timestamp += std::chrono::seconds(1); + } catch (MonitoringException& e) { + BOOST_CHECK_EQUAL(e.what(), std::string("Not enough values")); + } + } +} + +struct ResultsKvUint64_t { + uint64_t value; + uint64_t result; +}; + +BOOST_AUTO_TEST_CASE(testDerivedRateForUint64_t) +{ + std::vector valResKv = {{165535, 0}, {165545, 10}, {165555, 10}, {165575, 20}, {165585, 10}, {165590, 5}}; + DerivedMetrics derivedHandler; std::string name("metricUint64_t"); + auto timestamp = std::chrono::system_clock::now(); - for (auto const result : results) { + for (auto const valRes : valResKv) { try { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - o2::monitoring::Metric metric(result.value, name); + Metric metric(name, Metric::DefaultVerbosity, timestamp); + metric.addValue(valRes.value, "value"); derivedHandler.process(metric, DerivedMetricMode::RATE); BOOST_CHECK_EQUAL(metric.getName(), "metricUint64_t"); BOOST_CHECK_EQUAL(metric.getValues().back().first, "value_rate"); - BOOST_WARN_CLOSE(std::get(metric.getValues().back().second), result.rate, 1.0); + BOOST_CHECK_EQUAL(std::get(metric.getValues().back().second), valRes.result); + timestamp += std::chrono::seconds(1); } catch (MonitoringException& e) { BOOST_CHECK_EQUAL(e.what(), std::string("Not enough values")); } @@ -149,51 +182,39 @@ BOOST_AUTO_TEST_CASE(divisionByZero) BOOST_AUTO_TEST_CASE(derivedIncrementInt) { - struct IncrementResults { - int value; - int increment; - }; - std::vector results = {{10, 10}, {20, 30}, {30, 60}, {50, 110}, {60, 170}, {65, 235}, {70, 305}, {80, 385}, {90, 475}, {100, 575}}; - o2::monitoring::DerivedMetrics derivedHandler; + std::vector results = {{10, 10}, {20, 30}, {30, 60}, {50, 110}, {60, 170}, {65, 235}, {70, 305}, {80, 385}, {90, 475}, {100, 575}}; + DerivedMetrics derivedHandler; std::string name("metricInt"); for (auto const result : results) { - o2::monitoring::Metric metric(result.value, name); + Metric metric(result.value, name); derivedHandler.process(metric, DerivedMetricMode::INCREMENT); - BOOST_CHECK_EQUAL(std::get(metric.getValues().back().second), result.increment); + BOOST_CHECK_EQUAL(std::get(metric.getValues().back().second), result.result); BOOST_CHECK_EQUAL(metric.getValues().back().first, "value_increment"); } } BOOST_AUTO_TEST_CASE(derivedIncrementUint64_t) { - struct IncrementResults { - uint64_t value; - uint64_t rate; - }; - std::vector results = {{5, 5}, {165535, 165540}, {15, 165555}, {50, 165605}, {165590, 331195}}; - o2::monitoring::DerivedMetrics derivedHandler; + std::vector results = {{5, 5}, {165535, 165540}, {15, 165555}, {50, 165605}, {165590, 331195}}; + DerivedMetrics derivedHandler; std::string name("metricUint64_t"); for (auto const result : results) { - o2::monitoring::Metric metric(result.value, name); + Metric metric(result.value, name); derivedHandler.process(metric, DerivedMetricMode::INCREMENT); - BOOST_CHECK_EQUAL(std::get(metric.getValues().back().second), result.rate); + BOOST_CHECK_EQUAL(std::get(metric.getValues().back().second), result.result); BOOST_CHECK_EQUAL(metric.getValues().back().first, "value_increment"); } } BOOST_AUTO_TEST_CASE(derivedIncrementDouble) { - struct IncrementResults { - double value; - double rate; - }; - std::vector results = {{1.2, 1.2}, {11.2, 12.4}, {21.2, 33.6}, {41.2, 74.8}, {51.2, 126}, {61, 187}}; - o2::monitoring::DerivedMetrics derivedHandler; + std::vector results = {{1.2, 1.2}, {11.2, 12.4}, {21.2, 33.6}, {41.2, 74.8}, {51.2, 126}, {61, 187}}; + DerivedMetrics derivedHandler; std::string name("metricDouble"); for (auto const result : results) { - o2::monitoring::Metric metric(result.value, name); + Metric metric(result.value, name); derivedHandler.process(metric, DerivedMetricMode::INCREMENT); - BOOST_CHECK_CLOSE(std::get(metric.getValues().back().second), result.rate, 0.01); + BOOST_CHECK_CLOSE(std::get(metric.getValues().back().second), result.result, 0.01); BOOST_CHECK_EQUAL(metric.getValues().back().first, "value_increment"); } }