blob: f44a6ae13279d3e4e749749f38b3a0ea8910d0e9 [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 (
19 "encoding/hex"
20 "github.com/google/uuid"
21 "github.com/opencord/voltha-go/common/log"
William Kurkiandaa6bb22019-03-07 12:26:28 -050022 "github.com/opencord/voltha-protos/go/common"
23 "github.com/opencord/voltha-protos/go/openflow_13"
24 "github.com/opencord/voltha-protos/go/voltha"
Stephane Barbariedc5022d2018-11-19 15:21:44 -050025 "math/rand"
26 "reflect"
27 "strconv"
28 "sync"
29 "testing"
30)
31
32var (
Stephane Barbarie1039ec42019-02-04 10:43:16 -050033 BenchmarkProxy_Root *root
34 BenchmarkProxy_DeviceProxy *Proxy
35 BenchmarkProxy_PLT *proxyLoadTest
36 BenchmarkProxy_Logger log.Logger
Stephane Barbariedc5022d2018-11-19 15:21:44 -050037)
38
39type proxyLoadChanges struct {
40 ID string
41 Before interface{}
42 After interface{}
43}
44type proxyLoadTest struct {
45 sync.RWMutex
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040046 addedDevices []string
Stephane Barbariedc5022d2018-11-19 15:21:44 -050047 updatedFirmwares []proxyLoadChanges
48 updatedFlows []proxyLoadChanges
49 preAddExecuted bool
50 postAddExecuted bool
51 preUpdateExecuted bool
52 postUpdateExecuted bool
53}
54
55func (plt *proxyLoadTest) SetPreAddExecuted(status bool) {
56 plt.Lock()
57 defer plt.Unlock()
58 plt.preAddExecuted = status
59}
60func (plt *proxyLoadTest) SetPostAddExecuted(status bool) {
61 plt.Lock()
62 defer plt.Unlock()
63 plt.postAddExecuted = status
64}
65func (plt *proxyLoadTest) SetPreUpdateExecuted(status bool) {
66 plt.Lock()
67 defer plt.Unlock()
68 plt.preUpdateExecuted = status
69}
70func (plt *proxyLoadTest) SetPostUpdateExecuted(status bool) {
71 plt.Lock()
72 defer plt.Unlock()
73 plt.postUpdateExecuted = status
74}
Stephane Barbarie1039ec42019-02-04 10:43:16 -050075
Stephane Barbariedc5022d2018-11-19 15:21:44 -050076func init() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -050077 BenchmarkProxy_Root = NewRoot(&voltha.Voltha{}, nil)
Stephane Barbariedc5022d2018-11-19 15:21:44 -050078
Stephane Barbarie1039ec42019-02-04 10:43:16 -050079 BenchmarkProxy_Logger, _ = log.AddPackage(log.JSON, log.InfoLevel, nil)
80 //log.UpdateAllLoggers(log.Fields{"instanceId": "PROXY_LOAD_TEST"})
81
82 BenchmarkProxy_DeviceProxy = BenchmarkProxy_Root.node.CreateProxy("/", false)
Stephane Barbariedc5022d2018-11-19 15:21:44 -050083 // Register ADD instructions callbacks
Stephane Barbarie1039ec42019-02-04 10:43:16 -050084 BenchmarkProxy_PLT = &proxyLoadTest{}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050085
Stephane Barbarie1039ec42019-02-04 10:43:16 -050086 BenchmarkProxy_DeviceProxy.RegisterCallback(PRE_ADD, commonCallbackFunc, "PRE_ADD", BenchmarkProxy_PLT.SetPreAddExecuted)
87 BenchmarkProxy_DeviceProxy.RegisterCallback(POST_ADD, commonCallbackFunc, "POST_ADD", BenchmarkProxy_PLT.SetPostAddExecuted)
Stephane Barbariedc5022d2018-11-19 15:21:44 -050088
89 //// Register UPDATE instructions callbacks
Stephane Barbarie1039ec42019-02-04 10:43:16 -050090 BenchmarkProxy_DeviceProxy.RegisterCallback(PRE_UPDATE, commonCallbackFunc, "PRE_UPDATE", BenchmarkProxy_PLT.SetPreUpdateExecuted)
91 BenchmarkProxy_DeviceProxy.RegisterCallback(POST_UPDATE, commonCallbackFunc, "POST_UPDATE", BenchmarkProxy_PLT.SetPostUpdateExecuted)
Stephane Barbariedc5022d2018-11-19 15:21:44 -050092
93}
94
Stephane Barbarie1039ec42019-02-04 10:43:16 -050095func BenchmarkProxy_AddDevice(b *testing.B) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050096 defer GetProfiling().Report()
97 b.RunParallel(func(pb *testing.PB) {
98 b.Log("Started adding devices")
99 for pb.Next() {
100 ltPorts := []*voltha.Port{
101 {
102 PortNo: 123,
103 Label: "lt-port-0",
104 Type: voltha.Port_PON_OLT,
105 AdminState: common.AdminState_ENABLED,
106 OperStatus: common.OperStatus_ACTIVE,
107 DeviceId: "lt-port-0-device-id",
108 Peers: []*voltha.Port_PeerPort{},
109 },
110 }
111
112 ltStats := &openflow_13.OfpFlowStats{
113 Id: 1000,
114 }
115 ltFlows := &openflow_13.Flows{
116 Items: []*openflow_13.OfpFlowStats{ltStats},
117 }
118 ltDevice := &voltha.Device{
119 Id: "",
120 Type: "simulated_olt",
121 Address: &voltha.Device_HostAndPort{HostAndPort: "1.2.3.4:5555"},
122 AdminState: voltha.AdminState_PREPROVISIONED,
123 Flows: ltFlows,
124 Ports: ltPorts,
125 }
126
127 ltDevIDBin, _ := uuid.New().MarshalBinary()
128 ltDevID := "0001" + hex.EncodeToString(ltDevIDBin)[:12]
129 ltDevice.Id = ltDevID
130
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500131 BenchmarkProxy_PLT.SetPreAddExecuted(false)
132 BenchmarkProxy_PLT.SetPostAddExecuted(false)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500133
134 var added interface{}
135 // Add the device
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500136 if added = BenchmarkProxy_DeviceProxy.AddWithID("/devices", ltDevID, ltDevice, ""); added == nil {
137 BenchmarkProxy_Logger.Errorf("Failed to add device: %+v", ltDevice)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500138 continue
139 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500140 BenchmarkProxy_Logger.Infof("Device was added 1: %+v", added)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500141 }
142
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500143 BenchmarkProxy_PLT.Lock()
144 BenchmarkProxy_PLT.addedDevices = append(BenchmarkProxy_PLT.addedDevices, added.(*voltha.Device).Id)
145 BenchmarkProxy_PLT.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500146 }
147 })
148
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500149 BenchmarkProxy_Logger.Infof("Number of added devices : %d", len(BenchmarkProxy_PLT.addedDevices))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500150}
151
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500152func BenchmarkProxy_UpdateFirmware(b *testing.B) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500153 b.RunParallel(func(pb *testing.PB) {
154 for pb.Next() {
155 //for i:=0; i < b.N; i++ {
156
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500157 if len(BenchmarkProxy_PLT.addedDevices) > 0 {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500158 var target interface{}
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500159 randomID := BenchmarkProxy_PLT.addedDevices[rand.Intn(len(BenchmarkProxy_PLT.addedDevices))]
160 firmProxy := BenchmarkProxy_Root.node.CreateProxy("/", false)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500161 if target = firmProxy.Get("/devices/"+randomID, 0, false,
162 ""); !reflect.ValueOf(target).IsValid() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500163 BenchmarkProxy_Logger.Errorf("Failed to find device: %s %+v", randomID, target)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500164 continue
165 }
166
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500167 BenchmarkProxy_PLT.SetPreUpdateExecuted(false)
168 BenchmarkProxy_PLT.SetPostUpdateExecuted(false)
169 firmProxy.RegisterCallback(PRE_UPDATE, commonCallbackFunc, "PRE_UPDATE", BenchmarkProxy_PLT.SetPreUpdateExecuted)
170 firmProxy.RegisterCallback(POST_UPDATE, commonCallbackFunc, "POST_UPDATE", BenchmarkProxy_PLT.SetPostUpdateExecuted)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500171
172 var fwVersion int
173
174 before := target.(*voltha.Device).FirmwareVersion
175 if target.(*voltha.Device).FirmwareVersion == "n/a" {
176 fwVersion = 0
177 } else {
178 fwVersion, _ = strconv.Atoi(target.(*voltha.Device).FirmwareVersion)
179 fwVersion++
180 }
181
182 target.(*voltha.Device).FirmwareVersion = strconv.Itoa(fwVersion)
183 after := target.(*voltha.Device).FirmwareVersion
184
185 var updated interface{}
186 if updated = firmProxy.Update("/devices/"+randomID, target.(*voltha.Device), false,
187 ""); updated == nil {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500188 BenchmarkProxy_Logger.Errorf("Failed to update device: %+v", target)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500189 continue
190 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500191 BenchmarkProxy_Logger.Infof("Device was updated : %+v", updated)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500192
193 }
194
195 if d := firmProxy.Get("/devices/"+randomID, 0, false,
196 ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500197 BenchmarkProxy_Logger.Errorf("Failed to get device: %s", randomID)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500198 continue
199 } else if d.(*voltha.Device).FirmwareVersion == after {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500200 BenchmarkProxy_Logger.Infof("Imm Device was updated with new value: %s %+v", randomID, d)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500201 } else if d.(*voltha.Device).FirmwareVersion == before {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500202 BenchmarkProxy_Logger.Errorf("Imm Device kept old value: %s %+v %+v", randomID, d, target)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500203 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500204 BenchmarkProxy_Logger.Errorf("Imm Device has unknown value: %s %+v %+v", randomID, d, target)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500205 }
206
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500207 BenchmarkProxy_PLT.Lock()
208 BenchmarkProxy_PLT.updatedFirmwares = append(
209 BenchmarkProxy_PLT.updatedFirmwares,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500210 proxyLoadChanges{ID: randomID, Before: before, After: after},
211 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500212 BenchmarkProxy_PLT.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500213 }
214 }
215 })
216}
217
218func traverseBranches(revision Revision, depth int) {
219 if revision == nil {
220 return
221 }
222 prefix := strconv.Itoa(depth) + " ~~~~ "
223 for i := 0; i < depth; i++ {
224 prefix += " "
225 }
226
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500227 BenchmarkProxy_Logger.Debugf("%sRevision: %s %+v", prefix, revision.GetHash(), revision.GetData())
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500228
229 //for brIdx, brRev := range revision.GetBranch().Revisions {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500230 // BenchmarkProxy_Logger.Debugf("%sbranchIndex: %s", prefix, brIdx)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500231 // traverseBranches(brRev, depth+1)
232 //}
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500233 for childrenI, children := range revision.GetAllChildren() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500234 BenchmarkProxy_Logger.Debugf("%schildrenIndex: %s, length: %d", prefix, childrenI, len(children))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500235
236 for _, subrev := range children {
237 //subrev.GetBranch().Latest
238 traverseBranches(subrev, depth+1)
239 }
240 }
241
242}
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500243func BenchmarkProxy_UpdateFlows(b *testing.B) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500244 b.RunParallel(func(pb *testing.PB) {
245 for pb.Next() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500246 if len(BenchmarkProxy_PLT.addedDevices) > 0 {
247 randomID := BenchmarkProxy_PLT.addedDevices[rand.Intn(len(BenchmarkProxy_PLT.addedDevices))]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500248
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500249 flowsProxy := BenchmarkProxy_Root.node.CreateProxy("/devices/"+randomID+"/flows", false)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500250 flows := flowsProxy.Get("/", 0, false, "")
251
252 before := flows.(*openflow_13.Flows).Items[0].TableId
253 flows.(*openflow_13.Flows).Items[0].TableId = uint32(rand.Intn(3000))
254 after := flows.(*openflow_13.Flows).Items[0].TableId
255
256 flowsProxy.RegisterCallback(
257 PRE_UPDATE,
258 commonCallback2,
259 )
260 flowsProxy.RegisterCallback(
261 POST_UPDATE,
262 commonCallback2,
263 )
264
265 var updated interface{}
266 if updated = flowsProxy.Update("/", flows.(*openflow_13.Flows), false, ""); updated == nil {
267 b.Errorf("Failed to update flows for device: %+v", flows)
268 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500269 BenchmarkProxy_Logger.Infof("Flows were updated : %+v", updated)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500270 }
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500271 BenchmarkProxy_PLT.Lock()
272 BenchmarkProxy_PLT.updatedFlows = append(
273 BenchmarkProxy_PLT.updatedFlows,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500274 proxyLoadChanges{ID: randomID, Before: before, After: after},
275 )
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500276 BenchmarkProxy_PLT.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500277 }
278 }
279 })
280}
281
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500282func BenchmarkProxy_GetDevices(b *testing.B) {
283 //traverseBranches(BenchmarkProxy_DeviceProxy.Root.node.Branches[NONE].GetLatest(), 0)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500284
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500285 for i := 0; i < len(BenchmarkProxy_PLT.addedDevices); i++ {
286 devToGet := BenchmarkProxy_PLT.addedDevices[i]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500287 // Verify that the added device can now be retrieved
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500288 if d := BenchmarkProxy_DeviceProxy.Get("/devices/"+devToGet, 0, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500289 ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500290 BenchmarkProxy_Logger.Errorf("Failed to get device: %s", devToGet)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500291 continue
292 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500293 BenchmarkProxy_Logger.Infof("Got device: %s %+v", devToGet, d)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500294 }
295 }
296}
297
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500298func BenchmarkProxy_GetUpdatedFirmware(b *testing.B) {
299 for i := 0; i < len(BenchmarkProxy_PLT.updatedFirmwares); i++ {
300 devToGet := BenchmarkProxy_PLT.updatedFirmwares[i].ID
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500301 // Verify that the updated device can be retrieved and that the updates were actually applied
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500302 if d := BenchmarkProxy_DeviceProxy.Get("/devices/"+devToGet, 0, false,
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500303 ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500304 BenchmarkProxy_Logger.Errorf("Failed to get device: %s", devToGet)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500305 continue
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500306 } else if d.(*voltha.Device).FirmwareVersion == BenchmarkProxy_PLT.updatedFirmwares[i].After.(string) {
307 BenchmarkProxy_Logger.Infof("Device was updated with new value: %s %+v", devToGet, d)
308 } else if d.(*voltha.Device).FirmwareVersion == BenchmarkProxy_PLT.updatedFirmwares[i].Before.(string) {
309 BenchmarkProxy_Logger.Errorf("Device kept old value: %s %+v %+v", devToGet, d, BenchmarkProxy_PLT.updatedFirmwares[i])
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500310 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500311 BenchmarkProxy_Logger.Errorf("Device has unknown value: %s %+v %+v", devToGet, d, BenchmarkProxy_PLT.updatedFirmwares[i])
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500312 }
313 }
314}
315
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500316func BenchmarkProxy_GetUpdatedFlows(b *testing.B) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500317 var d interface{}
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500318 for i := 0; i < len(BenchmarkProxy_PLT.updatedFlows); i++ {
319 devToGet := BenchmarkProxy_PLT.updatedFlows[i].ID
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500320 // Verify that the updated device can be retrieved and that the updates were actually applied
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500321 flowsProxy := BenchmarkProxy_Root.node.CreateProxy("/devices/"+devToGet+"/flows", false)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500322 if d = flowsProxy.Get("/", 0, false,
323 ""); !reflect.ValueOf(d).IsValid() {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500324 BenchmarkProxy_Logger.Errorf("Failed to get device flows: %s", devToGet)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500325 continue
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500326 } else if d.(*openflow_13.Flows).Items[0].TableId == BenchmarkProxy_PLT.updatedFlows[i].After.(uint32) {
327 BenchmarkProxy_Logger.Infof("Device was updated with new flow value: %s %+v", devToGet, d)
328 } else if d.(*openflow_13.Flows).Items[0].TableId == BenchmarkProxy_PLT.updatedFlows[i].Before.(uint32) {
329 BenchmarkProxy_Logger.Errorf("Device kept old flow value: %s %+v %+v", devToGet, d, BenchmarkProxy_PLT.updatedFlows[i])
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500330 } else {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500331 BenchmarkProxy_Logger.Errorf("Device has unknown flow value: %s %+v %+v", devToGet, d, BenchmarkProxy_PLT.updatedFlows[i])
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500332 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500333 }
334}