aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/metrics/ewma_ut.cpp
blob: 01ef2478f78ced574b51f795601781aa9d1b898d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "ewma.h"
#include "metric.h"

#include <library/cpp/testing/unittest/registar.h>


using namespace NMonitoring;

const auto EPS = 1e-6;
void ElapseMinute(IExpMovingAverage& ewma) {
    for (auto i = 0; i < 12; ++i) {
        ewma.Tick();
    }
}

Y_UNIT_TEST_SUITE(TEwmaTest) {
    Y_UNIT_TEST(OneMinute) {
        TGauge gauge;

        auto ewma = OneMinuteEwma(&gauge);
        ewma->Update(3);
        ewma->Tick();

        TVector<double> expectedValues {
            0.6,
            0.22072766,
            0.08120117,
            0.02987224,
            0.01098938,
            0.00404277,
            0.00148725,
            0.00054713,
            0.00020128,
            0.00007405,
            0.00002724,
            0.00001002,
            0.00000369,
            0.00000136,
            0.00000050,
            0.00000018,
        };

        for (auto expectedValue : expectedValues) {
            UNIT_ASSERT_DOUBLES_EQUAL(ewma->Rate(), expectedValue, EPS);
            ElapseMinute(*ewma);
        }
    }

    Y_UNIT_TEST(FiveMinutes) {
        TGauge gauge;

        auto ewma = FiveMinuteEwma(&gauge);
        ewma->Update(3);
        ewma->Tick();

        TVector<double> expectedValues {
            0.6,
            0.49123845,
            0.40219203,
            0.32928698,
            0.26959738,
            0.22072766,
            0.18071653,
            0.14795818,
            0.12113791,
            0.09917933,
            0.08120117,
            0.06648190,
            0.05443077,
            0.04456415,
            0.03648604,
            0.02987224,
        };

        for (auto expectedValue : expectedValues) {
            UNIT_ASSERT_DOUBLES_EQUAL(ewma->Rate(), expectedValue, EPS);
            ElapseMinute(*ewma);
        }
    }

    Y_UNIT_TEST(FiveteenMinutes) {
        TGauge gauge;

        auto ewma = FiveteenMinuteEwma(&gauge);
        ewma->Update(3);
        ewma->Tick();

        TVector<double> expectedValues {
            0.6,
            0.56130419,
            0.52510399,
            0.49123845,
            0.45955700,
            0.42991879,
            0.40219203,
            0.37625345,
            0.35198773,
            0.32928698,
            0.30805027,
            0.28818318,
            0.26959738,
            0.25221023,
            0.23594443,
            0.22072766,
        };

        for (auto expectedValue : expectedValues) {
            UNIT_ASSERT_DOUBLES_EQUAL(ewma->Rate(), expectedValue, EPS);
            ElapseMinute(*ewma);
        }
    }
};