aboutsummaryrefslogtreecommitdiffstats
path: root/metrics/src/tests/summetrictest.cpp
blob: e3d58659daf8b30ef9f4c870adf72cfeb896c76f (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/metrics/metrics.h>
#include <vespa/vespalib/gtest/gtest.h>

namespace metrics {

TEST(SumMetricTest, test_long_count_metric)
{
    MetricSet parent("parent", {}, "");
    SumMetric<LongCountMetric> sum("foo", {}, "foodesc", &parent);

    LongCountMetric v1("ff", {}, "", &parent);
    LongCountMetric v2("aa", {}, "", &parent);

    sum.addMetricToSum(v1);
    sum.addMetricToSum(v2);

        // Give them some values
    v1.inc(3);
    v2.inc(7);

        // Verify XML output. Should be in register order.
    std::string expected("foo count=10");
    EXPECT_EQ(expected, sum.toString());
    EXPECT_EQ(int64_t(10), sum.getLongValue("value"));
}

TEST(SumMetricTest, test_average_metric)
{
    MetricSet parent("parent", {}, "");
    SumMetric<LongAverageMetric> sum("foo", {}, "foodesc", &parent);

    LongAverageMetric v1("ff", {}, "", &parent);
    LongAverageMetric v2("aa", {}, "", &parent);

    sum.addMetricToSum(v1);
    sum.addMetricToSum(v2);

        // Give them some values
    v1.addValue(3);
    v2.addValue(7);

        // Verify XML output. Should be in register order.
    std::string expected("foo average=5 last=7 min=3 max=7 count=2 total=10");
    EXPECT_EQ(expected, sum.toString());
    EXPECT_EQ(int64_t(5), sum.getLongValue("value"));
    EXPECT_EQ(int64_t(3), sum.getLongValue("min"));
    EXPECT_EQ(int64_t(7), sum.getLongValue("max"));
}

TEST(SumMetricTest, test_metric_set)
{
    MetricSet parent("parent", {}, "");
    SumMetric<MetricSet> sum("foo", {}, "bar", &parent);

    MetricSet set1("a", {}, "", &parent);
    MetricSet set2("b", {}, "", &parent);
    LongValueMetric v1("c", {}, "", &set1);
    LongValueMetric v2("d", {}, "", &set2);
    LongCountMetric v3("e", {}, "", &set1);
    LongCountMetric v4("f", {}, "", &set2);

    sum.addMetricToSum(set1);
    sum.addMetricToSum(set2);

    // Give them some values
    v1.addValue(3);
    v2.addValue(7);
    v3.inc(2);
    v4.inc();

    // Verify XML output. Should be in register order.
    std::string expected("'\n"
            "foo:\n"
            "  c average=3 last=3 min=3 max=3 count=1 total=3\n"
            "  e count=2'"
    );
    EXPECT_EQ(expected, "'\n" + sum.toString() + "'");
}

TEST(SumMetricTest, test_remove)
{
    MetricSet parent("parent", {}, "");
    SumMetric<LongCountMetric> sum("foo", {}, "foodesc", &parent);

    LongCountMetric v1("ff", {}, "", &parent);
    LongCountMetric v2("aa", {}, "", &parent);
    LongCountMetric v3("zz", {}, "", &parent);

    sum.addMetricToSum(v1);
    sum.addMetricToSum(v2);
    sum.addMetricToSum(v3);

    // Give them some values
    v1.inc(3);
    v2.inc(7);
    v3.inc(10);

    EXPECT_EQ(int64_t(20), sum.getLongValue("value"));
    sum.removeMetricFromSum(v2);
    EXPECT_EQ(int64_t(13), sum.getLongValue("value"));
}

TEST(SumMetricTest, test_start_value)
{
    MetricSnapshot snapshot("active");
    SumMetric<LongValueMetric> sum("foo", {}, "foodesc",
                                   &snapshot.getMetrics());
    LongValueMetric start("start", {}, "", 0);
    start.set(50);
    sum.setStartValue(start);

    // without children
    EXPECT_EQ(int64_t(50), sum.getLongValue("value"));

    MetricSnapshot copy("copy");
    copy.recreateSnapshot(snapshot.getMetrics(), true);
    snapshot.addToSnapshot(copy, 100);

    LongValueMetric value("value", {}, "", &snapshot.getMetrics());
    sum.addMetricToSum(value);
    value.set(10);

    // with children
    EXPECT_EQ(int64_t(60), sum.getLongValue("value"));
}

}