Skip to content

Instantly share code, notes, and snippets.

@cstockton
Created May 18, 2017 17:48
Show Gist options
  • Save cstockton/ce806b81f3fcf0af190bebd20ecdfdae to your computer and use it in GitHub Desktop.
Save cstockton/ce806b81f3fcf0af190bebd20ecdfdae to your computer and use it in GitHub Desktop.
package server
import (
"fmt"
"testing"
"time"
log "github.com/Sirupsen/logrus"
"github.com/osrg/gobgp/config"
"github.com/osrg/gobgp/packet/bgp"
"github.com/osrg/gobgp/table"
)
type testConfig struct {
*config.Global
*config.Neighbor
}
var (
testConfigs = [...]testConfig{
{
&config.Global{
Config: config.GlobalConfig{
As: 1,
RouterId: "1.1.1.1",
Port: 10179,
},
},
&config.Neighbor{
Config: config.NeighborConfig{
NeighborAddress: "127.0.0.1",
PeerAs: 2,
},
Transport: config.Transport{
Config: config.TransportConfig{
PassiveMode: true,
},
},
},
},
{
&config.Global{
Config: config.GlobalConfig{
As: 2,
RouterId: "2.2.2.2",
Port: -1,
},
},
&config.Neighbor{
Config: config.NeighborConfig{
NeighborAddress: "127.0.0.1",
PeerAs: 1,
},
Transport: config.Transport{
Config: config.TransportConfig{
RemotePort: 10179,
},
},
},
},
}
)
func MustStart(t testing.TB, idx int) *BgpServer {
s := NewBgpServer()
go s.Serve()
if idx > len(testConfigs) {
t.Fatalf(`exp idx %v within max bounds %v`, idx, len(testConfigs))
}
cfgG := testConfigs[idx].Global
if err := s.Start(cfgG); err != nil {
t.Fatalf(`exp nil err from Start using config %v; got %v`, idx, err)
}
return s
}
func BenchmarkBasic(b *testing.B) {
srv := MustStart(b, 1)
p1 := &table.Policy{Name: "p1"}
if err := srv.AddPolicy(p1, false); err != nil {
b.Fatalf(`exp nil err from AddPolicy; got %v`, err)
}
log.SetLevel(log.WarnLevel)
attrs := []bgp.PathAttributeInterface{
bgp.NewPathAttributeOrigin(0),
bgp.NewPathAttributeNextHop("10.0.0.1"),
}
var expEach int
for c := 0; c < 20; c++ {
for d := 0; d < 100; d++ {
for x := uint8(0); x < 5; x += 4 {
expEach++
ip := fmt.Sprintf("10.1.%d.%d", c, d)
pfx := bgp.NewIPAddrPrefix(32-x, ip)
tbl := []*table.Path{table.NewPath(
nil, pfx, false, attrs, time.Now(), false)}
if _, err := srv.AddPath("", tbl); err != nil {
b.Fatalf(`exp nil err from AddPath; got %v`, err)
}
pfx6 := bgp.NewIPv6AddrPrefix(120-(x*2), "::ffff:"+ip)
tbl = []*table.Path{table.NewPath(
nil, pfx6, false, attrs, time.Now(), false)}
if _, err := srv.AddPath("", tbl); err != nil {
b.Fatalf(`exp nil err from AddPath; got %v`, err)
}
}
}
}
b.Run(`IPv4`, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tbl, err := srv.GetRib(``, bgp.RF_IPv4_UC, nil)
if err != nil {
b.Fatalf(`exp nil err from GetRib; got %v`, err)
}
if exp, got := expEach, len(tbl.GetDestinations()); exp != got {
b.Fatalf(`exp %v dests; got %v`, exp, got)
}
if tbl == nil {
b.Fatal(`exp non-nil table`)
}
}
})
b.Run(`IPv6`, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tbl, err := srv.GetRib(``, bgp.RF_IPv6_UC, nil)
if err != nil {
b.Fatalf(`exp nil err from GetRib; got %v`, err)
}
if exp, got := expEach, len(tbl.GetDestinations()); exp != got {
b.Fatalf(`exp %v dests; got %v`, exp, got)
}
if tbl == nil {
b.Fatal(`exp non-nil table`)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment