aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/metrics/timer_ut.cpp
blob: e1039b06d9fc22e3836b088c282f7a7ff7f98d78 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "timer.h"

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

using namespace NMonitoring;
using namespace NThreading;

Y_UNIT_TEST_SUITE(TTimerTest) {

    using namespace std::chrono;

    struct TTestClock {
        using time_point = time_point<high_resolution_clock>;

        static time_point TimePoint;

        static time_point now() {
            return TimePoint;
        }
    };

    TTestClock::time_point TTestClock::TimePoint;


    Y_UNIT_TEST(Gauge) {
        TTestClock::TimePoint = TTestClock::time_point::min();

        TGauge gauge(0);
        {
            TMetricTimerScope<TGauge, milliseconds, TTestClock> t{&gauge};
            TTestClock::TimePoint += milliseconds(10);
        }
        UNIT_ASSERT_EQUAL(10, gauge.Get());

        {
            TMetricTimerScope<TGauge, milliseconds, TTestClock> t{&gauge};
            TTestClock::TimePoint += milliseconds(20);
        }
        UNIT_ASSERT_EQUAL(20, gauge.Get());
    }

    Y_UNIT_TEST(IntGauge) {
        TTestClock::TimePoint = TTestClock::time_point::min();

        TIntGauge gauge(0);
        {
            TMetricTimerScope<TIntGauge, milliseconds, TTestClock> t{&gauge};
            TTestClock::TimePoint += milliseconds(10);
        }
        UNIT_ASSERT_EQUAL(10, gauge.Get());

        {
            TMetricTimerScope<TIntGauge, milliseconds, TTestClock> t{&gauge};
            TTestClock::TimePoint += milliseconds(20);
        }
        UNIT_ASSERT_EQUAL(20, gauge.Get());
    }

    Y_UNIT_TEST(CounterNew) {
        TTestClock::TimePoint = TTestClock::time_point::min();

        TCounter counter(0);
        {
            TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter};
            TTestClock::TimePoint += milliseconds(10);
        }
        UNIT_ASSERT_EQUAL(10, counter.Get());

        {
            TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter};
            TTestClock::TimePoint += milliseconds(20);
        }
        UNIT_ASSERT_EQUAL(30, counter.Get());
    }

    Y_UNIT_TEST(Rate) {
        TTestClock::TimePoint = TTestClock::time_point::min();

        TRate rate(0);
        {
            TMetricTimerScope<TRate, milliseconds, TTestClock> t{&rate};
            TTestClock::TimePoint += milliseconds(10);
        }
        UNIT_ASSERT_EQUAL(10, rate.Get());

        {
            TMetricTimerScope<TRate, milliseconds, TTestClock> t{&rate};
            TTestClock::TimePoint += milliseconds(20);
        }
        UNIT_ASSERT_EQUAL(30, rate.Get());
    }

    Y_UNIT_TEST(Histogram) {
        TTestClock::TimePoint = TTestClock::time_point::min();

        auto assertHistogram = [](const TVector<ui64>& expected, IHistogramSnapshotPtr snapshot) {
            UNIT_ASSERT_EQUAL(expected.size(), snapshot->Count());
            for (size_t i = 0; i < expected.size(); ++i) {
                UNIT_ASSERT_EQUAL(expected[i], snapshot->Value(i));
            }
        };

        THistogram histogram(ExplicitHistogram({10, 20, 30}), true);
        {
            TMetricTimerScope<THistogram, milliseconds, TTestClock> t{&histogram};
            TTestClock::TimePoint += milliseconds(5);
        }
        assertHistogram({1, 0, 0, 0}, histogram.TakeSnapshot());

        {
            TMetricTimerScope<THistogram, milliseconds, TTestClock> t{&histogram};
            TTestClock::TimePoint += milliseconds(15);
        }
        assertHistogram({1, 1, 0, 0}, histogram.TakeSnapshot());
    }

    Y_UNIT_TEST(Moving) {
        TTestClock::TimePoint = TTestClock::time_point::min();

        TCounter counter(0);
        {
            TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter};
            [tt = std::move(t)] {
                TTestClock::TimePoint += milliseconds(5);
                Y_UNUSED(tt);
            }();

            TTestClock::TimePoint += milliseconds(10);
        }

        UNIT_ASSERT_EQUAL(counter.Get(), 5);
    }

    Y_UNIT_TEST(MovingIntoApply) {
        TTestClock::TimePoint = TTestClock::time_point::min();
        auto pool = CreateThreadPool(1);

        TCounter counter(0);
        {
            TFutureFriendlyTimer<TCounter, milliseconds, TTestClock> t{&counter};

            auto f = Async([=] {
                return;
            }, *pool).Apply([tt = t] (auto) {
                TTestClock::TimePoint += milliseconds(5);
                tt.Record();
            });

            f.Wait();
            TTestClock::TimePoint += milliseconds(10);
        }

        UNIT_ASSERT_EQUAL(counter.Get(), 5);
    }
}