Skip to content

Commit

Permalink
Merge pull request #253 from LiZhenCheng9527/benchmark-linstener
Browse files Browse the repository at this point in the history
Added benchmark test for listener.Flush()
  • Loading branch information
kmesh-bot authored Apr 23, 2024
2 parents f1a31d1 + d8070b0 commit ba9dcf8
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 53 deletions.
111 changes: 111 additions & 0 deletions pkg/cache/v2/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
62 changes: 9 additions & 53 deletions pkg/controller/envoy/ads_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
package envoy

import (
"fmt"
"os"
"syscall"
"testing"

config_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
Expand All @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
67 changes: 67 additions & 0 deletions pkg/utils/test/bpf_map.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit ba9dcf8

Please sign in to comment.