timer_ut.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "timer.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <library/cpp/threading/future/async.h>
  4. #include <library/cpp/threading/future/future.h>
  5. using namespace NMonitoring;
  6. using namespace NThreading;
  7. Y_UNIT_TEST_SUITE(TTimerTest) {
  8. using namespace std::chrono;
  9. struct TTestClock {
  10. using time_point = time_point<high_resolution_clock>;
  11. static time_point TimePoint;
  12. static time_point now() {
  13. return TimePoint;
  14. }
  15. };
  16. TTestClock::time_point TTestClock::TimePoint;
  17. Y_UNIT_TEST(Gauge) {
  18. TTestClock::TimePoint = TTestClock::time_point::min();
  19. TGauge gauge(0);
  20. {
  21. TMetricTimerScope<TGauge, milliseconds, TTestClock> t{&gauge};
  22. TTestClock::TimePoint += milliseconds(10);
  23. }
  24. UNIT_ASSERT_EQUAL(10, gauge.Get());
  25. {
  26. TMetricTimerScope<TGauge, milliseconds, TTestClock> t{&gauge};
  27. TTestClock::TimePoint += milliseconds(20);
  28. }
  29. UNIT_ASSERT_EQUAL(20, gauge.Get());
  30. }
  31. Y_UNIT_TEST(IntGauge) {
  32. TTestClock::TimePoint = TTestClock::time_point::min();
  33. TIntGauge gauge(0);
  34. {
  35. TMetricTimerScope<TIntGauge, milliseconds, TTestClock> t{&gauge};
  36. TTestClock::TimePoint += milliseconds(10);
  37. }
  38. UNIT_ASSERT_EQUAL(10, gauge.Get());
  39. {
  40. TMetricTimerScope<TIntGauge, milliseconds, TTestClock> t{&gauge};
  41. TTestClock::TimePoint += milliseconds(20);
  42. }
  43. UNIT_ASSERT_EQUAL(20, gauge.Get());
  44. }
  45. Y_UNIT_TEST(CounterNew) {
  46. TTestClock::TimePoint = TTestClock::time_point::min();
  47. TCounter counter(0);
  48. {
  49. TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter};
  50. TTestClock::TimePoint += milliseconds(10);
  51. }
  52. UNIT_ASSERT_EQUAL(10, counter.Get());
  53. {
  54. TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter};
  55. TTestClock::TimePoint += milliseconds(20);
  56. }
  57. UNIT_ASSERT_EQUAL(30, counter.Get());
  58. }
  59. Y_UNIT_TEST(Rate) {
  60. TTestClock::TimePoint = TTestClock::time_point::min();
  61. TRate rate(0);
  62. {
  63. TMetricTimerScope<TRate, milliseconds, TTestClock> t{&rate};
  64. TTestClock::TimePoint += milliseconds(10);
  65. }
  66. UNIT_ASSERT_EQUAL(10, rate.Get());
  67. {
  68. TMetricTimerScope<TRate, milliseconds, TTestClock> t{&rate};
  69. TTestClock::TimePoint += milliseconds(20);
  70. }
  71. UNIT_ASSERT_EQUAL(30, rate.Get());
  72. }
  73. Y_UNIT_TEST(Histogram) {
  74. TTestClock::TimePoint = TTestClock::time_point::min();
  75. auto assertHistogram = [](const TVector<ui64>& expected, IHistogramSnapshotPtr snapshot) {
  76. UNIT_ASSERT_EQUAL(expected.size(), snapshot->Count());
  77. for (size_t i = 0; i < expected.size(); ++i) {
  78. UNIT_ASSERT_EQUAL(expected[i], snapshot->Value(i));
  79. }
  80. };
  81. THistogram histogram(ExplicitHistogram({10, 20, 30}), true);
  82. {
  83. TMetricTimerScope<THistogram, milliseconds, TTestClock> t{&histogram};
  84. TTestClock::TimePoint += milliseconds(5);
  85. }
  86. assertHistogram({1, 0, 0, 0}, histogram.TakeSnapshot());
  87. {
  88. TMetricTimerScope<THistogram, milliseconds, TTestClock> t{&histogram};
  89. TTestClock::TimePoint += milliseconds(15);
  90. }
  91. assertHistogram({1, 1, 0, 0}, histogram.TakeSnapshot());
  92. }
  93. Y_UNIT_TEST(Moving) {
  94. TTestClock::TimePoint = TTestClock::time_point::min();
  95. TCounter counter(0);
  96. {
  97. TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter};
  98. [tt = std::move(t)] {
  99. TTestClock::TimePoint += milliseconds(5);
  100. Y_UNUSED(tt);
  101. }();
  102. TTestClock::TimePoint += milliseconds(10);
  103. }
  104. UNIT_ASSERT_EQUAL(counter.Get(), 5);
  105. }
  106. Y_UNIT_TEST(MovingIntoApply) {
  107. TTestClock::TimePoint = TTestClock::time_point::min();
  108. auto pool = CreateThreadPool(1);
  109. TCounter counter(0);
  110. {
  111. TFutureFriendlyTimer<TCounter, milliseconds, TTestClock> t{&counter};
  112. auto f = Async([=] {
  113. return;
  114. }, *pool).Apply([tt = t] (auto) {
  115. TTestClock::TimePoint += milliseconds(5);
  116. tt.Record();
  117. });
  118. f.Wait();
  119. TTestClock::TimePoint += milliseconds(10);
  120. }
  121. UNIT_ASSERT_EQUAL(counter.Get(), 5);
  122. }
  123. }