Skip to content

Commit 8fe9b5e

Browse files
authored
Add benchmark for xds cache (istio#39443)
* Add benchmark for xds cache * revert mistake * fmt
1 parent 99906ab commit 8fe9b5e

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ etc/
5555
var/
5656
# Go compiled tests
5757
*.test
58+
# Profiles
59+
*.prof
5860
# MacOS extended attributes
5961
._*
6062
# MacOS Desktop Services Store

pilot/pkg/xds/bench_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"os"
2121
"path"
22+
"strconv"
2223
"strings"
2324
"testing"
2425
"text/template"
@@ -34,10 +35,12 @@ import (
3435
"istio.io/istio/pilot/pkg/config/kube/crd"
3536
"istio.io/istio/pilot/pkg/features"
3637
"istio.io/istio/pilot/pkg/model"
38+
"istio.io/istio/pilot/pkg/networking/core/v1alpha3/route"
3739
"istio.io/istio/pilot/pkg/networking/util"
3840
v3 "istio.io/istio/pilot/pkg/xds/v3"
3941
"istio.io/istio/pilot/test/xdstest"
4042
"istio.io/istio/pkg/config"
43+
"istio.io/istio/pkg/config/host"
4144
"istio.io/istio/pkg/config/mesh"
4245
"istio.io/istio/pkg/config/schema/collections"
4346
"istio.io/istio/pkg/config/schema/gvk"
@@ -554,3 +557,57 @@ func BenchmarkPushRequest(b *testing.B) {
554557
}
555558
}
556559
}
560+
561+
func makeCacheKey(n int) model.XdsCacheEntry {
562+
ns := strconv.Itoa(n)
563+
key := &route.Cache{
564+
RouteName: "something",
565+
ClusterID: "my-cluster",
566+
DNSDomain: "some.domain.example.com",
567+
DNSCapture: true,
568+
DNSAutoAllocate: false,
569+
ListenerPort: 1234,
570+
Services: []*model.Service{
571+
{Hostname: host.Name(ns + "some1.example.com"), Attributes: model.ServiceAttributes{Namespace: "test1"}},
572+
{Hostname: host.Name(ns + "some2.example.com"), Attributes: model.ServiceAttributes{Namespace: "test2"}},
573+
},
574+
DestinationRules: []*config.Config{
575+
{Meta: config.Meta{Name: ns + "a", Namespace: "b"}},
576+
{Meta: config.Meta{Name: ns + "d", Namespace: "e"}},
577+
},
578+
EnvoyFilterKeys: []string{ns + "1/a", ns + "2/b", ns + "3/c"},
579+
}
580+
return key
581+
}
582+
583+
func BenchmarkCache(b *testing.B) {
584+
// Ensure cache doesn't grow too large
585+
test.SetIntForTest(b, &features.XDSCacheMaxSize, 1_000)
586+
res := &discovery.Resource{Name: "test"}
587+
zeroTime := time.Time{}
588+
b.Run("key", func(b *testing.B) {
589+
key := makeCacheKey(1)
590+
for n := 0; n < b.N; n++ {
591+
_ = key.Key()
592+
}
593+
})
594+
b.Run("insert", func(b *testing.B) {
595+
c := model.NewXdsCache()
596+
597+
for n := 0; n < b.N; n++ {
598+
key := makeCacheKey(n)
599+
req := &model.PushRequest{Start: zeroTime.Add(time.Duration(n))}
600+
c.Add(key, req, res)
601+
}
602+
})
603+
b.Run("get", func(b *testing.B) {
604+
c := model.NewXdsCache()
605+
606+
key := makeCacheKey(1)
607+
req := &model.PushRequest{Start: zeroTime.Add(time.Duration(1))}
608+
c.Add(key, req, res)
609+
for n := 0; n < b.N; n++ {
610+
c.Get(key)
611+
}
612+
})
613+
}

pkg/test/util.go

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ func SetBoolForTest(t Failer, vv *bool, v bool) {
5252
})
5353
}
5454

55+
// SetIntForTest sets a variable for the duration of a test, then resets it once the test is complete.
56+
func SetIntForTest(t Failer, vv *int, v int) {
57+
old := *vv
58+
*vv = v
59+
t.Cleanup(func() {
60+
*vv = old
61+
})
62+
}
63+
5564
// SetFloatForTest sets a variable for the duration of a test, then resets it once the test is complete.
5665
func SetFloatForTest(t Failer, vv *float64, v float64) {
5766
old := *vv

0 commit comments

Comments
 (0)