MetricFrameworkTests.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2021 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifdef _USE_CPPUNIT
  14. #include <cppunit/TestFixture.h>
  15. #include "unittests.hpp"
  16. #include <algorithm>
  17. #include "jmetrics.hpp"
  18. using namespace hpccMetrics;
  19. class MetricFrameworkTestSink : public MetricSink
  20. {
  21. public:
  22. explicit MetricFrameworkTestSink(const char *name) :
  23. MetricSink(name, "test") { }
  24. ~MetricFrameworkTestSink() override = default;
  25. void startCollection(MetricsReporter *_pReporter) override
  26. {
  27. pReporter = _pReporter;
  28. isCollecting = true;
  29. }
  30. void stopCollection() override
  31. {
  32. isCollecting = false;
  33. }
  34. std::vector<std::shared_ptr<IMetric>> getReportMetrics() const
  35. {
  36. return pReporter->queryMetricsForReport(name);
  37. }
  38. public:
  39. bool isCollecting = false;
  40. };
  41. class MetricFrameworkTests : public CppUnit::TestFixture
  42. {
  43. public:
  44. MetricFrameworkTests()
  45. {
  46. pTestSink = new MetricFrameworkTestSink("testsink");
  47. frameworkTestReporter.addSink(pTestSink, "testsink");
  48. }
  49. ~MetricFrameworkTests() = default;
  50. CPPUNIT_TEST_SUITE(MetricFrameworkTests);
  51. CPPUNIT_TEST(Test_counter_metric_increments_properly);
  52. CPPUNIT_TEST(Test_gauge_metric_updates_properly);
  53. CPPUNIT_TEST(Test_custom_metric);
  54. CPPUNIT_TEST(Test_reporter_calls_sink_to_start_and_stop_collection);
  55. CPPUNIT_TEST(Test_reporter_manages_metrics_properly);
  56. CPPUNIT_TEST_SUITE_END();
  57. protected:
  58. void Test_counter_metric_increments_properly()
  59. {
  60. std::shared_ptr<CounterMetric> pCounter = std::make_shared<CounterMetric>("test-counter", "description");
  61. CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(pCounter->queryValue()));
  62. //
  63. // Test default increment (by 1)
  64. pCounter->inc();
  65. int counterValue = pCounter->queryValue();
  66. CPPUNIT_ASSERT_EQUAL(1, counterValue);
  67. //
  68. // Test increment by > 1
  69. pCounter->inc(2);
  70. counterValue = pCounter->queryValue();
  71. CPPUNIT_ASSERT_EQUAL(3, counterValue);
  72. }
  73. void Test_gauge_metric_updates_properly()
  74. {
  75. std::shared_ptr<GaugeMetric> pGauge = std::make_shared<GaugeMetric>("test-gauge", "description");
  76. int gaugeValue = pGauge->queryValue();
  77. CPPUNIT_ASSERT_EQUAL(0, gaugeValue);
  78. //
  79. // Test initial setting of gauge
  80. pGauge->set(25);
  81. gaugeValue = pGauge->queryValue();
  82. CPPUNIT_ASSERT_EQUAL(25, gaugeValue);
  83. //
  84. // Test updating gauge
  85. pGauge->adjust(10);
  86. gaugeValue = pGauge->queryValue();
  87. CPPUNIT_ASSERT_EQUAL(35, gaugeValue);
  88. pGauge->adjust(-5);
  89. gaugeValue = pGauge->queryValue();
  90. CPPUNIT_ASSERT_EQUAL(30, gaugeValue);
  91. }
  92. void Test_custom_metric()
  93. {
  94. int customCounter = 0;
  95. std::shared_ptr<CustomMetric<int>> pCustomCounter = std::make_shared<CustomMetric<int>>("custom-counter", "description", METRICS_COUNTER, customCounter);
  96. int customCounterValue = pCustomCounter->queryValue();
  97. CPPUNIT_ASSERT_EQUAL(0, customCounterValue);
  98. customCounter++;
  99. customCounterValue = pCustomCounter->queryValue();
  100. CPPUNIT_ASSERT_EQUAL(1, customCounterValue);
  101. }
  102. void Test_reporter_calls_sink_to_start_and_stop_collection()
  103. {
  104. frameworkTestReporter.startCollecting();
  105. CPPUNIT_ASSERT_EQUAL(true, pTestSink->isCollecting);
  106. frameworkTestReporter.stopCollecting();
  107. CPPUNIT_ASSERT_EQUAL(false, pTestSink->isCollecting);
  108. }
  109. void Test_reporter_manages_metrics_properly()
  110. {
  111. int numAdded;
  112. std::shared_ptr<CounterMetric> pCounter = std::make_shared<CounterMetric>("test-counter", "description");
  113. std::shared_ptr<GaugeMetric> pGauge = std::make_shared<GaugeMetric>("test-gauge", "description");
  114. frameworkTestReporter.addMetric(pCounter);
  115. frameworkTestReporter.addMetric(pGauge);
  116. numAdded = 2;
  117. frameworkTestReporter.startCollecting();
  118. //
  119. // Make sure the initial list is correct
  120. int numMetrics = frameworkTestReporter.queryMetricsForReport("testsink").size();
  121. CPPUNIT_ASSERT_EQUAL(numAdded, numMetrics);
  122. //
  123. // Add a metric while reporting is enabled and make sure it is returned
  124. std::shared_ptr<CounterMetric> pNewCounter = std::make_shared<CounterMetric>("test-newcounter", "description");
  125. frameworkTestReporter.addMetric(pNewCounter);
  126. numAdded++;
  127. numMetrics = frameworkTestReporter.queryMetricsForReport("testsink").size();
  128. CPPUNIT_ASSERT_EQUAL(numAdded, numMetrics);
  129. //
  130. // Destroy a metric and ensure it is no longer in the list of report metrics
  131. pNewCounter = nullptr;
  132. numAdded--;
  133. numMetrics = frameworkTestReporter.queryMetricsForReport("testsink").size();
  134. CPPUNIT_ASSERT_EQUAL(numAdded, numMetrics);
  135. frameworkTestReporter.stopCollecting();
  136. }
  137. protected:
  138. MetricsReporter frameworkTestReporter;
  139. MetricFrameworkTestSink *pTestSink;
  140. };
  141. CPPUNIT_TEST_SUITE_REGISTRATION( MetricFrameworkTests );
  142. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MetricFrameworkTests, "MetricFrameworkTests" );
  143. #endif