summaryrefslogtreecommitdiffstats
path: root/metrics/src/test/java/com/yahoo/metrics/ValueMetricTest.java
blob: 4758888b0c893ddb9e1fd40ccd6f166166c03353 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.metrics;

import junit.framework.TestCase;

public class ValueMetricTest extends TestCase {
    public void testAveragedDoubleValueMetric()
    {
        AveragedDoubleValueMetric m = new AveragedDoubleValueMetric("test", "tag", "description", null);
        m.addValue(100.0);
        assertEquals("count=\"1\" min=\"100.00\" max=\"100.00\" last=\"100.00\" total=\"100.00\" average=\"100.00\"", m.toString());
        m.addValue(100.0);
        assertEquals("count=\"2\" min=\"100.00\" max=\"100.00\" last=\"100.00\" total=\"200.00\" average=\"100.00\"", m.toString());
        m.addValue(40.0);
        assertEquals("count=\"3\" min=\"40.00\" max=\"100.00\" last=\"40.00\" total=\"240.00\" average=\"80.00\"", m.toString());

        AveragedDoubleValueMetric m2 = new AveragedDoubleValueMetric(m, Metric.CopyType.CLONE, null);
        assertEquals("count=\"3\" min=\"40.00\" max=\"100.00\" last=\"40.00\" total=\"240.00\" average=\"80.00\"", m.toString());
        assertEquals("count=\"3\" min=\"40.00\" max=\"100.00\" last=\"40.00\" total=\"240.00\" average=\"80.00\"", m2.toString());
        m.reset();
        assertEquals("count=\"0\" min=\"0\" max=\"0\" last=\"0\" total=\"0\" average=\"0.00\"", m.toString());
        assertEquals("count=\"3\" min=\"40.00\" max=\"100.00\" last=\"40.00\" total=\"240.00\" average=\"80.00\"", m2.toString());

        AveragedDoubleValueMetric m3 = new AveragedDoubleValueMetric("test", "tag", "description", null);
        m3.addValue(200.0);
        m3.addValue(100.0);
        m3.addValue(400.0);

        AveragedDoubleValueMetric m4 = new AveragedDoubleValueMetric("test", "tag", "description", null);
        m3.addValue(50.0);
        m3.addValue(100.0);
        m3.addValue(2000.0);

        AveragedDoubleValueMetric sum = new AveragedDoubleValueMetric(m2, Metric.CopyType.INACTIVE, null);
        m3.addToPart(sum);
        m4.addToPart(sum);
        assertEquals("count=\"9\" min=\"40.00\" max=\"2000.00\" last=\"2000.00\" total=\"3090.00\" average=\"343.33\"", sum.toString());

        AveragedDoubleValueMetric snapshot = new AveragedDoubleValueMetric(m2, Metric.CopyType.INACTIVE, null);
        m3.addToSnapshot(snapshot);
        m4.addToSnapshot(snapshot);
        assertEquals("count=\"9\" min=\"40.00\" max=\"2000.00\" last=\"2000.00\" total=\"3090.00\" average=\"343.33\"", snapshot.toString());

        assertEquals("<test description=\"description\" average=\"343.33\" last=\"2000.00\" min=\"40.00\" max=\"2000.00\" count=\"9\" total=\"3090.00\"/>\n", sum.toXml(0, 2));

        assertEquals(80.0, m2.getDoubleValue("value"));
        assertEquals(80.0, m2.getDoubleValue("average"));
        assertEquals(40.0, m2.getDoubleValue("min"));
        assertEquals(100.0, m2.getDoubleValue("max"));
        assertEquals(40.0, m2.getDoubleValue("last"));
        assertEquals(3.0, m2.getDoubleValue("count"));
        assertEquals(240.0, m2.getDoubleValue("total"));

        assertEquals(80, m2.getLongValue("value"));
        assertEquals(80, m2.getLongValue("average"));
        assertEquals(40, m2.getLongValue("min"));
        assertEquals(100, m2.getLongValue("max"));
        assertEquals(40, m2.getLongValue("last"));
        assertEquals(3, m2.getLongValue("count"));
        assertEquals(240, m2.getLongValue("total"));
    }

