Improved metrics
[ric-plt/xapp-frame.git] / pkg / xapp / metrics_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19
20 package xapp
21
22 import (
23         "testing"
24 )
25
26 var mCVect CounterVec
27 var mGVect GaugeVec
28
29 var mCGroupVect map[string]CounterVec
30 var mGGroupVect map[string]GaugeVec
31
32 func TestMetricSetup(t *testing.T) {
33         mCVect = Metric.RegisterCounterVec(CounterOpts{Name: "counter1", Help: "counter1"}, []string{"name", "event"}, "SUBSYSTEM0")
34
35         mCGroupVect = Metric.RegisterCounterVecGroup(
36                 []CounterOpts{
37                         {Name: "counter1", Help: "counter1"},
38                 },
39                 []string{"name", "event"},
40                 "SUBSYSTEM1")
41
42         mGVect = Metric.RegisterGaugeVec(CounterOpts{Name: "gauge1", Help: "gauge1"}, []string{"name", "event"}, "SUBSYSTEM0")
43
44         mGGroupVect = Metric.RegisterGaugeVecGroup(
45                 []CounterOpts{
46                         {Name: "gauge1", Help: "gauge1"},
47                 },
48                 []string{"name", "event"},
49                 "SUBSYSTEM1")
50
51         tmpCVect := Metric.RegisterCounterVec(CounterOpts{Name: "counter1", Help: "counter1"}, []string{"name", "event"}, "SUBSYSTEM0")
52
53         if tmpCVect.Vec != mCVect.Vec {
54                 t.Errorf("tmpCVect not same than mCVect. cache not working?")
55         }
56
57         tmpGVect := Metric.RegisterGaugeVec(CounterOpts{Name: "gauge1", Help: "gauge1"}, []string{"name", "event"}, "SUBSYSTEM0")
58
59         if tmpGVect.Vec != mGVect.Vec {
60                 t.Errorf("tmpGVect not same than mGVect. cache not working?")
61         }
62
63         Metric.RegisterCounterVec(CounterOpts{Name: "counter1", Help: "counter1"}, []string{"name", "eventMismatch"}, "SUBSYSTEM0")
64         Metric.RegisterGaugeVec(CounterOpts{Name: "gauge1", Help: "gauge1"}, []string{"name", "eventMismatch"}, "SUBSYSTEM0")
65
66 }
67
68 func TestMetricCounter(t *testing.T) {
69         TestCounterOpt := CounterOpts{Name: "CounterBlaah1", Help: "CounterBlaah1"}
70         ret1 := Metric.RegisterCounter(TestCounterOpt, "TestMetricCounter")
71         ret1.Inc()
72         ret2 := Metric.RegisterCounter(TestCounterOpt, "TestMetricCounter")
73         ret2.Inc()
74         if ret1 != ret2 {
75                 t.Errorf("ret1 not same than ret2. cache not working?")
76         }
77 }
78
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"},
85         }
86
87         ret1 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounterGroup")
88
89         if len(ret1) == 0 {
90                 t.Errorf("ret1 counter group is empty")
91         }
92
93         ret1["CounterBlaah1"].Inc()
94         ret1["CounterBlaah2"].Inc()
95         ret1["CounterBlaah3"].Inc()
96         ret1["CounterBlaah4"].Inc()
97
98         ret2 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounterGroup")
99
100         if len(ret2) == 0 {
101                 t.Errorf("ret2 counter group is empty")
102         }
103
104         ret2["CounterBlaah1"].Inc()
105         ret2["CounterBlaah2"].Inc()
106         ret2["CounterBlaah3"].Inc()
107         ret2["CounterBlaah4"].Inc()
108
109         if len(ret1) != len(ret2) {
110                 t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2))
111         }
112 }
113
114 func TestMetricGauge(t *testing.T) {
115         TestGaugeOpts := CounterOpts{Name: "GaugeBlaah1", Help: "GaugeBlaah1"}
116         ret1 := Metric.RegisterGauge(TestGaugeOpts, "TestMetricGauge")
117         ret1.Inc()
118         ret2 := Metric.RegisterGauge(TestGaugeOpts, "TestMetricGauge")
119         ret2.Inc()
120         if ret1 != ret2 {
121                 t.Errorf("ret1 not same than ret2. cache not working?")
122         }
123 }
124
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"},
131         }
132
133         ret1 := Metric.RegisterGaugeGroup(TestGaugeOpts, "TestMetricGaugeGroup")
134
135         if len(ret1) == 0 {
136                 t.Errorf("ret1 gauge group is empty")
137         }
138
139         ret1["GaugeBlaah1"].Inc()
140         ret1["GaugeBlaah2"].Inc()
141         ret1["GaugeBlaah3"].Inc()
142         ret1["GaugeBlaah4"].Inc()
143
144         ret2 := Metric.RegisterGaugeGroup(TestGaugeOpts, "TestMetricGaugeGroup")
145
146         if len(ret2) == 0 {
147                 t.Errorf("ret2 gauge group is empty")
148         }
149
150         ret2["GaugeBlaah1"].Inc()
151         ret2["GaugeBlaah2"].Inc()
152         ret2["GaugeBlaah3"].Inc()
153         ret2["GaugeBlaah4"].Inc()
154
155         if len(ret1) != len(ret2) {
156                 t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2))
157         }
158 }
159
160 func TestMetricCounterVector(t *testing.T) {
161         //
162         //
163         c_1_1 := Metric.GetCounterFromVect([]string{"name1", "event1"}, mCVect)
164         c_1_1.Inc()
165         c_1_2 := Metric.GetCounterFromVect([]string{"name1", "event1"}, mCVect)
166         c_1_2.Inc()
167         if c_1_1 != c_1_2 {
168                 t.Errorf("c_1_1 not same than c_1_2. cache not working?")
169         }
170         //
171         //
172         c_2_1 := Metric.GetCounterFromVect([]string{"name1", "event2"}, mCVect)
173         c_2_1.Inc()
174         c_2_2 := Metric.GetCounterFromVect([]string{"name1", "event2"}, mCVect)
175         c_2_2.Inc()
176         if c_2_1 != c_2_2 {
177                 t.Errorf("c_2_1 not same than c_2_2. cache not working?")
178         }
179         if c_1_1 == c_2_1 {
180                 t.Errorf("c_1_1 same than c_2_1. what?")
181         }
182
183 }
184
185 func TestMetricCounterGroupVector(t *testing.T) {
186         //
187         //
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")
191         }
192         c_grp1["counter1"].Inc()
193
194         //
195         //
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")
199         }
200         c_grp2["counter1"].Inc()
201 }
202
203 func TestMetricGaugeVector(t *testing.T) {
204         //
205         //
206         c_1_1 := Metric.GetGaugeFromVect([]string{"name1", "event1"}, mGVect)
207         c_1_1.Inc()
208         c_1_2 := Metric.GetGaugeFromVect([]string{"name1", "event1"}, mGVect)
209         c_1_2.Inc()
210         if c_1_1 != c_1_2 {
211                 t.Errorf("c_1_1 not same than c_1_2. cache not working?")
212         }
213         //
214         //
215         c_2_1 := Metric.GetGaugeFromVect([]string{"name1", "event2"}, mGVect)
216         c_2_1.Inc()
217         c_2_2 := Metric.GetGaugeFromVect([]string{"name1", "event2"}, mGVect)
218         c_2_2.Inc()
219         if c_2_1 != c_2_2 {
220                 t.Errorf("c_2_1 not same than c_2_2. cache not working?")
221         }
222         if c_1_1 == c_2_1 {
223                 t.Errorf("c_1_1 same than c_2_1. what?")
224         }
225 }
226
227 func TestMetricGaugeGroupVector(t *testing.T) {
228         //
229         //
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")
233         }
234         g_grp1["gauge1"].Inc()
235
236         //
237         //
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")
241         }
242         g_grp2["gauge1"].Inc()
243 }
244
245 func TestMetricCounterGroupVectorPrefix(t *testing.T) {
246         //
247         //
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")
251         }
252         c_grp1["event1_counter1"].Inc()
253
254         //
255         //
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")
259         }
260         c_grp2["event2_counter1"].Inc()
261
262         //
263         //
264         m_grp := NewMetricGroupsCache()
265         m_grp.CombineCounterGroups(c_grp1, c_grp2)
266
267         //
268         //
269         if m_grp.CIs("event1_counter1") == false {
270                 t.Errorf("m_grp event1_counter1 not exists")
271         }
272         m_grp.CInc("event1_counter1")
273
274         //
275         //
276         if m_grp.CIs("event2_counter1") == false {
277                 t.Errorf("m_grp event2_counter1 not exists")
278         }
279
280         m_grp.CAdd("event2_counter1", 1)
281         m_grp.CGet("event2_counter1")
282 }
283
284 func TestMetricGaugeGroupVectorPrefix(t *testing.T) {
285         //
286         //
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")
290         }
291         g_grp1["event1_gauge1"].Inc()
292
293         //
294         //
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")
298         }
299         g_grp2["event2_gauge1"].Inc()
300
301         m_grp := NewMetricGroupsCache()
302         m_grp.CombineGaugeGroups(g_grp1, g_grp2)
303
304         //
305         //
306         if m_grp.GIs("event1_gauge1") == false {
307                 t.Errorf("m_grp event1_gauge1 not exists")
308         }
309         m_grp.GInc("event1_gauge1")
310
311         //
312         //
313         if m_grp.GIs("event2_gauge1") == false {
314                 t.Errorf("m_grp event2_gauge1 not exists")
315         }
316         m_grp.GInc("event2_gauge1")
317
318         m_grp.GGet("event2_gauge1")
319         m_grp.GDec("event2_gauge1")
320         m_grp.GSet("event2_gauge1", 1)
321 }
322
323 func TestMetricGroupCache(t *testing.T) {
324         //
325         //
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")
329         }
330         c_grp1["event1_counter1"].Inc()
331
332         //
333         //
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")
337         }
338         c_grp2["counter1"].Inc()
339
340         //
341         //
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")
345         }
346         g_grp1["event1_gauge1"].Inc()
347
348         //
349         //
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")
353         }
354         g_grp2["gauge1"].Inc()
355
356         //
357         //
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)
363
364         if m_grp == nil {
365                 t.Errorf("Cache failed")
366         }
367
368         if m_grp.CIs("event1_counter1") == false {
369                 t.Errorf("m_grp.Counters event1_counter1 not exists")
370         }
371         m_grp.CInc("event1_counter1")
372
373         if m_grp.CIs("event2_counter1") == false {
374                 t.Errorf("m_grp.Counters event2_counter1 not exists")
375         }
376         m_grp.CInc("event2_counter1")
377
378         if m_grp.GIs("event1_gauge1") == false {
379                 t.Errorf("m_grp.Gauges event1_gauge1 not exists")
380         }
381         m_grp.GInc("event1_gauge1")
382
383         if m_grp.GIs("event2_gauge1") == false {
384                 t.Errorf("m_grp.Gauges event2_gauge1 not exists")
385         }
386         m_grp.GInc("event2_gauge1")
387
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)
393 }