From 641f2df51aa89a0f7b4a0cdcb85dfa4209bca25e Mon Sep 17 00:00:00 2001 From: Shaun Crampton Date: Mon, 29 Jun 2020 11:17:17 +0100 Subject: [PATCH] Add benchmarks for named port index. --- labelindex/named_port_bench_test.go | 102 ++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 labelindex/named_port_bench_test.go diff --git a/labelindex/named_port_bench_test.go b/labelindex/named_port_bench_test.go new file mode 100644 index 0000000000..67ab451b07 --- /dev/null +++ b/labelindex/named_port_bench_test.go @@ -0,0 +1,102 @@ +// Copyright (c) 2020 Tigera, Inc. All rights reserved. + +// 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 labelindex_test + +import ( + "fmt" + "net" + "runtime" + "testing" + + "github.com/sirupsen/logrus" + + . "github.com/projectcalico/felix/labelindex" + + "github.com/projectcalico/libcalico-go/lib/backend/api" + "github.com/projectcalico/libcalico-go/lib/backend/model" + calinet "github.com/projectcalico/libcalico-go/lib/net" + "github.com/projectcalico/libcalico-go/lib/selector" +) + +func BenchmarkWorkloadUpdate10Sels(b *testing.B) { + benchmarkWorkloadUpdates(b, 10) +} +func BenchmarkWorkloadUpdate100Sels(b *testing.B) { + benchmarkWorkloadUpdates(b, 100) +} +func BenchmarkWorkloadUpdate1000Sels(b *testing.B) { + benchmarkWorkloadUpdates(b, 1000) +} +func BenchmarkWorkloadUpdate10000Sels(b *testing.B) { + benchmarkWorkloadUpdates(b, 10000) +} + +func benchmarkWorkloadUpdates(b *testing.B, numSels int) { + var lastID string + var lastMember IPSetMember + + logLevel := logrus.GetLevel() + logrus.SetLevel(logrus.InfoLevel) + defer logrus.SetLevel(logLevel) + + idx := NewSelectorAndNamedPortIndex() + idx.OnMemberAdded = func(ipSetID string, member IPSetMember) { + lastID = ipSetID + lastMember = member + } + idx.OnMemberRemoved = func(ipSetID string, member IPSetMember) { + lastID = ipSetID + lastMember = member + } + for i := 0; i < numSels; i++ { + sel, err := selector.Parse(fmt.Sprintf(`alpha == "beta" || has(ipset-%d)`, i)) + if err != nil { + b.Fatal(err) + } + idx.UpdateIPSet(fmt.Sprintf("ipset-%d", i), sel, ProtocolNone, "") + } + + updates := make([]api.Update, b.N) + for n := 0; n < b.N; n++ { + key := model.WorkloadEndpointKey{ + Hostname: "host", + OrchestratorID: "k8s", + WorkloadID: fmt.Sprintf("wep-%d", n), + EndpointID: "eth0", + } + ipNet := calinet.IPNet{IPNet: net.IPNet{ + IP: net.IPv4(10, 0, byte(n>>8), byte(n&0xff)), + Mask: net.CIDRMask(32, 32), + }} + updates[n] = api.Update{ + KVPair: model.KVPair{ + Key: key, + Value: &model.WorkloadEndpoint{ + Labels: map[string]string{"alpha": "beta"}, + IPv4Nets: []calinet.IPNet{ipNet}, + ProfileIDs: []string{"default"}, + }, + }, + } + } + + b.ResetTimer() + for n := 0; n < b.N; n++ { + idx.OnUpdate(updates[n]) + } + + runtime.KeepAlive(lastID) + runtime.KeepAlive(lastMember) +}