aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/metrics/summary_collector_ut.cpp
blob: 191929550f73a83216cd999dc39849adfc3393bb (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
#include "summary_collector.h"

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

#include <util/random/random.h>

#include <numeric>
#include <algorithm>

namespace NMonitoring {

Y_UNIT_TEST_SUITE(SummaryCollectorTest) {

    void CheckSnapshot(ISummaryDoubleSnapshotPtr snapshot, const TVector<double> values) {
        const double eps = 1e-9;

        double sum = std::accumulate(values.begin(), values.end(), 0.0);
        double min = *std::min_element(values.begin(), values.end());
        double max = *std::max_element(values.begin(), values.end());
        double last = values.back();
        ui64 count = values.size();

        UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetSum(), sum, eps);
        UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetMin(), min, eps);
        UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetMax(), max, eps);
        UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetLast(), last, eps);
        UNIT_ASSERT_EQUAL(snapshot->GetCount(), count);
    }

    Y_UNIT_TEST(Simple) {
        {
            TVector<double> test{05, -1.5, 0.0, 2.5, 0.25, -1.0};
            TSummaryDoubleCollector summary;
            for (auto value : test) {
                summary.Collect(value);
            }
            CheckSnapshot(summary.Snapshot(), test);
        }
        {
            TVector<double> test{-1.0, 1.0, 9.0, -5000.0, 5000.0, 5.0, -5.0};
            TSummaryDoubleCollector summary;
            for (auto value : test) {
                summary.Collect(value);
            }
            CheckSnapshot(summary.Snapshot(), test);
        }
    }

    Y_UNIT_TEST(RandomStressTest) {
        const ui32 attemts = 100;
        for (ui32 i = 0; i < attemts; ++i) {
            const ui32 size = 100;
            TVector<double> values(size);
            TSummaryDoubleCollector summary;
            for (auto& value : values) {
                value = RandomNumber<double>() - 0.5;
                summary.Collect(value);
            }
            CheckSnapshot(summary.Snapshot(), values);
        }
    }
}

}