bd7c73726c6e9a9c7b1349801bcfc79b18ea034b
[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 map[string]CounterVec
27 var mGVect map[string]GaugeVec
28 var mGGroup map[string]Gauge
29
30 func TestMetricSetup(t *testing.T) {
31         mCVect = Metric.RegisterCounterVecGroup(
32                 []CounterOpts{
33                         {Name: "counter1", Help: "counter1"},
34                 },
35                 []string{"name", "event"},
36                 "SUBSYSTEM")
37
38         mGVect = Metric.RegisterGaugeVecGroup(
39                 []CounterOpts{
40                         {Name: "counter2", Help: "counter2"},
41                 },
42                 []string{"name", "event"},
43                 "SUBSYSTEM")
44
45         mGGroup = Metric.RegisterGaugeGroup(
46                 []CounterOpts{
47                         {Name: "counter3", Help: "counter3"},
48                 },
49                 "SUBSYSTEM2")
50 }
51
52 func TestMetricCounter(t *testing.T) {
53         var TestCounterOpts = []CounterOpts{
54                 {Name: "Blaah1", Help: "Blaah1"},
55                 {Name: "Blaah2", Help: "Blaah2"},
56                 {Name: "Blaah3", Help: "Blaah3"},
57                 {Name: "Blaah4", Help: "Blaah4"},
58         }
59
60         ret1 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounter")
61
62         if len(ret1) == 0 {
63                 t.Errorf("ret1 counter group is empty")
64         }
65
66         ret2 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounter")
67
68         if len(ret2) == 0 {
69                 t.Errorf("ret2 counter group is empty")
70         }
71
72         if len(ret1) != len(ret2) {
73                 t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2))
74         }
75 }
76
77 func TestMetricCounterVector(t *testing.T) {
78         //
79         //
80         c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCVect)
81         if _, ok := c_grp1["counter1"]; ok == false {
82                 t.Errorf("c_grp1 counter1 not exists")
83         }
84         c_grp1["counter1"].Inc()
85
86         //
87         //
88         c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCVect)
89         if _, ok := c_grp2["counter1"]; ok == false {
90                 t.Errorf("c_grp2 counter1 not exists")
91         }
92         c_grp2["counter1"].Inc()
93 }
94
95 func TestMetricGaugeVector(t *testing.T) {
96         //
97         //
98         g_grp1 := Metric.GetGaugeGroupFromVects([]string{"name1", "event1"}, mGVect)
99         if _, ok := g_grp1["counter2"]; ok == false {
100                 t.Errorf("g_grp1 counter2 not exists")
101         }
102         g_grp1["counter2"].Inc()
103
104         //
105         //
106         g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGVect)
107         if _, ok := g_grp2["counter2"]; ok == false {
108                 t.Errorf("g_grp2 counter2 not exists")
109         }
110         g_grp2["counter2"].Inc()
111 }
112
113 func TestMetricCounterVectorPrefix(t *testing.T) {
114         //
115         //
116         c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
117         if _, ok := c_grp1["event1_counter1"]; ok == false {
118                 t.Errorf("c_grp1 event1_counter1 not exists")
119         }
120         c_grp1["event1_counter1"].Inc()
121
122         //
123         //
124         c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
125         if _, ok := c_grp2["event2_counter1"]; ok == false {
126                 t.Errorf("c_grp2 event2_counter1 not exists")
127         }
128         c_grp2["event2_counter1"].Inc()
129
130         //
131         //
132         m_grp := NewMetricGroupsCache()
133         m_grp.CombineCounterGroups(c_grp1, c_grp2)
134
135         //
136         //
137         if m_grp.CIs("event1_counter1") == false {
138                 t.Errorf("m_grp event1_counter1 not exists")
139         }
140         m_grp.CInc("event1_counter1")
141
142         //
143         //
144         if m_grp.CIs("event2_counter1") == false {
145                 t.Errorf("m_grp event2_counter1 not exists")
146         }
147
148         m_grp.CAdd("event2_counter1", 1)
149         m_grp.CGet("event2_counter1")
150 }
151
152 func TestMetricGaugeVectorPrefix(t *testing.T) {
153         //
154         //
155         g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
156         if _, ok := g_grp1["event1_counter2"]; ok == false {
157                 t.Errorf("g_grp1 event1_counter2 not exists")
158         }
159         g_grp1["event1_counter2"].Inc()
160
161         //
162         //
163         g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
164         if _, ok := g_grp2["event2_counter2"]; ok == false {
165                 t.Errorf("g_grp2 event2_counter2 not exists")
166         }
167         g_grp2["event2_counter2"].Inc()
168
169         m_grp := NewMetricGroupsCache()
170         m_grp.CombineGaugeGroups(g_grp1, g_grp2)
171
172         //
173         //
174         if m_grp.GIs("event1_counter2") == false {
175                 t.Errorf("m_grp event1_counter2 not exists")
176         }
177         m_grp.GInc("event1_counter2")
178
179         //
180         //
181         if m_grp.GIs("event2_counter2") == false {
182                 t.Errorf("m_grp event2_counter2 not exists")
183         }
184         m_grp.GInc("event2_counter2")
185
186         m_grp.GGet("event2_counter2")
187         m_grp.GDec("event2_counter2")
188         m_grp.GSet("event2_counter2", 1)
189 }
190
191 func TestMetricGroupCache(t *testing.T) {
192         //
193         //
194         c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
195         if _, ok := c_grp1["event1_counter1"]; ok == false {
196                 t.Errorf("c_grp1 event1_counter1 not exists")
197         }
198         c_grp1["event1_counter1"].Inc()
199
200         //
201         //
202         c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
203         if _, ok := c_grp2["event2_counter1"]; ok == false {
204                 t.Errorf("c_grp2 event2_counter1 not exists")
205         }
206         c_grp2["event2_counter1"].Inc()
207
208         //
209         //
210         g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
211         if _, ok := g_grp1["event1_counter2"]; ok == false {
212                 t.Errorf("g_grp1 event1_counter2 not exists")
213         }
214         g_grp1["event1_counter2"].Inc()
215
216         //
217         //
218         g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
219         if _, ok := g_grp2["event2_counter2"]; ok == false {
220                 t.Errorf("g_grp2 event2_counter2 not exists")
221         }
222         g_grp2["event2_counter2"].Inc()
223
224         //
225         //
226         m_grp := NewMetricGroupsCache()
227         m_grp.CombineCounterGroups(c_grp1)
228         m_grp.CombineCounterGroups(c_grp2)
229         m_grp.CombineGaugeGroups(g_grp1)
230         m_grp.CombineGaugeGroups(g_grp2)
231
232         if m_grp == nil {
233                 t.Errorf("Cache failed")
234         }
235
236         if m_grp.CIs("event1_counter1") == false {
237                 t.Errorf("m_grp.Counters event1_counter1 not exists")
238         }
239         m_grp.CInc("event1_counter1")
240
241         if m_grp.CIs("event2_counter1") == false {
242                 t.Errorf("m_grp.Counters event2_counter1 not exists")
243         }
244         m_grp.CInc("event2_counter1")
245
246         if m_grp.GIs("event1_counter2") == false {
247                 t.Errorf("m_grp.Gauges event1_counter2 not exists")
248         }
249         m_grp.GInc("event1_counter2")
250
251         if m_grp.GIs("event2_counter2") == false {
252                 t.Errorf("m_grp.Gauges event2_counter2 not exists")
253         }
254         m_grp.GInc("event2_counter2")
255
256         m_grp.CAdd("event2_counter1", 1)
257         m_grp.CGet("event2_counter1")
258         m_grp.GGet("event2_counter2")
259         m_grp.GDec("event2_counter2")
260         m_grp.GSet("event2_counter2", 1)
261 }