blob: e5ae1c1263f2b85bc3e5f3a83a8681a6c2832b6f [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"
21 "github.com/google/uuid"
22 "github.com/opencord/voltha-lib-go/v2/pkg/log"
23 "github.com/opencord/voltha-protos/v2/go/common"
24 "github.com/opencord/voltha-protos/v2/go/openflow_13"
25 "github.com/opencord/voltha-protos/v2/go/voltha"
Thomas Lee Se5a44012019-11-07 20:32:24 +053026 "github.com/stretchr/testify/assert"
sbarbari17d7e222019-11-05 10:02:29 -050027 "math/rand"
28 "reflect"
29 "strconv"
30 "sync"
31 "testing"
32)
33
34var (
35 BenchmarkProxy_Root *root
36 BenchmarkProxy_DeviceProxy *Proxy
37 BenchmarkProxy_PLT *proxyLoadTest
38 BenchmarkProxy_Logger log.Logger
39)
40
41type proxyLoadChanges struct {
42 ID string
43 Before interface{}
44 After interface{}
45}
46type proxyLoadTest struct {
47 mutex sync.RWMutex
48
49 addMutex sync.RWMutex
50 addedDevices []string
51
52 firmwareMutex sync.RWMutex
53 updatedFirmwares []proxyLoadChanges
54 flowMutex sync.RWMutex
55 updatedFlows []proxyLoadChanges
56
57 preAddExecuted bool
58 postAddExecuted bool
59 preUpdateExecuted bool
60 postUpdateExecuted bool
61}
62
63func (plt *proxyLoadTest) SetPreAddExecuted(status bool) {
64 plt.mutex.Lock()
65 defer plt.mutex.Unlock()
66 plt.preAddExecuted = status
67}
68func (plt *proxyLoadTest) SetPostAddExecuted(status bool) {
69 plt.mutex.Lock()
70 defer plt.mutex.Unlock()
71 plt.postAddExecuted = status
72}
73func (plt *proxyLoadTest) SetPreUpdateExecuted(status bool) {
74 plt.mutex.Lock()
75 defer plt.mutex.Unlock()
76 plt.preUpdateExecuted = status
77}
78func (plt *proxyLoadTest) SetPostUpdateExecuted(status bool) {
79 plt.mutex.Lock()
80 defer plt.mutex.Unlock()
81 plt.postUpdateExecuted = status
82}
83
84func init() {
Thomas Lee Se5a44012019-11-07 20:32:24 +053085 var err error
sbarbari17d7e222019-11-05 10:02:29 -050086 BenchmarkProxy_Root = NewRoot(&voltha.Voltha{}, nil)
87
88 BenchmarkProxy_Logger, _ = log.AddPackage(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"})
89 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
90 //Setup default logger - applies for packages that do not have specific logger set
91 if _, err := log.SetDefaultLogger(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"}); err != nil {
92 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
93 }
94
95 // Update all loggers (provisioned via init) with a common field
96 if err := log.UpdateAllLoggers(log.Fields{"instanceId": "PLT"}); err != nil {
97 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
98 }
99 log.SetPackageLogLevel("github.com/opencord/voltha-go/db/model", log.DebugLevel)
100
Thomas Lee Se5a44012019-11-07 20:32:24 +0530101 if BenchmarkProxy_DeviceProxy, err = BenchmarkProxy_Root.node.CreateProxy(context.Background(), "/", false); err != nil {
102 log.With(log.Fields{"error": err}).Fatal("Cannot create benchmark proxy")
103 }
sbarbari17d7e222019-11-05 10:02:29 -0500104 // Register ADD instructions callbacks
105 BenchmarkProxy_PLT = &proxyLoadTest{}
106
107 BenchmarkProxy_DeviceProxy.RegisterCallback(PRE_ADD, commonCallbackFunc, "PRE_ADD", BenchmarkProxy_PLT.SetPreAddExecuted)
108 BenchmarkProxy_DeviceProxy.RegisterCallback(POST_ADD, commonCallbackFunc, "POST_ADD", BenchmarkProxy_PLT.SetPostAddExecuted)
109
110 //// Register UPDATE instructions callbacks
111 BenchmarkProxy_DeviceProxy.RegisterCallback(PRE_UPDATE, commonCallbackFunc, "PRE_UPDATE", BenchmarkProxy_PLT.SetPreUpdateExecuted)
112 BenchmarkProxy_DeviceProxy.RegisterCallback(POST_UPDATE, commonCallbackFunc, "POST_UPDATE", BenchmarkProxy_PLT.SetPostUpdateExecuted)
113
114}
115
116func BenchmarkProxy_AddDevice(b *testing.B) {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530117 var err error
sbarbari17d7e222019-11-05 10:02:29 -0500118 defer GetProfiling().Report()
119 b.RunParallel(func(pb *testing.PB) {
120 b.Log("Started adding devices")
121 for pb.Next() {
122 ltPorts := []*voltha.Port{
123 {
124 PortNo: 123,
125 Label: "lt-port-0",
126 Type: voltha.Port_PON_OLT,
127 AdminState: common.AdminState_ENABLED,
128 OperStatus: common.OperStatus_ACTIVE,
129 DeviceId: "lt-port-0-device-id",
130 Peers: []*voltha.Port_PeerPort{},
131 },
132 }
133
134 ltStats := &openflow_13.OfpFlowStats{
135 Id: 1000,
136 }
137 ltFlows := &openflow_13.Flows{
138 Items: []*openflow_13.OfpFlowStats{ltStats},
139 }
140 ltDevice := &voltha.Device{
141 Id: "",
142 Type: "simulated_olt",
143 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
144 AdminState: voltha.AdminState_PREPROVISIONED,
145 Flows: ltFlows,
146 Ports: ltPorts,
147 }
148
149 ltDevIDBin, _ := uuid.New().MarshalBinary()
150 ltDevID := "0001" + hex.EncodeToString(ltDevIDBin)[:12]
151 ltDevice.Id = ltDevID
152
153 BenchmarkProxy_PLT.SetPreAddExecuted(false)
154 BenchmarkProxy_PLT.SetPostAddExecuted(false)
155
156 var added interface{}
157 // Add the device
Thomas Lee Se5a44012019-11-07 20:32:24 +0530158 if added, err = BenchmarkProxy_DeviceProxy.AddWithID(context.Background(), "/devices", ltDevID, ltDevice, ""); err != nil {
159 log.With(log.Fields{"error": err}).Fatal("Cannot create proxy")
160 }
161 if added == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500162 BenchmarkProxy_Logger.Errorf("Failed to add device: %+v", ltDevice)
163 continue
164 } else {
165 BenchmarkProxy_Logger.Infof("Device was added 1: %+v", added)
166 }
167
168 BenchmarkProxy_PLT.addMutex.Lock()
169 BenchmarkProxy_PLT.addedDevices = append(BenchmarkProxy_PLT.addedDevices, added.(*voltha.Device).Id)
170 BenchmarkProxy_PLT.addMutex.Unlock()
171 }
172 })
173
174 BenchmarkProxy_Logger.Infof("Number of added devices : %d", len(BenchmarkProxy_PLT.addedDevices))
175}
176
177func BenchmarkProxy_UpdateFirmware(b *testing.B) {
178 b.RunParallel(func(pb *testing.PB) {
179 for pb.Next() {
180 //for i:=0; i < b.N; i++ {
181
182 if len(BenchmarkProxy_PLT.addedDevices) > 0 {
183 var target interface{}
184 randomID := BenchmarkProxy_PLT.addedDevices[rand.Intn(len(BenchmarkProxy_PLT.addedDevices))]
Thomas Lee Se5a44012019-11-07 20:32:24 +0530185 firmProxy, err := BenchmarkProxy_Root.node.CreateProxy(context.Background(), "/", false)
186 if err != nil {
187 log.With(log.Fields{"error": err}).Fatal("Cannot create firmware proxy")
188 }
189 target, err = firmProxy.Get(context.Background(), "/devices/"+randomID, 0, false,
190 "")
191 if err != nil {
192 BenchmarkProxy_Logger.Errorf("Failed to create target due to error %v", err)
193 assert.NotNil(b, err)
194 }
195 if !reflect.ValueOf(target).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500196 BenchmarkProxy_Logger.Errorf("Failed to find device: %s %+v", randomID, target)
197 continue
198 }
199
200 BenchmarkProxy_PLT.SetPreUpdateExecuted(false)
201 BenchmarkProxy_PLT.SetPostUpdateExecuted(false)
202 firmProxy.RegisterCallback(PRE_UPDATE, commonCallbackFunc, "PRE_UPDATE", BenchmarkProxy_PLT.SetPreUpdateExecuted)
203 firmProxy.RegisterCallback(POST_UPDATE, commonCallbackFunc, "POST_UPDATE", BenchmarkProxy_PLT.SetPostUpdateExecuted)
204
205 var fwVersion int
206
207 before := target.(*voltha.Device).FirmwareVersion
208 if target.(*voltha.Device).FirmwareVersion == "n/a" {
209 fwVersion = 0
210 } else {
211 fwVersion, _ = strconv.Atoi(target.(*voltha.Device).FirmwareVersion)
212 fwVersion++
213 }
214
215 target.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
216 after := target.(*voltha.Device).FirmwareVersion
217
218 var updated interface{}
Thomas Lee Se5a44012019-11-07 20:32:24 +0530219 if updated, err = firmProxy.Update(context.Background(), "/devices/"+randomID, target.(*voltha.Device), false, ""); err != nil {
220 BenchmarkProxy_Logger.Errorf("Failed to update firmware proxy due to error %v", err)
221 assert.NotNil(b, err)
222 }
223 if updated == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500224 BenchmarkProxy_Logger.Errorf("Failed to update device: %+v", target)
225 continue
226 } else {
227 BenchmarkProxy_Logger.Infof("Device was updated : %+v", updated)
228
229 }
230
Thomas Lee Se5a44012019-11-07 20:32:24 +0530231 d, err := firmProxy.Get(context.Background(), "/devices/"+randomID, 0, false, "")
232 if err != nil {
233 BenchmarkProxy_Logger.Errorf("Failed to get device info from firmware proxy due to error %v", err)
234 assert.NotNil(b, err)
235 }
236 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500237 BenchmarkProxy_Logger.Errorf("Failed to get device: %s", randomID)
238 continue
239 } else if d.(*voltha.Device).FirmwareVersion == after {
240 BenchmarkProxy_Logger.Infof("Imm Device was updated with new value: %s %+v", randomID, d)
241 } else if d.(*voltha.Device).FirmwareVersion == before {
242 BenchmarkProxy_Logger.Errorf("Imm Device kept old value: %s %+v %+v", randomID, d, target)
243 } else {
244 BenchmarkProxy_Logger.Errorf("Imm Device has unknown value: %s %+v %+v", randomID, d, target)
245 }
246
247 BenchmarkProxy_PLT.firmwareMutex.Lock()
248
249 BenchmarkProxy_PLT.updatedFirmwares = append(
250 BenchmarkProxy_PLT.updatedFirmwares,
251 proxyLoadChanges{ID: randomID, Before: before, After: after},
252 )
253 BenchmarkProxy_PLT.firmwareMutex.Unlock()
254 }
255 }
256 })
257}
258
259func traverseBranches(revision Revision, depth int) {
260 if revision == nil {
261 return
262 }
263 prefix := strconv.Itoa(depth) + " ~~~~ "
264 for i := 0; i < depth; i++ {
265 prefix += " "
266 }
267
268 BenchmarkProxy_Logger.Debugf("%sRevision: %s %+v", prefix, revision.GetHash(), revision.GetData())
269
270 //for brIdx, brRev := range revision.GetBranch().Revisions {
271 // BenchmarkProxy_Logger.Debugf("%sbranchIndex: %s", prefix, brIdx)
272 // traverseBranches(brRev, depth+1)
273 //}
274 for childrenI, children := range revision.GetAllChildren() {
275 BenchmarkProxy_Logger.Debugf("%schildrenIndex: %s, length: %d", prefix, childrenI, len(children))
276
277 for _, subrev := range children {
278 //subrev.GetBranch().Latest
279 traverseBranches(subrev, depth+1)
280 }
281 }
282
283}
284func BenchmarkProxy_UpdateFlows(b *testing.B) {
285 b.RunParallel(func(pb *testing.PB) {
286 for pb.Next() {
287 if len(BenchmarkProxy_PLT.addedDevices) > 0 {
288 randomID := BenchmarkProxy_PLT.addedDevices[rand.Intn(len(BenchmarkProxy_PLT.addedDevices))]
289
Thomas Lee Se5a44012019-11-07 20:32:24 +0530290 flowsProxy, err := BenchmarkProxy_Root.node.CreateProxy(context.Background(), "/devices/"+randomID+"/flows", false)
291 if err != nil {
292 log.With(log.Fields{"error": err}).Fatal("Cannot create flows proxy")
293 }
294 flows, err := flowsProxy.Get(context.Background(), "/", 0, false, "")
295 if err != nil {
296 BenchmarkProxy_Logger.Errorf("Failed to get flows from flows proxy due to error: %v", err)
297 assert.NotNil(b, err)
298 }
sbarbari17d7e222019-11-05 10:02:29 -0500299
300 before := flows.(*openflow_13.Flows).Items[0].TableId
301 flows.(*openflow_13.Flows).Items[0].TableId = uint32(rand.Intn(3000))
302 after := flows.(*openflow_13.Flows).Items[0].TableId
303
304 flowsProxy.RegisterCallback(
305 PRE_UPDATE,
306 commonCallback2,
307 )
308 flowsProxy.RegisterCallback(
309 POST_UPDATE,
310 commonCallback2,
311 )
312
313 var updated interface{}
Thomas Lee Se5a44012019-11-07 20:32:24 +0530314 if updated, err = flowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, ""); err != nil {
315 BenchmarkProxy_Logger.Errorf("Cannot update flows proxy due to error: %v", err)
316 assert.NotNil(b, err)
317 }
318 if updated == nil {
sbarbari17d7e222019-11-05 10:02:29 -0500319 b.Errorf("Failed to update flows for device: %+v", flows)
320 } else {
321 BenchmarkProxy_Logger.Infof("Flows were updated : %+v", updated)
322 }
323 BenchmarkProxy_PLT.flowMutex.Lock()
324 BenchmarkProxy_PLT.updatedFlows = append(
325 BenchmarkProxy_PLT.updatedFlows,
326 proxyLoadChanges{ID: randomID, Before: before, After: after},
327 )
328 BenchmarkProxy_PLT.flowMutex.Unlock()
329 }
330 }
331 })
332}
333
334func BenchmarkProxy_GetDevices(b *testing.B) {
335 //traverseBranches(BenchmarkProxy_DeviceProxy.Root.node.Branches[NONE].GetLatest(), 0)
336
337 for i := 0; i < len(BenchmarkProxy_PLT.addedDevices); i++ {
338 devToGet := BenchmarkProxy_PLT.addedDevices[i]
339 // Verify that the added device can now be retrieved
Thomas Lee Se5a44012019-11-07 20:32:24 +0530340 d, err := BenchmarkProxy_DeviceProxy.Get(context.Background(), "/devices/"+devToGet, 0, false, "")
341 if err != nil {
342 BenchmarkProxy_Logger.Errorf("Failed to get device info from device proxy due to error: %v", err)
343 assert.NotNil(b, err)
344 }
345 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500346 BenchmarkProxy_Logger.Errorf("Failed to get device: %s", devToGet)
347 continue
348 } else {
349 BenchmarkProxy_Logger.Infof("Got device: %s %+v", devToGet, d)
350 }
351 }
352}
353
354func BenchmarkProxy_GetUpdatedFirmware(b *testing.B) {
355 for i := 0; i < len(BenchmarkProxy_PLT.updatedFirmwares); i++ {
356 devToGet := BenchmarkProxy_PLT.updatedFirmwares[i].ID
357 // Verify that the updated device can be retrieved and that the updates were actually applied
Thomas Lee Se5a44012019-11-07 20:32:24 +0530358 d, err := BenchmarkProxy_DeviceProxy.Get(context.Background(), "/devices/"+devToGet, 0, false, "")
359 if err != nil {
360 BenchmarkProxy_Logger.Errorf("Failed to get device info from device proxy due to error: %v", err)
361 assert.NotNil(b, err)
362 }
363 if !reflect.ValueOf(d).IsValid() {
sbarbari17d7e222019-11-05 10:02:29 -0500364 BenchmarkProxy_Logger.Errorf("Failed to get device: %s", devToGet)
365 continue
366 } else if d.(*voltha.Device).FirmwareVersion == BenchmarkProxy_PLT.updatedFirmwares[i].After.(string) {
367 BenchmarkProxy_Logger.Infof("Device was updated with new value: %s %+v", devToGet, d)
368 } else if d.(*voltha.Device).FirmwareVersion == BenchmarkProxy_PLT.updatedFirmwares[i].Before.(string) {
369 BenchmarkProxy_Logger.Errorf("Device kept old value: %s %+v %+v", devToGet, d, BenchmarkProxy_PLT.updatedFirmwares[i])
370 } else {
371 BenchmarkProxy_Logger.Errorf("Device has unknown value: %s %+v %+v", devToGet, d, BenchmarkProxy_PLT.updatedFirmwares[i])
372 }
373 }
374}