Skip to content

Commit

Permalink
[OMON-443] Make derived metric tests deterministic (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn committed Apr 13, 2021
1 parent f1f5062 commit 10c9602
Showing 1 changed file with 93 additions and 72 deletions.
165 changes: 93 additions & 72 deletions test/testDerived.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "../src/VariantVisitorAdd.h"
#include "../src/VariantVisitorRate.h"
#include <chrono>
#include <thread>
#include <vector>
#include <iostream>

Expand All @@ -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<RateResults> 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<ResultsKv> 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<double>(metric.getValues().back().second), result.rate, 1.0);
BOOST_CHECK_EQUAL(std::get<double>(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<RateResults> 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<ResultsKv> 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<double>(metric.getValues().back().second), result.rate, 1.0);
BOOST_CHECK_EQUAL(std::get<double>(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<RateResults> results = {{1.2, 0}, {11.2, 100}, {21.2, 100}, {41.2, 200}, {51.2, 100}, {61, 98}};
std::vector<RateResults> resultsTagged = {{0.5, 0}, {5.5, 50}, {10.5, 50}, {20.5, 100}, {40.5, 200}, {45.5, 50}};
std::vector<ResultsKvDouble> 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<double>(metric.getValues().back().second), results[i].rate, 1.0);
BOOST_CHECK_CLOSE(std::get<double>(metric.getValues().back().second), valRes.result, 0.1);
BOOST_CHECK_EQUAL(metric.getValues().back().first, "value_rate");
BOOST_WARN_CLOSE(std::get<double>(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<RateResults> results = {{165535, 0}, {165545, 100}, {165555, 100}, {165575, 200}, {165585, 100}, {165590, 50}};
std::vector<ResultsKvDouble> valResKv1 = {{1.2, 0}, {11.2, 10}, {21.2, 10}, {41.2, 20}, {51.2, 10}, {61, 9.8}};
std::vector<ResultsKvDouble> 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<double>(metric1.getValues().back().second), valResKv1[i].result, 0.1);
BOOST_CHECK_CLOSE(std::get<double>(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<ResultsKvUint64_t> 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<double>(metric.getValues().back().second), result.rate, 1.0);
BOOST_CHECK_EQUAL(std::get<double>(metric.getValues().back().second), valRes.result);
timestamp += std::chrono::seconds(1);
} catch (MonitoringException& e) {
BOOST_CHECK_EQUAL(e.what(), std::string("Not enough values"));
}
Expand Down Expand Up @@ -149,51 +182,39 @@ BOOST_AUTO_TEST_CASE(divisionByZero)

BOOST_AUTO_TEST_CASE(derivedIncrementInt)
{
struct IncrementResults {
int value;
int increment;
};
std::vector<IncrementResults> 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<ResultsKv> 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<int>(metric.getValues().back().second), result.increment);
BOOST_CHECK_EQUAL(std::get<int>(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<IncrementResults> results = {{5, 5}, {165535, 165540}, {15, 165555}, {50, 165605}, {165590, 331195}};
o2::monitoring::DerivedMetrics derivedHandler;
std::vector<ResultsKvUint64_t> 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<uint64_t>(metric.getValues().back().second), result.rate);
BOOST_CHECK_EQUAL(std::get<uint64_t>(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<IncrementResults> 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<ResultsKvDouble> 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<double>(metric.getValues().back().second), result.rate, 0.01);
BOOST_CHECK_CLOSE(std::get<double>(metric.getValues().back().second), result.result, 0.01);
BOOST_CHECK_EQUAL(metric.getValues().back().first, "value_increment");
}
}
Expand Down

0 comments on commit 10c9602

Please sign in to comment.