diff --git a/pkg/cache/v2/listener_test.go b/pkg/cache/v2/listener_test.go index e22c8886d..30554ca6b 100644 --- a/pkg/cache/v2/listener_test.go +++ b/pkg/cache/v2/listener_test.go @@ -23,11 +23,15 @@ import ( "github.com/stretchr/testify/assert" "google.golang.org/protobuf/types/known/anypb" "istio.io/istio/pkg/slices" + "k8s.io/apimachinery/pkg/util/rand" core_v2 "kmesh.net/kmesh/api/v2/core" + "kmesh.net/kmesh/api/v2/filter" listener_v2 "kmesh.net/kmesh/api/v2/listener" maps_v2 "kmesh.net/kmesh/pkg/cache/v2/maps" + "kmesh.net/kmesh/pkg/nets" "kmesh.net/kmesh/pkg/utils/hash" + "kmesh.net/kmesh/pkg/utils/test" ) func TestListenerFlush(t *testing.T) { @@ -180,3 +184,110 @@ func TestListenerFlush(t *testing.T) { assert.Equal(t, []*core_v2.SocketAddress{}, deleteListenerAddress) }) } + +func BenchmarkFlush(b *testing.B) { + t := &testing.T{} + test.InitBpfMap(t) + b.Cleanup(test.CleanupBpfMap) + + listener := &listener_v2.Listener{ + ApiStatus: core_v2.ApiStatus_UPDATE, + Address: &core_v2.SocketAddress{ + Port: uint32(80), + Ipv4: nets.ConvertIpToUint32("192.168.127.244"), + }, + FilterChains: []*listener_v2.FilterChain{ + { + Filters: []*listener_v2.Filter{ + { + Name: "filter1", + ConfigType: &listener_v2.Filter_TcpProxy{ + TcpProxy: &filter.TcpProxy{ + StatPrefix: "outbound|53||kube-dns.kube-system.svc.cluster.local", + ClusterSpecifier: &filter.TcpProxy_Cluster{ + Cluster: "outbound|53||kube-dns.kube-system.svc.cluster.local", + }, + MaxConnectAttempts: uint32(60), + }, + }, + }, + }, + }, + { + Filters: []*listener_v2.Filter{ + { + Name: "filter2", + ConfigType: &listener_v2.Filter_TcpProxy{ + TcpProxy: &filter.TcpProxy{ + StatPrefix: "outbound|80||kube-dns.kube-system.svc.cluster.local", + ClusterSpecifier: &filter.TcpProxy_Cluster{ + Cluster: "outbound|80||kube-dns.kube-system.svc.cluster.local", + }, + MaxConnectAttempts: uint32(60), + }, + }, + }, + }, + }, + { + Filters: []*listener_v2.Filter{ + { + Name: "filter3", + ConfigType: &listener_v2.Filter_TcpProxy{ + TcpProxy: &filter.TcpProxy{ + StatPrefix: "outbound|443||kube-dns.kube-system.svc.cluster.local", + ClusterSpecifier: &filter.TcpProxy_Cluster{ + Cluster: "outbound|443||kube-dns.kube-system.svc.cluster.local", + }, + MaxConnectAttempts: uint32(60), + }, + }, + }, + }, + }, + { + Filters: []*listener_v2.Filter{ + { + Name: "filter4", + ConfigType: &listener_v2.Filter_TcpProxy{ + TcpProxy: &filter.TcpProxy{ + StatPrefix: "outbound|9090||kube-dns.kube-system.svc.cluster.local", + ClusterSpecifier: &filter.TcpProxy_Cluster{ + Cluster: "outbound|9090||kube-dns.kube-system.svc.cluster.local", + }, + MaxConnectAttempts: uint32(60), + }, + }, + }, + }, + }, + { + Filters: []*listener_v2.Filter{ + { + Name: "filter5", + ConfigType: &listener_v2.Filter_TcpProxy{ + TcpProxy: &filter.TcpProxy{ + StatPrefix: "outbound|15001||kube-dns.kube-system.svc.cluster.local", + ClusterSpecifier: &filter.TcpProxy_Cluster{ + Cluster: "outbound|15001||kube-dns.kube-system.svc.cluster.local", + }, + MaxConnectAttempts: uint32(60), + }, + }, + }, + }, + }, + }, + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + cache := NewListenerCache() + listener.ApiStatus = core_v2.ApiStatus_UPDATE + listener.Name = rand.String(6) + cache.SetApiListener(listener.Name, listener) + + cache.Flush() + assert.Equal(t, listener.GetApiStatus(), core_v2.ApiStatus_NONE) + } +} diff --git a/pkg/controller/envoy/ads_event_test.go b/pkg/controller/envoy/ads_event_test.go index 62c80d851..f8510f329 100644 --- a/pkg/controller/envoy/ads_event_test.go +++ b/pkg/controller/envoy/ads_event_test.go @@ -17,9 +17,6 @@ package envoy import ( - "fmt" - "os" - "syscall" "testing" config_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3" @@ -35,14 +32,14 @@ import ( cluster_v2 "kmesh.net/kmesh/api/v2/cluster" core_v2 "kmesh.net/kmesh/api/v2/core" - "kmesh.net/kmesh/pkg/bpf" cache_v2 "kmesh.net/kmesh/pkg/cache/v2" "kmesh.net/kmesh/pkg/utils/hash" + "kmesh.net/kmesh/pkg/utils/test" ) func TestHandleCdsResponse(t *testing.T) { - initBpfMap(t) - t.Cleanup(cleanupBpfMap) + test.InitBpfMap(t) + t.Cleanup(test.CleanupBpfMap) t.Run("new cluster, cluster type is eds", func(t *testing.T) { svc := NewServiceEvent() svc.LastNonce.edsNonce = "utkmesh" @@ -249,8 +246,8 @@ func TestHandleCdsResponse(t *testing.T) { } func TestHandleEdsResponse(t *testing.T) { - initBpfMap(t) - t.Cleanup(cleanupBpfMap) + test.InitBpfMap(t) + t.Cleanup(test.CleanupBpfMap) t.Run("cluster's apiStatus is UPDATE", func(t *testing.T) { svc := NewServiceEvent() adsLoader := NewAdsLoader() @@ -404,50 +401,9 @@ func TestHandleEdsResponse(t *testing.T) { }) } -func initBpfMap(t *testing.T) { - err := os.MkdirAll("/mnt/kmesh_cgroup2", 0755) - if err != nil { - t.Fatalf("Failed to create dir /mnt/kmesh_cgroup2: %v", err) - } - err = syscall.Mount("none", "/mnt/kmesh_cgroup2/", "cgroup2", 0, "") - if err != nil { - cleanupBpfMap() - t.Fatalf("Failed to mount /mnt/kmesh_cgroup2/: %v", err) - } - err = syscall.Mount("/sys/fs/bpf", "/sys/fs/bpf", "bpf", 0, "") - if err != nil { - cleanupBpfMap() - t.Fatalf("Failed to mount /sys/fs/bpf: %v", err) - } - config := bpf.GetConfig() - config.BpfFsPath = "/sys/fs/bpf" - config.Cgroup2Path = "/mnt/kmesh_cgroup2" - err = bpf.StartKmesh() - if err != nil { - cleanupBpfMap() - t.Fatalf("bpf init failed: %v", err) - } -} - -func cleanupBpfMap() { - bpf.Stop() - err := syscall.Unmount("/mnt/kmesh_cgroup2", 0) - if err != nil { - fmt.Println("unmount /mnt/kmesh_cgroup2 error: ", err) - } - err = syscall.Unmount("/sys/fs/bpf", 0) - if err != nil { - fmt.Println("unmount /sys/fs/bpf error: ", err) - } - err = os.RemoveAll("/mnt/kmesh_cgroup2") - if err != nil { - fmt.Println("remove /mnt/kmesh_cgroup2 error: ", err) - } -} - func TestHandleLdsResponse(t *testing.T) { - initBpfMap(t) - t.Cleanup(cleanupBpfMap) + test.InitBpfMap(t) + t.Cleanup(test.CleanupBpfMap) t.Run("normal function test", func(t *testing.T) { adsLoader := NewAdsLoader() adsLoader.routeNames = []string{ @@ -618,8 +574,8 @@ func TestHandleLdsResponse(t *testing.T) { } func TestHandleRdsResponse(t *testing.T) { - initBpfMap(t) - t.Cleanup(cleanupBpfMap) + test.InitBpfMap(t) + t.Cleanup(test.CleanupBpfMap) t.Run("normal function test", func(t *testing.T) { svc := NewServiceEvent() svc.ack = &service_discovery_v3.DiscoveryRequest{ diff --git a/pkg/utils/test/bpf_map.go b/pkg/utils/test/bpf_map.go new file mode 100644 index 000000000..c6cb2a613 --- /dev/null +++ b/pkg/utils/test/bpf_map.go @@ -0,0 +1,67 @@ +/* + * Copyright 2024 The Kmesh Authors. + * + * 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 test + +import ( + "fmt" + "os" + "syscall" + "testing" + + "kmesh.net/kmesh/pkg/bpf" +) + +func InitBpfMap(t *testing.T) { + err := os.MkdirAll("/mnt/kmesh_cgroup2", 0755) + if err != nil { + t.Fatalf("Failed to create dir /mnt/kmesh_cgroup2: %v", err) + } + err = syscall.Mount("none", "/mnt/kmesh_cgroup2/", "cgroup2", 0, "") + if err != nil { + CleanupBpfMap() + t.Fatalf("Failed to mount /mnt/kmesh_cgroup2/: %v", err) + } + err = syscall.Mount("/sys/fs/bpf", "/sys/fs/bpf", "bpf", 0, "") + if err != nil { + CleanupBpfMap() + t.Fatalf("Failed to mount /sys/fs/bpf: %v", err) + } + config := bpf.GetConfig() + config.BpfFsPath = "/sys/fs/bpf" + config.Cgroup2Path = "/mnt/kmesh_cgroup2" + err = bpf.StartKmesh() + if err != nil { + CleanupBpfMap() + t.Fatalf("bpf init failed: %v", err) + } +} + +func CleanupBpfMap() { + bpf.Stop() + err := syscall.Unmount("/mnt/kmesh_cgroup2", 0) + if err != nil { + fmt.Println("unmount /mnt/kmesh_cgroup2 error: ", err) + } + err = syscall.Unmount("/sys/fs/bpf", 0) + if err != nil { + fmt.Println("unmount /sys/fs/bpf error: ", err) + } + err = os.RemoveAll("/mnt/kmesh_cgroup2") + if err != nil { + fmt.Println("remove /mnt/kmesh_cgroup2 error: ", err) + } +}