    public void testDoubleValueMetricNotUpdatedOnNaN() {
        AveragedDoubleValueMetric m = new AveragedDoubleValueMetric("test", "tag", "description", null);
        m.addValue(Double.NaN);
        assertEquals("count=\"0\" min=\"0\" max=\"0\" last=\"0\" total=\"0\" average=\"0.00\"", m.toString());
    }

    public void testDoubleValueMetricNotUpdatedOnInfinity() {
        AveragedDoubleValueMetric m = new AveragedDoubleValueMetric("test", "tag", "description", null);
        m.addValue(Double.POSITIVE_INFINITY);
        assertEquals("count=\"0\" min=\"0\" max=\"0\" last=\"0\" total=\"0\" average=\"0.00\"", m.toString());
    }

    public void testSummedDoubleValueMetric()
    {
        SummedDoubleValueMetric m = new SummedDoubleValueMetric("test", "tag", "description", null);
        m.addValue(100.0);
        assertEquals("count=\"1\" min=\"100.00\" max=\"100.00\" last=\"100.00\" total=\"100.00\" average=\"100.00\"", m.toString());
        m.addValue(100.0);
        assertEquals("count=\"2\" min=\"100.00\" max=\"100.00\" last=\"100.00\" total=\"200.00\" average=\"100.00\"", m.toString());
        m.addValue(40.0);
        assertEquals("count=\"3\" min=\"40.00\" max=\"100.00\" last=\"40.00\" total=\"240.00\" average=\"80.00\"", m.toString());

        SummedDoubleValueMetric m2 = new SummedDoubleValueMetric(m, Metric.CopyType.CLONE, null);
        assertEquals("count=\"3\" min=\"40.00\" max=\"100.00\" last=\"40.00\" total=\"240.00\" average=\"80.00\"", m.toString());
        assertEquals("count=\"3\" min=\"40.00\" max=\"100.00\" last=\"40.00\" total=\"240.00\" average=\"80.00\"", m2.toString());
        m.reset();
        assertEquals("count=\"0\" min=\"0\" max=\"0\" last=\"0\" total=\"0\" average=\"0.00\"", m.toString());
        assertEquals("count=\"3\" min=\"40.00\" max=\"100.00\" last=\"40.00\" total=\"240.00\" average=\"80.00\"", m2.toString());

        SummedDoubleValueMetric m3 = new SummedDoubleValueMetric("test", "tag", "description", null);
        m3.addValue(200.0);
        m3.addValue(100.0);
        m3.addValue(400.0);

        SummedDoubleValueMetric m4 = new SummedDoubleValueMetric("test", "tag", "description", null);
        m4.addValue(2000.0);

        SummedDoubleValueMetric sum = new SummedDoubleValueMetric(m2, Metric.CopyType.INACTIVE, null);
        m3.addToPart(sum);
        m4.addToPart(sum);
        assertEquals("count=\"7\" min=\"40.00\" max=\"2000.00\" last=\"2440.00\" total=\"16193.33\" average=\"2313.33\"", sum.toString());

        SummedDoubleValueMetric snapshot = new SummedDoubleValueMetric(m2, Metric.CopyType.INACTIVE, null);
        m3.addToSnapshot(snapshot);
        m4.addToSnapshot(snapshot);
        assertEquals("count=\"7\" min=\"40.00\" max=\"2000.00\" last=\"2000.00\" total=\"2940.00\" average=\"420.00\"", snapshot.toString());

        assertEquals("<test description=\"description\" average=\"2313.33\" last=\"2440.00\" min=\"40.00\" max=\"2000.00\" count=\"7\" total=\"16193.33\"/>\n", sum.toXml(0, 2));

        assertEquals(40.0, m2.getDoubleValue("value"));
        assertEquals(80.0, m2.getDoubleValue("average"));
        assertEquals(40.0, m2.getDoubleValue("min"));
        assertEquals(100.0, m2.getDoubleValue("max"));
        assertEquals(40.0, m2.getDoubleValue("last"));
        assertEquals(3.0, m2.getDoubleValue("count"));
        assertEquals(240.0, m2.getDoubleValue("total"));

        assertEquals(40, m2.getLongValue("value"));
        assertEquals(80, m2.getLongValue("average"));
        assertEquals(40, m2.getLongValue("min"));
        assertEquals(100, m2.getLongValue("max"));
        assertEquals(40, m2.getLongValue("last"));
        assertEquals(3, m2.getLongValue("count"));
        assertEquals(240, m2.getLongValue("total"));
    }

