summary_collector_ut.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "summary_collector.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/random/random.h>
  4. #include <numeric>
  5. #include <algorithm>
  6. namespace NMonitoring {
  7. Y_UNIT_TEST_SUITE(SummaryCollectorTest) {
  8. void CheckSnapshot(ISummaryDoubleSnapshotPtr snapshot, const TVector<double> values) {
  9. const double eps = 1e-9;
  10. double sum = std::accumulate(values.begin(), values.end(), 0.0);
  11. double min = *std::min_element(values.begin(), values.end());
  12. double max = *std::max_element(values.begin(), values.end());
  13. double last = values.back();
  14. ui64 count = values.size();
  15. UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetSum(), sum, eps);
  16. UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetMin(), min, eps);
  17. UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetMax(), max, eps);
  18. UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetLast(), last, eps);
  19. UNIT_ASSERT_EQUAL(snapshot->GetCount(), count);
  20. }
  21. Y_UNIT_TEST(Simple) {
  22. {
  23. TVector<double> test{05, -1.5, 0.0, 2.5, 0.25, -1.0};
  24. TSummaryDoubleCollector summary;
  25. for (auto value : test) {
  26. summary.Collect(value);
  27. }
  28. CheckSnapshot(summary.Snapshot(), test);
  29. }
  30. {
  31. TVector<double> test{-1.0, 1.0, 9.0, -5000.0, 5000.0, 5.0, -5.0};
  32. TSummaryDoubleCollector summary;
  33. for (auto value : test) {
  34. summary.Collect(value);
  35. }
  36. CheckSnapshot(summary.Snapshot(), test);
  37. }
  38. }
  39. Y_UNIT_TEST(RandomStressTest) {
  40. const ui32 attemts = 100;
  41. for (ui32 i = 0; i < attemts; ++i) {
  42. const ui32 size = 100;
  43. TVector<double> values(size);
  44. TSummaryDoubleCollector summary;
  45. for (auto& value : values) {
  46. value = RandomNumber<double>() - 0.5;
  47. summary.Collect(value);
  48. }
  49. CheckSnapshot(summary.Snapshot(), values);
  50. }
  51. }
  52. }
  53. }