ewma_ut.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "ewma.h"
  2. #include "metric.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. using namespace NMonitoring;
  5. const auto EPS = 1e-6;
  6. void ElapseMinute(IExpMovingAverage& ewma) {
  7. for (auto i = 0; i < 12; ++i) {
  8. ewma.Tick();
  9. }
  10. }
  11. Y_UNIT_TEST_SUITE(TEwmaTest) {
  12. Y_UNIT_TEST(OneMinute) {
  13. TGauge gauge;
  14. auto ewma = OneMinuteEwma(&gauge);
  15. ewma->Update(3);
  16. ewma->Tick();
  17. TVector<double> expectedValues {
  18. 0.6,
  19. 0.22072766,
  20. 0.08120117,
  21. 0.02987224,
  22. 0.01098938,
  23. 0.00404277,
  24. 0.00148725,
  25. 0.00054713,
  26. 0.00020128,
  27. 0.00007405,
  28. 0.00002724,
  29. 0.00001002,
  30. 0.00000369,
  31. 0.00000136,
  32. 0.00000050,
  33. 0.00000018,
  34. };
  35. for (auto expectedValue : expectedValues) {
  36. UNIT_ASSERT_DOUBLES_EQUAL(ewma->Rate(), expectedValue, EPS);
  37. ElapseMinute(*ewma);
  38. }
  39. }
  40. Y_UNIT_TEST(FiveMinutes) {
  41. TGauge gauge;
  42. auto ewma = FiveMinuteEwma(&gauge);
  43. ewma->Update(3);
  44. ewma->Tick();
  45. TVector<double> expectedValues {
  46. 0.6,
  47. 0.49123845,
  48. 0.40219203,
  49. 0.32928698,
  50. 0.26959738,
  51. 0.22072766,
  52. 0.18071653,
  53. 0.14795818,
  54. 0.12113791,
  55. 0.09917933,
  56. 0.08120117,
  57. 0.06648190,
  58. 0.05443077,
  59. 0.04456415,
  60. 0.03648604,
  61. 0.02987224,
  62. };
  63. for (auto expectedValue : expectedValues) {
  64. UNIT_ASSERT_DOUBLES_EQUAL(ewma->Rate(), expectedValue, EPS);
  65. ElapseMinute(*ewma);
  66. }
  67. }
  68. Y_UNIT_TEST(FiveteenMinutes) {
  69. TGauge gauge;
  70. auto ewma = FiveteenMinuteEwma(&gauge);
  71. ewma->Update(3);
  72. ewma->Tick();
  73. TVector<double> expectedValues {
  74. 0.6,
  75. 0.56130419,
  76. 0.52510399,
  77. 0.49123845,
  78. 0.45955700,
  79. 0.42991879,
  80. 0.40219203,
  81. 0.37625345,
  82. 0.35198773,
  83. 0.32928698,
  84. 0.30805027,
  85. 0.28818318,
  86. 0.26959738,
  87. 0.25221023,
  88. 0.23594443,
  89. 0.22072766,
  90. };
  91. for (auto expectedValue : expectedValues) {
  92. UNIT_ASSERT_DOUBLES_EQUAL(ewma->Rate(), expectedValue, EPS);
  93. ElapseMinute(*ewma);
  94. }
  95. }
  96. }