blob: f4fd3255dda57acc6a74916ec56f6585bd454280 [file] [log] [blame]
Stephane Barbariedc5022d2018-11-19 15:21:44 -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 (
Stephane Barbarieef6650d2019-07-18 12:15:09 -040019 "context"
Stephane Barbariedc5022d2018-11-19 15:21:44 -050020 "encoding/hex"
21 "github.com/google/uuid"
22 "github.com/opencord/voltha-go/common/log"
William Kurkiandaa6bb22019-03-07 12:26:28 -050023 "github.com/opencord/voltha-protos/go/common"
24 "github.com/opencord/voltha-protos/go/openflow_13"
25 "github.com/opencord/voltha-protos/go/voltha"
Stephane Barbariedc5022d2018-11-19 15:21:44 -050026 "math/rand"
27 "reflect"
28 "strconv"
29 "sync"
30 "testing"
31)
32
33var (
Stephane Barbarie1039ec42019-02-04 10:43:16 -050034 BenchmarkProxy_Root *root
35 BenchmarkProxy_DeviceProxy *Proxy
36 BenchmarkProxy_PLT *proxyLoadTest
37 BenchmarkProxy_Logger log.Logger
Stephane Barbariedc5022d2018-11-19 15:21:44 -050038)
39
40type proxyLoadChanges struct {
41 ID string
42 Before interface{}
43 After interface{}
44}
45type proxyLoadTest struct {
Stephane Barbarieef6650d2019-07-18 12:15:09 -040046 mutex sync.RWMutex
47
48 addMutex sync.RWMutex
49 addedDevices []string
50
51 firmwareMutex sync.RWMutex
52 updatedFirmwares []proxyLoadChanges
53 flowMutex sync.RWMutex
54 updatedFlows []proxyLoadChanges
55
Stephane Barbariedc5022d2018-11-19 15:21:44 -050056 preAddExecuted bool
57 postAddExecuted bool
58 preUpdateExecuted bool
59 postUpdateExecuted bool
60}
61
62func (plt *proxyLoadTest) SetPreAddExecuted(status bool) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -040063 plt.mutex.Lock()
64 defer plt.mutex.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -050065 plt.preAddExecuted = status
66}
67func (plt *proxyLoadTest) SetPostAddExecuted(status bool) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -040068 plt.mutex.Lock()
69 defer plt.mutex.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -050070 plt.postAddExecuted = status
71}
72func (plt *proxyLoadTest) SetPreUpdateExecuted(status bool) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -040073 plt.mutex.Lock()
74 defer plt.mutex.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -050075 plt.preUpdateExecuted = status
76}
77func (plt *proxyLoadTest) SetPostUpdateExecuted(status bool) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -040078 plt.mutex.Lock()
79 defer plt.mutex.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -050080 plt.postUpdateExecuted = status
81}
Stephane Barbarie1039ec42019-02-04 10:43:16 -050082
Stephane Barbariedc5022d2018-11-19 15:21:44 -050083func init() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -050084 BenchmarkProxy_Root = NewRoot(&voltha.Voltha{}, nil)
Stephane Barbariedc5022d2018-11-19 15:21:44 -050085
Stephane Barbarieef6650d2019-07-18 12:15:09 -040086 BenchmarkProxy_Logger, _ = log.AddPackage(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"})
Stephane Barbarie1039ec42019-02-04 10:43:16 -050087 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
Stephane Barbarieef6650d2019-07-18 12:15:09 -040088 //Setup default logger - applies for packages that do not have specific logger set
89 if _, err := log.SetDefaultLogger(log.JSON, log.DebugLevel, log.Fields{"instanceId": "PLT"}); err != nil {
90 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
91 }
Stephane Barbarie1039ec42019-02-04 10:43:16 -050092
Stephane Barbarieef6650d2019-07-18 12:15:09 -040093 // Update all loggers (provisioned via init) with a common field
94 if err := log.UpdateAllLoggers(log.Fields{"instanceId": "PLT"}); err != nil {
95 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
96 }
97 log.SetPackageLogLevel("github.com/opencord/voltha-go/db/model", log.DebugLevel)
98
99 BenchmarkProxy_DeviceProxy = BenchmarkProxy_Root.node.CreateProxy(context.Background(), "/", false)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500100 // Register ADD instructions callbacks
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500101 BenchmarkProxy_PLT = &proxyLoadTest{}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500102
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500103 BenchmarkProxy_DeviceProxy.RegisterCallback(PRE_ADD, commonCallbackFunc, "PRE_ADD", BenchmarkProxy_PLT.SetPreAddExecuted)
104 BenchmarkProxy_DeviceProxy.RegisterCallback(POST_ADD, commonCallbackFunc, "POST_ADD", BenchmarkProxy_PLT.SetPostAddExecuted)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500105
106 //// Register UPDATE instructions callbacks
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500107 BenchmarkProxy_DeviceProxy.RegisterCallback(PRE_UPDATE, commonCallbackFunc, "PRE_UPDATE", BenchmarkProxy_PLT.SetPreUpdateExecuted)
108 BenchmarkProxy_DeviceProxy.RegisterCallback(POST_UPDATE, commonCallbackFunc, "POST_UPDATE", BenchmarkProxy_PLT.SetPostUpdateExecuted)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500109
110}
111
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500112func BenchmarkProxy_AddDevice(b *testing.B) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500113 defer GetProfiling().Report()
114 b.RunParallel(func(pb *testing.PB) {
115 b.Log("Started adding devices")
116 for pb.Next() {
117 ltPorts := []*voltha.Port{
118 {
119 PortNo: 123,
120 Label: "lt-port-0",
121 Type: voltha.Port_PON_OLT,
122 AdminState: common.AdminState_ENABLED,
123 OperStatus: common.OperStatus_ACTIVE,
124 DeviceId: "lt-port-0-device-id",
125 Peers: []*voltha.Port_PeerPort{},
126 },
127 }
128
129 ltStats := &openflow_13.OfpFlowStats{
130 Id: 1000,
131 }
132 ltFlows := &openflow_13.Flows{
133 Items: []*openflow_13.OfpFlowStats{ltStats},
134 }
135 ltDevice := &voltha.Device{
136 Id: "",
137 Type: "simulated_olt",
138 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
139 AdminState: voltha.AdminState_PREPROVISIONED,
140 Flows: ltFlows,
141 Ports: ltPorts,
142 }
143
144 ltDevIDBin, _ := uuid.New().MarshalBinary()
145 ltDevID := "0001" + hex.EncodeToString(ltDevIDBin)[:12]
146 ltDevice.Id = ltDevID
147
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500148 BenchmarkProxy_PLT.SetPreAddExecuted(false)
149 BenchmarkProxy_PLT.SetPostAddExecuted(false)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500150
151 var added interface{}
152 // Add the device
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400153 if added = BenchmarkProxy_DeviceProxy.AddWithID(context.Background(), "/devices", ltDevID, ltDevice, ""); added == nil {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500154 BenchmarkProxy_Logger.Errorf("Failed to add device: %+v", ltDevice)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500155 continue
156 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500157 BenchmarkProxy_Logger.Infof("Device was added 1: %+v", added)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500158 }
159
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400160 BenchmarkProxy_PLT.addMutex.Lock()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500161 BenchmarkProxy_PLT.addedDevices = append(BenchmarkProxy_PLT.addedDevices, added.(*voltha.Device).Id)
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400162 BenchmarkProxy_PLT.addMutex.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500163 }
164 })
165
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500166 BenchmarkProxy_Logger.Infof("Number of added devices : %d", len(BenchmarkProxy_PLT.addedDevices))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500167}
168
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500169func BenchmarkProxy_UpdateFirmware(b *testing.B) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500170 b.RunParallel(func(pb *testing.PB) {
171 for pb.Next() {
172 //for i:=0; i < b.N; i++ {
173
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500174 if len(BenchmarkProxy_PLT.addedDevices) > 0 {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500175 var target interface{}
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500176 randomID := BenchmarkProxy_PLT.addedDevices[rand.Intn(len(BenchmarkProxy_PLT.addedDevices))]
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400177 firmProxy := BenchmarkProxy_Root.node.CreateProxy(context.Background(), "/", false)
178 if target = firmProxy.Get(context.Background(), "/devices/"+randomID, 0, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500179 ""); !reflect.ValueOf(target).IsValid() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500180 BenchmarkProxy_Logger.Errorf("Failed to find device: %s %+v", randomID, target)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500181 continue
182 }
183
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500184 BenchmarkProxy_PLT.SetPreUpdateExecuted(false)
185 BenchmarkProxy_PLT.SetPostUpdateExecuted(false)
186 firmProxy.RegisterCallback(PRE_UPDATE, commonCallbackFunc, "PRE_UPDATE", BenchmarkProxy_PLT.SetPreUpdateExecuted)
187 firmProxy.RegisterCallback(POST_UPDATE, commonCallbackFunc, "POST_UPDATE", BenchmarkProxy_PLT.SetPostUpdateExecuted)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500188
189 var fwVersion int
190
191 before := target.(*voltha.Device).FirmwareVersion
192 if target.(*voltha.Device).FirmwareVersion == "n/a" {
193 fwVersion = 0
194 } else {
195 fwVersion, _ = strconv.Atoi(target.(*voltha.Device).FirmwareVersion)
196 fwVersion++
197 }
198
199 target.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
200 after := target.(*voltha.Device).FirmwareVersion
201
202 var updated interface{}
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400203 if updated = firmProxy.Update(context.Background(), "/devices/"+randomID, target.(*voltha.Device), false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500204 ""); updated == nil {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500205 BenchmarkProxy_Logger.Errorf("Failed to update device: %+v", target)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500206 continue
207 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500208 BenchmarkProxy_Logger.Infof("Device was updated : %+v", updated)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500209
210 }
211
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400212 if d := firmProxy.Get(context.Background(), "/devices/"+randomID, 0, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500213 ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500214 BenchmarkProxy_Logger.Errorf("Failed to get device: %s", randomID)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500215 continue
216 } else if d.(*voltha.Device).FirmwareVersion == after {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500217 BenchmarkProxy_Logger.Infof("Imm Device was updated with new value: %s %+v", randomID, d)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500218 } else if d.(*voltha.Device).FirmwareVersion == before {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500219 BenchmarkProxy_Logger.Errorf("Imm Device kept old value: %s %+v %+v", randomID, d, target)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500220 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500221 BenchmarkProxy_Logger.Errorf("Imm Device has unknown value: %s %+v %+v", randomID, d, target)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500222 }
223
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400224 BenchmarkProxy_PLT.firmwareMutex.Lock()
225
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500226 BenchmarkProxy_PLT.updatedFirmwares = append(
227 BenchmarkProxy_PLT.updatedFirmwares,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500228 proxyLoadChanges{ID: randomID, Before: before, After: after},
229 )
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400230 BenchmarkProxy_PLT.firmwareMutex.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500231 }
232 }
233 })
234}
235
236func traverseBranches(revision Revision, depth int) {
237 if revision == nil {
238 return
239 }
240 prefix := strconv.Itoa(depth) + " ~~~~ "
241 for i := 0; i < depth; i++ {
242 prefix += " "
243 }
244
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500245 BenchmarkProxy_Logger.Debugf("%sRevision: %s %+v", prefix, revision.GetHash(), revision.GetData())
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500246
247 //for brIdx, brRev := range revision.GetBranch().Revisions {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500248 // BenchmarkProxy_Logger.Debugf("%sbranchIndex: %s", prefix, brIdx)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500249 // traverseBranches(brRev, depth+1)
250 //}
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500251 for childrenI, children := range revision.GetAllChildren() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500252 BenchmarkProxy_Logger.Debugf("%schildrenIndex: %s, length: %d", prefix, childrenI, len(children))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500253
254 for _, subrev := range children {
255 //subrev.GetBranch().Latest
256 traverseBranches(subrev, depth+1)
257 }
258 }
259
260}
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500261func BenchmarkProxy_UpdateFlows(b *testing.B) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500262 b.RunParallel(func(pb *testing.PB) {
263 for pb.Next() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500264 if len(BenchmarkProxy_PLT.addedDevices) > 0 {
265 randomID := BenchmarkProxy_PLT.addedDevices[rand.Intn(len(BenchmarkProxy_PLT.addedDevices))]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500266
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400267 flowsProxy := BenchmarkProxy_Root.node.CreateProxy(context.Background(), "/devices/"+randomID+"/flows", false)
268 flows := flowsProxy.Get(context.Background(), "/", 0, false, "")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500269
270 before := flows.(*openflow_13.Flows).Items[0].TableId
271 flows.(*openflow_13.Flows).Items[0].TableId = uint32(rand.Intn(3000))
272 after := flows.(*openflow_13.Flows).Items[0].TableId
273
274 flowsProxy.RegisterCallback(
275 PRE_UPDATE,
276 commonCallback2,
277 )
278 flowsProxy.RegisterCallback(
279 POST_UPDATE,
280 commonCallback2,
281 )
282
283 var updated interface{}
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400284 if updated = flowsProxy.Update(context.Background(), "/", flows.(*openflow_13.Flows), false, ""); updated == nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500285 b.Errorf("Failed to update flows for device: %+v", flows)
286 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500287 BenchmarkProxy_Logger.Infof("Flows were updated : %+v", updated)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500288 }
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400289 BenchmarkProxy_PLT.flowMutex.Lock()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500290 BenchmarkProxy_PLT.updatedFlows = append(
291 BenchmarkProxy_PLT.updatedFlows,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500292 proxyLoadChanges{ID: randomID, Before: before, After: after},
293 )
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400294 BenchmarkProxy_PLT.flowMutex.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500295 }
296 }
297 })
298}
299
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500300func BenchmarkProxy_GetDevices(b *testing.B) {
301 //traverseBranches(BenchmarkProxy_DeviceProxy.Root.node.Branches[NONE].GetLatest(), 0)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500302
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500303 for i := 0; i < len(BenchmarkProxy_PLT.addedDevices); i++ {
304 devToGet := BenchmarkProxy_PLT.addedDevices[i]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500305 // Verify that the added device can now be retrieved
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400306 if d := BenchmarkProxy_DeviceProxy.Get(context.Background(), "/devices/"+devToGet, 0, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500307 ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500308 BenchmarkProxy_Logger.Errorf("Failed to get device: %s", devToGet)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500309 continue
310 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500311 BenchmarkProxy_Logger.Infof("Got device: %s %+v", devToGet, d)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500312 }
313 }
314}
315
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500316func BenchmarkProxy_GetUpdatedFirmware(b *testing.B) {
317 for i := 0; i < len(BenchmarkProxy_PLT.updatedFirmwares); i++ {
318 devToGet := BenchmarkProxy_PLT.updatedFirmwares[i].ID
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500319 // Verify that the updated device can be retrieved and that the updates were actually applied
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400320 if d := BenchmarkProxy_DeviceProxy.Get(context.Background(), "/devices/"+devToGet, 0, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500321 ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500322 BenchmarkProxy_Logger.Errorf("Failed to get device: %s", devToGet)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500323 continue
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500324 } else if d.(*voltha.Device).FirmwareVersion == BenchmarkProxy_PLT.updatedFirmwares[i].After.(string) {
325 BenchmarkProxy_Logger.Infof("Device was updated with new value: %s %+v", devToGet, d)
326 } else if d.(*voltha.Device).FirmwareVersion == BenchmarkProxy_PLT.updatedFirmwares[i].Before.(string) {
327 BenchmarkProxy_Logger.Errorf("Device kept old value: %s %+v %+v", devToGet, d, BenchmarkProxy_PLT.updatedFirmwares[i])
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500328 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500329 BenchmarkProxy_Logger.Errorf("Device has unknown value: %s %+v %+v", devToGet, d, BenchmarkProxy_PLT.updatedFirmwares[i])
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500330 }
331 }
332}