Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add benchmark of workload #316

Merged
merged 5 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/cache/v2/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
cluster_v2 "kmesh.net/kmesh/api/v2/cluster"
core_v2 "kmesh.net/kmesh/api/v2/core"
"kmesh.net/kmesh/api/v2/endpoint"
"kmesh.net/kmesh/daemon/options"
maps_v2 "kmesh.net/kmesh/pkg/cache/v2/maps"
"kmesh.net/kmesh/pkg/nets"
"kmesh.net/kmesh/pkg/utils/hash"
Expand Down Expand Up @@ -178,7 +179,12 @@ func TestClusterFlush(t *testing.T) {

func BenchmarkClusterFlush(b *testing.B) {
t := &testing.T{}
cleanup := test.InitBpfMap(t)
config := options.BpfConfig{
Mode: "ads",
BpfFsPath: "/sys/fs/bpf",
Cgroup2Path: "/mnt/kmesh_cgroup2",
}
cleanup, _ := test.InitBpfMap(t, config)
b.Cleanup(cleanup)

cluster := cluster_v2.Cluster{
Expand Down
8 changes: 7 additions & 1 deletion pkg/cache/v2/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
core_v2 "kmesh.net/kmesh/api/v2/core"
"kmesh.net/kmesh/api/v2/filter"
listener_v2 "kmesh.net/kmesh/api/v2/listener"
"kmesh.net/kmesh/daemon/options"
maps_v2 "kmesh.net/kmesh/pkg/cache/v2/maps"
"kmesh.net/kmesh/pkg/nets"
"kmesh.net/kmesh/pkg/utils/hash"
Expand Down Expand Up @@ -187,7 +188,12 @@ func TestListenerFlush(t *testing.T) {

func BenchmarkFlush(b *testing.B) {
t := &testing.T{}
cleanup := test.InitBpfMap(t)
config := options.BpfConfig{
Mode: "ads",
BpfFsPath: "/sys/fs/bpf",
Cgroup2Path: "/mnt/kmesh_cgroup2",
}
cleanup, _ := test.InitBpfMap(t, config)
b.Cleanup(cleanup)

listener := &listener_v2.Listener{
Expand Down
29 changes: 25 additions & 4 deletions pkg/controller/ads/ads_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ import (

cluster_v2 "kmesh.net/kmesh/api/v2/cluster"
core_v2 "kmesh.net/kmesh/api/v2/core"
"kmesh.net/kmesh/daemon/options"
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) {
cleanup := test.InitBpfMap(t)
config := options.BpfConfig{
Mode: "ads",
BpfFsPath: "/sys/fs/bpf",
Cgroup2Path: "/mnt/kmesh_cgroup2",
}
cleanup, _ := test.InitBpfMap(t, config)
t.Cleanup(cleanup)
t.Run("new cluster, cluster type is eds", func(t *testing.T) {
p := newProcessor()
Expand Down Expand Up @@ -246,7 +252,12 @@ func TestHandleCdsResponse(t *testing.T) {
}

func TestHandleEdsResponse(t *testing.T) {
cleanup := test.InitBpfMap(t)
config := options.BpfConfig{
Mode: "ads",
BpfFsPath: "/sys/fs/bpf",
Cgroup2Path: "/mnt/kmesh_cgroup2",
}
cleanup, _ := test.InitBpfMap(t, config)
t.Cleanup(cleanup)
t.Run("cluster's apiStatus is UPDATE", func(t *testing.T) {
p := newProcessor()
Expand Down Expand Up @@ -402,7 +413,12 @@ func TestHandleEdsResponse(t *testing.T) {
}

func TestHandleLdsResponse(t *testing.T) {
cleanup := test.InitBpfMap(t)
config := options.BpfConfig{
Mode: "ads",
BpfFsPath: "/sys/fs/bpf",
Cgroup2Path: "/mnt/kmesh_cgroup2",
}
cleanup, _ := test.InitBpfMap(t, config)
t.Cleanup(cleanup)
t.Run("normal function test", func(t *testing.T) {
adsLoader := NewAdsCache()
Expand Down Expand Up @@ -574,7 +590,12 @@ func TestHandleLdsResponse(t *testing.T) {
}

func TestHandleRdsResponse(t *testing.T) {
cleanup := test.InitBpfMap(t)
config := options.BpfConfig{
Mode: "ads",
BpfFsPath: "/sys/fs/bpf",
Cgroup2Path: "/mnt/kmesh_cgroup2",
}
cleanup, _ := test.InitBpfMap(t, config)
t.Cleanup(cleanup)
t.Run("normal function test", func(t *testing.T) {
p := newProcessor()
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"testing"

"github.com/agiledragon/gomonkey/v2"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"gotest.tools/assert"

"kmesh.net/kmesh/pkg/bpf"
"kmesh.net/kmesh/pkg/constants"
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestClientResponseProcess(t *testing.T) {

utClient := NewXdsClient(constants.AdsMode, &bpf.BpfKmeshWorkload{})
err := utClient.createGrpcStreamClient()
assert.NilError(t, err)
assert.NoError(t, err)

reConnectPatches := gomonkey.NewPatches()
defer reConnectPatches.Reset()
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestClientResponseProcess(t *testing.T) {

utClient := NewXdsClient(constants.WorkloadMode, &bpf.BpfKmeshWorkload{})
err := utClient.createGrpcStreamClient()
assert.NilError(t, err)
assert.NoError(t, err)

reConnectPatches := gomonkey.NewPatches()
defer reConnectPatches.Reset()
Expand Down
86 changes: 86 additions & 0 deletions pkg/controller/workload/workload_processor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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 workload

import (
"net/netip"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/rand"

"kmesh.net/kmesh/api/v2/workloadapi"
"kmesh.net/kmesh/daemon/options"
"kmesh.net/kmesh/pkg/constants"
"kmesh.net/kmesh/pkg/utils/test"
)

func BenchmarkHandleDataWithService(b *testing.B) {
t := &testing.T{}
config := options.BpfConfig{
Mode: constants.WorkloadMode,
BpfFsPath: "/sys/fs/bpf",
Cgroup2Path: "/mnt/kmesh_cgroup2",
EnableMda: false,
}
cleanup, bpfLoader := test.InitBpfMap(t, config)
b.Cleanup(cleanup)

workloadController := NewController(bpfLoader.GetBpfKmeshWorkload().SockConn.KmeshCgroupSockWorkloadMaps)

b.ResetTimer()
for i := 0; i < b.N; i++ {
workload := createTestWorkload()
err := workloadController.Processor.handleDataWithService(workload)
assert.NoError(t, err)
}
}

func createTestWorkload() *workloadapi.Workload {
workload := workloadapi.Workload{
Namespace: "ns",
Name: "name",
Addresses: [][]byte{netip.AddrFrom4([4]byte{1, 2, 3, 4}).AsSlice()},
Network: "testnetwork",
CanonicalName: "foo",
CanonicalRevision: "latest",
WorkloadType: workloadapi.WorkloadType_POD,
WorkloadName: "name",
Status: workloadapi.WorkloadStatus_HEALTHY,
ClusterId: "cluster0",
Services: map[string]*workloadapi.PortList{
"ns/hostname": {
Ports: []*workloadapi.Port{
{
ServicePort: 80,
TargetPort: 8080,
},
{
ServicePort: 81,
TargetPort: 8180,
},
{
ServicePort: 82,
TargetPort: 82,
},
},
},
},
}
workload.Uid = "cluster0/" + rand.String(6)
return &workload
}
20 changes: 12 additions & 8 deletions pkg/utils/test/bpf_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import (

"kmesh.net/kmesh/daemon/options"
"kmesh.net/kmesh/pkg/bpf"
"kmesh.net/kmesh/pkg/constants"
)

type CleanupFn func()

func InitBpfMap(t *testing.T) CleanupFn {
func InitBpfMap(t *testing.T, config options.BpfConfig) (CleanupFn, *bpf.BpfLoader) {
err := os.MkdirAll("/mnt/kmesh_cgroup2", 0755)
if err != nil {
t.Fatalf("Failed to create dir /mnt/kmesh_cgroup2: %v", err)
Expand All @@ -43,21 +44,24 @@ func InitBpfMap(t *testing.T) CleanupFn {
CleanupBpfMap()
t.Fatalf("Failed to mount /sys/fs/bpf: %v", err)
}
config := options.BpfConfig{
Mode: "ads",
BpfFsPath: "/sys/fs/bpf",
Cgroup2Path: "/mnt/kmesh_cgroup2",
}

loader := bpf.NewBpfLoader(&config)
err = loader.StartAdsMode()

if config.Mode == constants.AdsMode {
err = loader.StartAdsMode()
}
if config.Mode == constants.WorkloadMode {
err = loader.StartWorkloadMode()
}

if err != nil {
CleanupBpfMap()
t.Fatalf("bpf init failed: %v", err)
}
return func() {
loader.Stop()
CleanupBpfMap()
}
}, loader
}

func CleanupBpfMap() {
Expand Down
Loading