blob: 25903edbb07a5a631739216af286235001ab74e8 [file] [log] [blame]
sbarbari17d7e222019-11-05 10:02:29 -05001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package model
17
18import (
19 "context"
20 "encoding/hex"
npujar9a30c702019-11-14 17:06:39 +053021 "math/rand"
22 "reflect"
23 "strconv"
24 "sync"
25 "testing"
26
sbarbari17d7e222019-11-05 10:02:29 -050027 "github.com/google/uuid"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080028 "github.com/opencord/voltha-lib-go/v3/pkg/log"
29 "github.com/opencord/voltha-protos/v3/go/common"
30 "github.com/opencord/voltha-protos/v3/go/openflow_13"
31 "github.com/opencord/voltha-protos/v3/go/voltha"
Thomas Lee Se5a44012019-11-07 20:32:24 +053032 "github.com/stretchr/testify/assert"
sbarbari17d7e222019-11-05 10:02:29 -050033)
34
35var (
npujar9a30c702019-11-14 17:06:39 +053036 BenchmarkProxyRoot Root
37 BenchmarkProxyDeviceProxy *Proxy
38 BenchmarkProxyPLT *proxyLoadTest
39 BenchmarkProxyLogger log.Logger
sbarbari17d7e222019-11-05 10:02:29 -050040)
41
42type proxyLoadChanges struct {
43 ID string
44 Before interface{}
45 After interface{}
46}
47type proxyLoadTest struct {
48 mutex sync.RWMutex
49
50 addMutex sync.RWMutex
51 addedDevices []string
52
53 firmwareMutex sync.RWMutex
54 updatedFirmwares []proxyLoadChanges
55 flowMutex sync.RWMutex
56 updatedFlows []proxyLoadChanges
57
58 preAddExecuted bool
59 postAddExecuted bool
60 preUpdateExecuted bool
61 postUpdateExecuted bool
62}
63
64func (plt *proxyLoadTest) SetPreAddExecuted(status bool) {
65 plt.mutex.Lock()
66 defer plt.mutex.Unlock()
67 plt.preAddExecuted = status
68}
69func (plt *proxyLoadTest) SetPostAddExecuted(status bool) {
70 plt.mutex.Lock()
71 defer plt.mutex.Unlock()
72 plt.postAddExecuted = status
73}
74func (plt *proxyLoadTest) SetPreUpdateExecuted(status bool) {
75 plt.mutex.Lock()
76 defer plt.mutex.Unlock()
77 plt.preUpdateExecuted = status
78}
79func (plt *proxyLoadTest) SetPostUpdateExecuted(status bool) {
80 plt.mutex.Lock()
81 defer plt.mutex.Unlock()
82 plt.postUpdateExecuted = status
83}
84
85func init() {
Thomas Lee Se5a44012019-11-07 20:32:24 +053086 var err error
npujar9a30c702019-11-14 17:06:39 +053087 BenchmarkProxyRoot = NewRoot(&voltha.Voltha{}, nil)
sbarbari17d7e222019-11-05 10:02:29 -050088
npujar9a30c702019-11-14 17:06:39 +053089 BenchmarkProxyLogger, _ = log.AddPackage(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"})
sbarbari17d7e222019-11-05 10:02:29 -050090 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
91 //Setup default logger - applies for packages that do not have specific logger set
92 if _, err := log.SetDefaultLogger(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"}); err != nil {
93 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
94 }
95
96 // Update all loggers (provisioned via init) with a common field
97 if err := log.UpdateAllLoggers(log.Fields{"instanceId": "PLT"}); err != nil {
98 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
99 }
100 log.SetPackageLogLevel("github.com/opencord/voltha-go/db/model", log.DebugLevel)
101
npujar9a30c702019-11-14 17:06:39 +0530102 if BenchmarkProxyDeviceProxy, err = BenchmarkProxyRoot.CreateProxy(context.Background(), "/", false); err != nil {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530103 log.With(log.Fields{"error": err}).Fatal("Cannot create benchmark proxy")
104 }
sbarbari17d7e222019-11-05 10:02:29 -0500105 // Register ADD instructions callbacks
npujar9a30c702019-11-14 17:06:39 +0530106 BenchmarkProxyPLT = &proxyLoadTest{}
sbarbari17d7e222019-11-05 10:02:29 -0500107
npujar9a30c702019-11-14 17:06:39 +0530108 BenchmarkProxyDeviceProxy.RegisterCallback(PreAdd, commonCallbackFunc, "PreAdd", BenchmarkProxyPLT.SetPreAddExecuted)
109 BenchmarkProxyDeviceProxy.RegisterCallback(PostAdd, commonCallbackFunc, "PostAdd", BenchmarkProxyPLT.SetPostAddExecuted)
sbarbari17d7e222019-11-05 10:02:29 -0500110
111 //// Register UPDATE instructions callbacks
npujar9a30c702019-11-14 17:06:39 +0530112 BenchmarkProxyDeviceProxy.RegisterCallback(PreUpdate, commonCallbackFunc, "PreUpdate", BenchmarkProxyPLT.SetPreUpdateExecuted)
113 BenchmarkProxyDeviceProxy.RegisterCallback(PostUpdate, commonCallbackFunc, "PostUpdate", BenchmarkProxyPLT.SetPostUpdateExecuted)
sbarbari17d7e222019-11-05 10:02:29 -0500114
115}
116
117func BenchmarkProxy_AddDevice(b *testing.B) {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530118 var err error
sbarbari17d7e222019-11-05 10:02:29 -0500119 defer GetProfiling().Report()
120 b.RunParallel(func(pb *testing.PB) {
121 b.Log("Started adding devices")
122 for pb.Next() {
123 ltPorts := []*voltha.Port{
124 {
125 PortNo: 123,
126 Label: "lt-port-0",
127 Type: voltha.Port_PON_OLT,
128 AdminState: common.AdminState_ENABLED,
129 OperStatus: common.OperStatus_ACTIVE,
130 DeviceId: "lt-port-0-device-id",
131 Peers: []*voltha.Port_PeerPort{},
132 },
133 }
134
135 ltStats := &openflow_13.OfpFlowStats{
136 Id: 1000,
137 }
138 ltFlows := &openflow_13.Flows{
139 Items: []*openflow_13.OfpFlowStats{ltStats},
140 }
141 ltDevice := &voltha.Device{
142 Id: "",
143 Type: "simulated_olt",
144 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
145 AdminState: voltha.AdminState_PREPROVISIONED,
146 Flows: ltFlows,
147 Ports: ltPorts,
148 }
149
150 ltDevIDBin, _ := uuid.New().MarshalBinary()
151 ltDevID := "0001" + hex.EncodeToString(ltDevIDBin)[:12]
152 ltDevice.Id = ltDevID
153
npujar9a30c702019-11-14 17:06:39 +0530154 BenchmarkProxyPLT.SetPreAddExecuted(false)
155 BenchmarkProxyPLT.SetPostAddExecuted(false)
sbarbari17d7e222019-11-05 10:02:29 -0500156
157 var added interface{}
158 // Add the device
npujar9a30c702019-11-14 17:06:39 +0530159 if added, err = BenchmarkProxyDeviceProxy.AddWithID(context.Background(), "/devices", ltDevID, ltDevice, ""); err != nil {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530160 log.With(log.Fields{"error": err}).Fatal("Cannot create proxy")
161 }
162 if added == nil {
npujar9a30c702019-11-14 17:06:39 +0530163 BenchmarkProxyLogger.Errorf("Failed to add device: %+v", ltDevice)
sbarbari17d7e222019-11-05 10:02:29 -0500164 continue
165 } else {
npujar9a30c702019-11-14 17:06:39 +0530166 BenchmarkProxyLogger.Infof("Device was added 1: %+v", added)
sbarbari17d7e222019-11-05 10:02:29 -0500167 }
168
npujar9a30c702019-11-14 17:06:39 +0530169 BenchmarkProxyPLT.addMutex.Lock()
170 BenchmarkProxyPLT.addedDevices = append(BenchmarkProxyPLT.addedDevices, added.(*voltha.Device).Id)
171 BenchmarkProxyPLT.addMutex.Unlock()
sbarbari17d7e222019-11-05 10:02:29 -0500172 }
173 })
174
npujar9a30c702019-11-14 17:06:39 +0530175 BenchmarkProxyLogger.Infof("Number of added devices : %d", len(BenchmarkProxyPLT.addedDevices))
sbarbari17d7e222019-11-05 10:02:29 -0500176}
177
178func BenchmarkProxy_UpdateFirmware(b *testing.B) {
179 b.RunParallel(func(pb *testing.PB) {
180 for pb.Next() {
181 //for i:=0; i < b.N; i++ {
182
npujar9a30c702019-11-14 17:06:39 +0530183 if len(BenchmarkProxyPLT.addedDevices) > 0 {
sbarbari17d7e222019-11-05 10:02:29 -0500184 var target interface{}
npujar9a30c702019-11-14 17:06:39 +0530185 randomID := BenchmarkProxyPLT.addedDevices[rand.Intn(len(BenchmarkProxyPLT.addedDevices))]
186 firmProxy, err := BenchmarkProxyRoot.CreateProxy(context.Background(), "/", false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530187 if err != nil {
188 log.With(log.Fields{"error": err}).Fatal("Cannot create firmware proxy")
189 }
190 target, err = firmProxy.Get(context.Background(), "/devices/"+randomID, 0, false,
191 "")
192 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530193 BenchmarkProxyLogger.Errorf("Failed to create target due to error %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530194 assert.NotNil(b, err)
195 }
196 if !reflect.ValueOf(target).IsValid() {
npujar9a30c702019-11-14 17:06:39 +0530197 BenchmarkProxyLogger.Errorf("Failed to find device: %s %+v", randomID, target)
sbarbari17d7e222019-11-05 10:02:29 -0500198 continue
199 }
200
npujar9a30c702019-11-14 17:06:39 +0530201 BenchmarkProxyPLT.SetPreUpdateExecuted(false)
202 BenchmarkProxyPLT.SetPostUpdateExecuted(false)
203 firmProxy.RegisterCallback(PreUpdate, commonCallbackFunc, "PreUpdate", BenchmarkProxyPLT.SetPreUpdateExecuted)
204 firmProxy.RegisterCallback(PostUpdate, commonCallbackFunc, "PostUpdate", BenchmarkProxyPLT.SetPostUpdateExecuted)
sbarbari17d7e222019-11-05 10:02:29 -0500205
206 var fwVersion int
207
208 before := target.(*voltha.Device).FirmwareVersion
209 if target.(*voltha.Device).FirmwareVersion == "n/a" {
210 fwVersion = 0
211 } else {
212 fwVersion, _ = strconv.Atoi(target.(*voltha.Device).FirmwareVersion)
213 fwVersion++
214 }
215
216 target.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
217 after := target.(*voltha.Device).FirmwareVersion
218
219 var updated interface{}
Thomas Lee Se5a44012019-11-07 20:32:24 +0530220 if updated, err = firmProxy.Update(context.Background(), "/devices/"+randomID, target.(*voltha.Device), false, ""); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530221 BenchmarkProxyLogger.Errorf("Failed to update firmware proxy due to error %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530222 assert.NotNil(b, err)
223 }
224 if updated == nil {
npujar9a30c702019-11-14 17:06:39 +0530225 BenchmarkProxyLogger.Errorf("Failed to update device: %+v", target)
sbarbari17d7e222019-11-05 10:02:29 -0500226 continue
227 } else {
npujar9a30c702019-11-14 17:06:39 +0530228 BenchmarkProxyLogger.Infof("Device was updated : %+v", updated)
sbarbari17d7e222019-11-05 10:02:29 -0500229
230 }
231
Thomas Lee Se5a44012019-11-07 20:32:24 +0530232 d, err := firmProxy.Get(context.Background(), "/devices/"+randomID, 0, false, "")
233 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530234 BenchmarkProxyLogger.Errorf("Failed to get device info from firmware proxy due to error %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530235 assert.NotNil(b, err)
236 }
237 if !reflect.ValueOf(d).IsValid() {
npujar9a30c702019-11-14 17:06:39 +0530238 BenchmarkProxyLogger.Errorf("Failed to get device: %s", randomID)
sbarbari17d7e222019-11-05 10:02:29 -0500239 continue
240 } else if d.(*voltha.Device).FirmwareVersion == after {
npujar9a30c702019-11-14 17:06:39 +0530241 BenchmarkProxyLogger.Infof("Imm Device was updated with new value: %s %+v", randomID, d)
sbarbari17d7e222019-11-05 10:02:29 -0500242 } else if d.(*voltha.Device).FirmwareVersion == before {
npujar9a30c702019-11-14 17:06:39 +0530243 BenchmarkProxyLogger.Errorf("Imm Device kept old value: %s %+v %+v", randomID, d, target)
sbarbari17d7e222019-11-05 10:02:29 -0500244 } else {
npujar9a30c702019-11-14 17:06:39 +0530245 BenchmarkProxyLogger.Errorf("Imm Device has unknown value: %s %+v %+v", randomID, d, target)
sbarbari17d7e222019-11-05 10:02:29 -0500246 }
247
npujar9a30c702019-11-14 17:06:39 +0530248 BenchmarkProxyPLT.firmwareMutex.Lock()
sbarbari17d7e222019-11-05 10:02:29 -0500249
npujar9a30c702019-11-14 17:06:39 +0530250 BenchmarkProxyPLT.updatedFirmwares = append(
251 BenchmarkProxyPLT.updatedFirmwares,
sbarbari17d7e222019-11-05 10:02:29 -0500252 proxyLoadChanges{ID: randomID, Before: before, After: after},
253 )
npujar9a30c702019-11-14 17:06:39 +0530254 BenchmarkProxyPLT.firmwareMutex.Unlock()
sbarbari17d7e222019-11-05 10:02:29 -0500255 }
256 }
257 })
258}
259
sbarbari17d7e222019-11-05 10:02:29 -0500260func BenchmarkProxy_UpdateFlows(b *testing.B) {
261 b.RunParallel(func(pb *testing.PB) {
262 for pb.Next() {
npujar9a30c702019-11-14 17:06:39 +0530263 if len(BenchmarkProxyPLT.addedDevices) > 0 {
264 randomID := BenchmarkProxyPLT.addedDevices[rand.Intn(len(BenchmarkProxyPLT.addedDevices))]
sbarbari17d7e222019-11-05 10:02:29 -0500265
npujar9a30c702019-11-14 17:06:39 +0530266 flowsProxy, err := BenchmarkProxyRoot.CreateProxy(context.Background(), "/devices/"+randomID+"/flows", false)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530267 if err != nil {
268 log.With(log.Fields{"error": err}).Fatal("Cannot create flows proxy")
269 }
270 flows, err := flowsProxy.Get(context.Background(), "/", 0, false, "")
271 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530272 BenchmarkProxyLogger.Errorf("Failed to get flows from flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530273 assert.NotNil(b, err)
274 }
sbarbari17d7e222019-11-05 10:02:29 -0500275
276 before := flows.(*openflow_13.Flows).Items[0].TableId
277 flows.(*openflow_13.Flows).Items[0].TableId = uint32(rand.Intn(3000))
278 after := flows.(*openflow_13.Flows).Items[0].TableId
279
280 flowsProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530281 PreUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500282 commonCallback2,
283 )
284 flowsProxy.RegisterCallback(
npujar9a30c702019-11-14 17:06:39 +0530285 PostUpdate,
sbarbari17d7e222019-11-05 10:02:29 -0500286 commonCallback2,
287 )
288
289 var updated interface{}
Thomas Lee Se5a44012019-11-07 20:32:24 +0530290 if updated, err = flowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, ""); err != nil {
npujar9a30c702019-11-14 17:06:39 +0530291 BenchmarkProxyLogger.Errorf("Cannot update flows proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530292 assert.NotNil(b, err)
293 }
294 if updated == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500295 b.Errorf("Failed to update flows for device: %+v", flows)
296 } else {
npujar9a30c702019-11-14 17:06:39 +0530297 BenchmarkProxyLogger.Infof("Flows were updated : %+v", updated)
sbarbari17d7e222019-11-05 10:02:29 -0500298 }
npujar9a30c702019-11-14 17:06:39 +0530299 BenchmarkProxyPLT.flowMutex.Lock()
300 BenchmarkProxyPLT.updatedFlows = append(
301 BenchmarkProxyPLT.updatedFlows,
sbarbari17d7e222019-11-05 10:02:29 -0500302 proxyLoadChanges{ID: randomID, Before: before, After: after},
303 )
npujar9a30c702019-11-14 17:06:39 +0530304 BenchmarkProxyPLT.flowMutex.Unlock()
sbarbari17d7e222019-11-05 10:02:29 -0500305 }
306 }
307 })
308}
309
310func BenchmarkProxy_GetDevices(b *testing.B) {
311 //traverseBranches(BenchmarkProxy_DeviceProxy.Root.node.Branches[NONE].GetLatest(), 0)
312
npujar9a30c702019-11-14 17:06:39 +0530313 for i := 0; i < len(BenchmarkProxyPLT.addedDevices); i++ {
314 devToGet := BenchmarkProxyPLT.addedDevices[i]
sbarbari17d7e222019-11-05 10:02:29 -0500315 // Verify that the added device can now be retrieved
npujar9a30c702019-11-14 17:06:39 +0530316 d, err := BenchmarkProxyDeviceProxy.Get(context.Background(), "/devices/"+devToGet, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530317 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530318 BenchmarkProxyLogger.Errorf("Failed to get device info from device proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530319 assert.NotNil(b, err)
320 }
321 if !reflect.ValueOf(d).IsValid() {
npujar9a30c702019-11-14 17:06:39 +0530322 BenchmarkProxyLogger.Errorf("Failed to get device: %s", devToGet)
sbarbari17d7e222019-11-05 10:02:29 -0500323 continue
324 } else {
npujar9a30c702019-11-14 17:06:39 +0530325 BenchmarkProxyLogger.Infof("Got device: %s %+v", devToGet, d)
sbarbari17d7e222019-11-05 10:02:29 -0500326 }
327 }
328}
329
330func BenchmarkProxy_GetUpdatedFirmware(b *testing.B) {
npujar9a30c702019-11-14 17:06:39 +0530331 for i := 0; i < len(BenchmarkProxyPLT.updatedFirmwares); i++ {
332 devToGet := BenchmarkProxyPLT.updatedFirmwares[i].ID
sbarbari17d7e222019-11-05 10:02:29 -0500333 // Verify that the updated device can be retrieved and that the updates were actually applied
npujar9a30c702019-11-14 17:06:39 +0530334 d, err := BenchmarkProxyDeviceProxy.Get(context.Background(), "/devices/"+devToGet, 0, false, "")
Thomas Lee Se5a44012019-11-07 20:32:24 +0530335 if err != nil {
npujar9a30c702019-11-14 17:06:39 +0530336 BenchmarkProxyLogger.Errorf("Failed to get device info from device proxy due to error: %v", err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530337 assert.NotNil(b, err)
338 }
339 if !reflect.ValueOf(d).IsValid() {
npujar9a30c702019-11-14 17:06:39 +0530340 BenchmarkProxyLogger.Errorf("Failed to get device: %s", devToGet)
sbarbari17d7e222019-11-05 10:02:29 -0500341 continue
npujar9a30c702019-11-14 17:06:39 +0530342 } else if d.(*voltha.Device).FirmwareVersion == BenchmarkProxyPLT.updatedFirmwares[i].After.(string) {
343 BenchmarkProxyLogger.Infof("Device was updated with new value: %s %+v", devToGet, d)
344 } else if d.(*voltha.Device).FirmwareVersion == BenchmarkProxyPLT.updatedFirmwares[i].Before.(string) {
345 BenchmarkProxyLogger.Errorf("Device kept old value: %s %+v %+v", devToGet, d, BenchmarkProxyPLT.updatedFirmwares[i])
sbarbari17d7e222019-11-05 10:02:29 -0500346 } else {
npujar9a30c702019-11-14 17:06:39 +0530347 BenchmarkProxyLogger.Errorf("Device has unknown value: %s %+v %+v", devToGet, d, BenchmarkProxyPLT.updatedFirmwares[i])
sbarbari17d7e222019-11-05 10:02:29 -0500348 }
349 }
350}