2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
29 var mCGroupVect map[string]CounterVec
30 var mGGroupVect map[string]GaugeVec
32 func TestMetricSetup(t *testing.T) {
33 mCVect = Metric.RegisterCounterVec(CounterOpts{Name: "counter1", Help: "counter1"}, []string{"name", "event"}, "SUBSYSTEM0")
35 mCGroupVect = Metric.RegisterCounterVecGroup(
37 {Name: "counter1", Help: "counter1"},
39 []string{"name", "event"},
42 mGVect = Metric.RegisterGaugeVec(CounterOpts{Name: "gauge1", Help: "gauge1"}, []string{"name", "event"}, "SUBSYSTEM0")
44 mGGroupVect = Metric.RegisterGaugeVecGroup(
46 {Name: "gauge1", Help: "gauge1"},
48 []string{"name", "event"},
51 tmpCVect := Metric.RegisterCounterVec(CounterOpts{Name: "counter1", Help: "counter1"}, []string{"name", "event"}, "SUBSYSTEM0")
53 if tmpCVect.Vec != mCVect.Vec {
54 t.Errorf("tmpCVect not same than mCVect. cache not working?")
57 tmpGVect := Metric.RegisterGaugeVec(CounterOpts{Name: "gauge1", Help: "gauge1"}, []string{"name", "event"}, "SUBSYSTEM0")
59 if tmpGVect.Vec != mGVect.Vec {
60 t.Errorf("tmpGVect not same than mGVect. cache not working?")
63 Metric.RegisterCounterVec(CounterOpts{Name: "counter1", Help: "counter1"}, []string{"name", "eventMismatch"}, "SUBSYSTEM0")
64 Metric.RegisterGaugeVec(CounterOpts{Name: "gauge1", Help: "gauge1"}, []string{"name", "eventMismatch"}, "SUBSYSTEM0")
68 func TestMetricCounter(t *testing.T) {
69 TestCounterOpt := CounterOpts{Name: "CounterBlaah1", Help: "CounterBlaah1"}
70 ret1 := Metric.RegisterCounter(TestCounterOpt, "TestMetricCounter")
72 ret2 := Metric.RegisterCounter(TestCounterOpt, "TestMetricCounter")
75 t.Errorf("ret1 not same than ret2. cache not working?")
79 func TestMetricCounterGroup(t *testing.T) {
80 var TestCounterOpts = []CounterOpts{
81 {Name: "CounterBlaah1", Help: "CounterBlaah1"},
82 {Name: "CounterBlaah2", Help: "CounterBlaah2"},
83 {Name: "CounterBlaah3", Help: "CounterBlaah3"},
84 {Name: "CounterBlaah4", Help: "CounterBlaah4"},
87 ret1 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounterGroup")
90 t.Errorf("ret1 counter group is empty")
93 ret1["CounterBlaah1"].Inc()
94 ret1["CounterBlaah2"].Inc()
95 ret1["CounterBlaah3"].Inc()
96 ret1["CounterBlaah4"].Inc()
98 ret2 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounterGroup")
101 t.Errorf("ret2 counter group is empty")
104 ret2["CounterBlaah1"].Inc()
105 ret2["CounterBlaah2"].Inc()
106 ret2["CounterBlaah3"].Inc()
107 ret2["CounterBlaah4"].Inc()
109 if len(ret1) != len(ret2) {
110 t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2))
114 func TestMetricGauge(t *testing.T) {
115 TestGaugeOpts := CounterOpts{Name: "GaugeBlaah1", Help: "GaugeBlaah1"}
116 ret1 := Metric.RegisterGauge(TestGaugeOpts, "TestMetricGauge")
118 ret2 := Metric.RegisterGauge(TestGaugeOpts, "TestMetricGauge")
121 t.Errorf("ret1 not same than ret2. cache not working?")
125 func TestMetricGaugeGroup(t *testing.T) {
126 var TestGaugeOpts = []CounterOpts{
127 {Name: "GaugeBlaah1", Help: "GaugeBlaah1"},
128 {Name: "GaugeBlaah2", Help: "GaugeBlaah2"},
129 {Name: "GaugeBlaah3", Help: "GaugeBlaah3"},
130 {Name: "GaugeBlaah4", Help: "GaugeBlaah4"},
133 ret1 := Metric.RegisterGaugeGroup(TestGaugeOpts, "TestMetricGaugeGroup")
136 t.Errorf("ret1 gauge group is empty")
139 ret1["GaugeBlaah1"].Inc()
140 ret1["GaugeBlaah2"].Inc()
141 ret1["GaugeBlaah3"].Inc()
142 ret1["GaugeBlaah4"].Inc()
144 ret2 := Metric.RegisterGaugeGroup(TestGaugeOpts, "TestMetricGaugeGroup")
147 t.Errorf("ret2 gauge group is empty")
150 ret2["GaugeBlaah1"].Inc()
151 ret2["GaugeBlaah2"].Inc()
152 ret2["GaugeBlaah3"].Inc()
153 ret2["GaugeBlaah4"].Inc()
155 if len(ret1) != len(ret2) {
156 t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2))
160 func TestMetricCounterVector(t *testing.T) {
163 c_1_1 := Metric.GetCounterFromVect([]string{"name1", "event1"}, mCVect)
165 c_1_2 := Metric.GetCounterFromVect([]string{"name1", "event1"}, mCVect)
168 t.Errorf("c_1_1 not same than c_1_2. cache not working?")
172 c_2_1 := Metric.GetCounterFromVect([]string{"name1", "event2"}, mCVect)
174 c_2_2 := Metric.GetCounterFromVect([]string{"name1", "event2"}, mCVect)
177 t.Errorf("c_2_1 not same than c_2_2. cache not working?")
180 t.Errorf("c_1_1 same than c_2_1. what?")
185 func TestMetricCounterGroupVector(t *testing.T) {
188 c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCGroupVect)
189 if _, ok := c_grp1["counter1"]; ok == false {
190 t.Errorf("c_grp1 counter1 not exists")
192 c_grp1["counter1"].Inc()
196 c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCGroupVect)
197 if _, ok := c_grp2["counter1"]; ok == false {
198 t.Errorf("c_grp2 counter1 not exists")
200 c_grp2["counter1"].Inc()
203 func TestMetricGaugeVector(t *testing.T) {
206 c_1_1 := Metric.GetGaugeFromVect([]string{"name1", "event1"}, mGVect)
208 c_1_2 := Metric.GetGaugeFromVect([]string{"name1", "event1"}, mGVect)
211 t.Errorf("c_1_1 not same than c_1_2. cache not working?")
215 c_2_1 := Metric.GetGaugeFromVect([]string{"name1", "event2"}, mGVect)
217 c_2_2 := Metric.GetGaugeFromVect([]string{"name1", "event2"}, mGVect)
220 t.Errorf("c_2_1 not same than c_2_2. cache not working?")
223 t.Errorf("c_1_1 same than c_2_1. what?")
227 func TestMetricGaugeGroupVector(t *testing.T) {
230 g_grp1 := Metric.GetGaugeGroupFromVects([]string{"name1", "event1"}, mGGroupVect)
231 if _, ok := g_grp1["gauge1"]; ok == false {
232 t.Errorf("g_grp1 gauge1 not exists")
234 g_grp1["gauge1"].Inc()
238 g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGGroupVect)
239 if _, ok := g_grp2["gauge1"]; ok == false {
240 t.Errorf("g_grp2 gauge1 not exists")
242 g_grp2["gauge1"].Inc()
245 func TestMetricCounterGroupVectorPrefix(t *testing.T) {
248 c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCGroupVect)
249 if _, ok := c_grp1["event1_counter1"]; ok == false {
250 t.Errorf("c_grp1 event1_counter1 not exists")
252 c_grp1["event1_counter1"].Inc()
256 c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCGroupVect)
257 if _, ok := c_grp2["event2_counter1"]; ok == false {
258 t.Errorf("c_grp2 event2_counter1 not exists")
260 c_grp2["event2_counter1"].Inc()
264 m_grp := NewMetricGroupsCache()
265 m_grp.CombineCounterGroups(c_grp1, c_grp2)
269 if m_grp.CIs("event1_counter1") == false {
270 t.Errorf("m_grp event1_counter1 not exists")
272 m_grp.CInc("event1_counter1")
276 if m_grp.CIs("event2_counter1") == false {
277 t.Errorf("m_grp event2_counter1 not exists")
280 m_grp.CAdd("event2_counter1", 1)
281 m_grp.CGet("event2_counter1")
284 func TestMetricGaugeGroupVectorPrefix(t *testing.T) {
287 g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGGroupVect)
288 if _, ok := g_grp1["event1_gauge1"]; ok == false {
289 t.Errorf("g_grp1 event1_gauge1 not exists")
291 g_grp1["event1_gauge1"].Inc()
295 g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGGroupVect)
296 if _, ok := g_grp2["event2_gauge1"]; ok == false {
297 t.Errorf("g_grp2 event2_gauge1 not exists")
299 g_grp2["event2_gauge1"].Inc()
301 m_grp := NewMetricGroupsCache()
302 m_grp.CombineGaugeGroups(g_grp1, g_grp2)
306 if m_grp.GIs("event1_gauge1") == false {
307 t.Errorf("m_grp event1_gauge1 not exists")
309 m_grp.GInc("event1_gauge1")
313 if m_grp.GIs("event2_gauge1") == false {
314 t.Errorf("m_grp event2_gauge1 not exists")
316 m_grp.GInc("event2_gauge1")
318 m_grp.GGet("event2_gauge1")
319 m_grp.GDec("event2_gauge1")
320 m_grp.GSet("event2_gauge1", 1)
323 func TestMetricGroupCache(t *testing.T) {
326 c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCGroupVect)
327 if _, ok := c_grp1["event1_counter1"]; ok == false {
328 t.Errorf("c_grp1 event1_counter1 not exists")
330 c_grp1["event1_counter1"].Inc()
334 c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCGroupVect)
335 if _, ok := c_grp2["counter1"]; ok == false {
336 t.Errorf("c_grp2 counter1 not exists")
338 c_grp2["counter1"].Inc()
342 g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGGroupVect)
343 if _, ok := g_grp1["event1_gauge1"]; ok == false {
344 t.Errorf("g_grp1 event1_gauge1 not exists")
346 g_grp1["event1_gauge1"].Inc()
350 g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGGroupVect)
351 if _, ok := g_grp2["gauge1"]; ok == false {
352 t.Errorf("g_grp2 gauge1 not exists")
354 g_grp2["gauge1"].Inc()
358 m_grp := NewMetricGroupsCache()
359 m_grp.CombineCounterGroups(c_grp1)
360 m_grp.CombineCounterGroupsWithPrefix("event2_", c_grp2)
361 m_grp.CombineGaugeGroups(g_grp1)
362 m_grp.CombineGaugeGroupsWithPrefix("event2_", g_grp2)
365 t.Errorf("Cache failed")
368 if m_grp.CIs("event1_counter1") == false {
369 t.Errorf("m_grp.Counters event1_counter1 not exists")
371 m_grp.CInc("event1_counter1")
373 if m_grp.CIs("event2_counter1") == false {
374 t.Errorf("m_grp.Counters event2_counter1 not exists")
376 m_grp.CInc("event2_counter1")
378 if m_grp.GIs("event1_gauge1") == false {
379 t.Errorf("m_grp.Gauges event1_gauge1 not exists")
381 m_grp.GInc("event1_gauge1")
383 if m_grp.GIs("event2_gauge1") == false {
384 t.Errorf("m_grp.Gauges event2_gauge1 not exists")
386 m_grp.GInc("event2_gauge1")
388 m_grp.CAdd("event2_counter1", 1)
389 m_grp.CGet("event2_counter1")
390 m_grp.GGet("event2_gauge1")
391 m_grp.GDec("event2_gauge1")
392 m_grp.GSet("event2_gauge1", 1)