forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
188 lines (163 loc) · 4.82 KB
/
bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2019 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kvscheduler_test
import (
"context"
"flag"
"fmt"
"strconv"
"testing"
"go.ligato.io/cn-infra/v2/logging"
_ "go.ligato.io/cn-infra/v2/logging/logrus" // for setting default registry
mock_ifplugin "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/ifplugin"
"go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/ifplugin/model"
mock_l2plugin "go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/l2plugin"
"go.ligato.io/vpp-agent/v3/examples/kvscheduler/mock_plugins/l2plugin/model"
"go.ligato.io/vpp-agent/v3/pkg/models"
. "go.ligato.io/vpp-agent/v3/plugins/kvscheduler"
. "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
)
/*
------------------------
KVScheduler benchmarks
------------------------
- starts scheduler together with mocked ifplugin and l2plugin
- configures 1/10/100/1000 number of interfaces with bridge domain
How to run:
- build test binary `go test -c`
- run all benchmarks: `./kvscheduler.test -test.run=XXX -test.bench=.`
- with CPU profile: `./kvscheduler.test -test.run=XXX -test.bench=. -test.cpuprofile=cpu.out`
- analyze profile: `go tool pprof cpu.out`
- with mem profile: `./kvscheduler.test -test.run=XXX -test.bench=. -memprofile mem.out`
- analyze profile: `go tool pprof -alloc_space mem.out`
- with trace profile: `./kvscheduler.test -test.run=XXX -test.bench=. -trace trace.out`
- analyze profile: `go tool trace -http=:6060 trace.out`
*/
func BenchmarkScale(b *testing.B) {
benchmarkScale(b, true)
}
func BenchmarkScaleWithoutSimulation(b *testing.B) {
benchmarkScale(b, false)
}
func benchmarkScale(b *testing.B, withSimulation bool) {
for _, n := range [...]int{1, 10, 100, 1000} {
b.Run(strconv.Itoa(n), func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := runScale(n, withSimulation)
if err != nil {
b.Fatal(err)
}
}
})
}
}
// result should be saved to global variable to prevent compiler optimization
var seqNum uint64
func runScale(n int, withSimulation bool) error {
c := setupScale()
defer teardownScale(c)
// run non-resync transaction against empty SB
txn := c.scheduler.StartNBTransaction()
// create single bridge domain
valBd := &mock_l2.BridgeDomain{
Name: fmt.Sprintf("bd-%d", 1),
}
// create n interfaces for bridge domain
for i := 0; i < n; i++ {
valIface := &mock_interfaces.Interface{
Name: fmt.Sprintf("iface-%d", i),
Type: mock_interfaces.Interface_LOOPBACK,
}
valBd.Interfaces = append(valBd.Interfaces, &mock_l2.BridgeDomain_Interface{
Name: valIface.Name,
})
txn.SetValue(models.Key(valIface), valIface)
}
txn.SetValue(models.Key(valBd), valBd)
testCtx := context.Background()
if withSimulation {
testCtx = WithSimulation(testCtx)
}
seq, err := txn.Commit(WithDescription(testCtx, "benchmarking scale"))
if err != nil {
return err
}
seqNum = seq
return err
}
func setupScale() *runCtx {
// prepare run context
c := newRunCtx()
if err := c.Init(); err != nil {
panic(err)
}
return c
}
func teardownScale(c *runCtx) {
if err := c.Close(); err != nil {
panic(err)
}
}
type runCtx struct {
scheduler *Scheduler
IfPlugin *mock_ifplugin.IfPlugin
L2Plugin *mock_l2plugin.L2Plugin
}
func newRunCtx() *runCtx {
c := &runCtx{}
c.scheduler = NewPlugin(UseDeps(func(deps *Deps) {
deps.HTTPHandlers = nil
}))
c.IfPlugin = mock_ifplugin.NewPlugin(mock_ifplugin.UseDeps(
func(deps *mock_ifplugin.Deps) {
deps.KVScheduler = c.scheduler
}))
c.L2Plugin = mock_l2plugin.NewPlugin(mock_l2plugin.UseDeps(
func(deps *mock_l2plugin.Deps) {
deps.KVScheduler = c.scheduler
}))
return c
}
func (c *runCtx) Init() error {
if err := c.scheduler.Init(); err != nil {
return err
}
if err := c.IfPlugin.Init(); err != nil {
return err
}
if err := c.L2Plugin.Init(); err != nil {
return err
}
return nil
}
func (c *runCtx) Close() error {
if err := c.L2Plugin.Close(); err != nil {
return err
}
if err := c.IfPlugin.Close(); err != nil {
return err
}
if err := c.scheduler.Close(); err != nil {
return err
}
logging.DefaultRegistry.ClearRegistry()
return nil
}
var scaleFlag = flag.Int("scale", 10, "number of items for scale test")
func TestScale(t *testing.T) {
if err := runScale(*scaleFlag, true); err != nil {
t.Fatal(err)
}
}