    public void testAveragedLongValueMetric()
    {
        AveragedLongValueMetric m = new AveragedLongValueMetric("test", "tag", "description", null);

        assertEquals(0l, m.getLongValue("max"));
        assertEquals(0l, m.getLongValue("min"));

        m.addValue((long)100);
        assertEquals("count=\"1\" min=\"100\" max=\"100\" last=\"100\" total=\"100\" average=\"100.00\"", m.toString());
        m.addValue((long)100);
        assertEquals("count=\"2\" min=\"100\" max=\"100\" last=\"100\" total=\"200\" average=\"100.00\"", m.toString());
        m.addValue((long)40);
        assertEquals("count=\"3\" min=\"40\" max=\"100\" last=\"40\" total=\"240\" average=\"80.00\"", m.toString());

        AveragedLongValueMetric m2 = new AveragedLongValueMetric(m, Metric.CopyType.CLONE, null);
        assertEquals("count=\"3\" min=\"40\" max=\"100\" last=\"40\" total=\"240\" average=\"80.00\"", m.toString());
        assertEquals("count=\"3\" min=\"40\" max=\"100\" last=\"40\" total=\"240\" average=\"80.00\"", m2.toString());
        m.reset();
        assertEquals("count=\"0\" min=\"0\" max=\"0\" last=\"0\" total=\"0\" average=\"0.00\"", m.toString());
        assertEquals("count=\"3\" min=\"40\" max=\"100\" last=\"40\" total=\"240\" average=\"80.00\"", m2.toString());

        AveragedLongValueMetric m3 = new AveragedLongValueMetric("test", "tag", "description", null);
        m3.addValue((long)200);
        m3.addValue((long)100);
        m3.addValue((long)400);

        AveragedLongValueMetric m4 = new AveragedLongValueMetric("test", "tag", "description", null);
        m3.addValue((long)50);
        m3.addValue((long)100);
        m3.addValue((long)2000);

        AveragedLongValueMetric sum = new AveragedLongValueMetric(m2, Metric.CopyType.INACTIVE, null);
        m3.addToPart(sum);
        m4.addToPart(sum);
        assertEquals("count=\"9\" min=\"40\" max=\"2000\" last=\"2000\" total=\"3090\" average=\"343.33\"", sum.toString());

        AveragedLongValueMetric snapshot = new AveragedLongValueMetric(m2, Metric.CopyType.INACTIVE, null);
        m3.addToSnapshot(snapshot);
        m4.addToSnapshot(snapshot);
        assertEquals("count=\"9\" min=\"40\" max=\"2000\" last=\"2000\" total=\"3090\" average=\"343.33\"", snapshot.toString());

        assertEquals("<test description=\"description\" average=\"343.33\" last=\"2000\" min=\"40\" max=\"2000\" count=\"9\" total=\"3090\"/>\n", sum.toXml(0, 2));

        assertEquals(80, m2.getLongValue("value"));
        assertEquals(80, m2.getLongValue("average"));
        assertEquals(40, m2.getLongValue("min"));
        assertEquals(100, m2.getLongValue("max"));
        assertEquals(40, m2.getLongValue("last"));
        assertEquals(3, m2.getLongValue("count"));
        assertEquals(240, m2.getLongValue("total"));
    }


}