aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/counters/timer_ut.cpp
blob: e29b8e22b4d9d7fedb5f574648a2faf67df04f7a (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
#include "timer.h"

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

using namespace NMonitoring;
using namespace std::literals::chrono_literals;

class TCallback {
public:
    explicit TCallback(int value)
        : Value_(value){};
    void operator()(std::chrono::high_resolution_clock::duration duration) {
        Value_ = duration.count();
    };

    int Value_;
};

Y_UNIT_TEST_SUITE(TTimerTest) {
    Y_UNIT_TEST(RecordValue) {
        TTimerNs timerNs(1ns, 1s);
        UNIT_ASSERT(timerNs.RecordValue(10us));

        TTimerUs timerUs(1us, 1s);
        UNIT_ASSERT(timerUs.RecordValue(10us));

        THistogramSnapshot snapshot;
        timerNs.TakeSnapshot(&snapshot);
        UNIT_ASSERT_EQUAL(snapshot.Min, 10000);
        UNIT_ASSERT_EQUAL(snapshot.Max, 10007);
        UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6);

        timerUs.TakeSnapshot(&snapshot);
        UNIT_ASSERT_EQUAL(snapshot.Min, 10);
        UNIT_ASSERT_EQUAL(snapshot.Max, 10);
        UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6);
    }

    Y_UNIT_TEST(Measure) {
        TTimerNs timer(1ns, 1s);
        timer.Measure([]() {
            Sleep(TDuration::MilliSeconds(1));
        });
        THistogramSnapshot snapshot;
        timer.TakeSnapshot(&snapshot);

        UNIT_ASSERT(snapshot.Min > std::chrono::nanoseconds(1ms).count());
        UNIT_ASSERT(snapshot.Max > std::chrono::nanoseconds(1ms).count());
        UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6);
    }

    Y_UNIT_TEST(TimerScope) {
        TTimerUs timer(1us, 1000s);
        {
            TTimerScope<TTimerUs> scope(&timer);
            Sleep(TDuration::MilliSeconds(10));
        }
        THistogramSnapshot snapshot;
        timer.TakeSnapshot(&snapshot);

        UNIT_ASSERT(snapshot.Min > std::chrono::microseconds(10ms).count());
        UNIT_ASSERT(snapshot.Max > std::chrono::microseconds(10ms).count());
        UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6);
    }

    Y_UNIT_TEST(TimerScopeWithCallback) {
        TCallback callback(0);
        TTimerUs timer(1us, 1000s);
        {
            TTimerScope<TTimerUs, TCallback> scope(&timer, &callback);
            Sleep(TDuration::MilliSeconds(10));
        }
        THistogramSnapshot snapshot;
        timer.TakeSnapshot(&snapshot);

        UNIT_ASSERT(snapshot.Min > std::chrono::microseconds(10ms).count());
        UNIT_ASSERT(snapshot.Max > std::chrono::microseconds(10ms).count());
        UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6);
        UNIT_ASSERT(callback.Value_ > std::chrono::microseconds(10ms).count());
    }
}