Skip to content

Commit

Permalink
feat: add benchmark and skipping features example
Browse files Browse the repository at this point in the history
  • Loading branch information
crandles committed Sep 16, 2024
1 parent 8050f0c commit 3b3d71e
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
42 changes: 42 additions & 0 deletions examples/benchmark_tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Benchmark tests and skipping features

This directory contains the example of:
- how to run **without test features**, and use your preferred `testing` package (standard library and testify included in example)
* When skipping features, you will not receive any benefit from the `features` related setup or teardown `envfuncs`, but you will still be able to access a kubernetes config (`envconfig`) to create a client that has access to the expected test cluster.
- how to run Go **Test Benchmarks**, e.g. via `go test -bench=.`

# Skipping features

```go
func BenchmarkListPods(b *testing.B) {
client, err := testenv.EnvConf().NewClient()
// ...your client is ready you use -- or use the `*rest.Config` to create your preferred client
}

func TestExample(t *testing.T) {
client, err := testenv.EnvConf().NewClient()
// ...
}
```


# Run Tests with flags

These test cases can be executed using the normal `go test -bench=-` command by passing the right arguments

```bash
go test -bench=. -v .
```

With the output generated as following.

```bash
goos: <YOUR_OS>
goarch: <YOUR_ARCH>
pkg: sigs.k8s.io/e2e-framework/examples/benchmark_tests
cpu: <YOUR_CPU_TYPE>
BenchmarkListPods
BenchmarkListPods-12 100 180148936 ns/op
PASS
ok sigs.k8s.io/e2e-framework/examples/benchmark_tests 47.880s
```
35 changes: 35 additions & 0 deletions examples/benchmark_tests/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2024 The Kubernetes 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 benchmark_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
)

func BenchmarkListPods(b *testing.B) {
client, err := testenv.EnvConf().NewClient()
require.NoError(b, err)
var pods corev1.PodList
for n := 0; n < b.N; n++ {
err = client.Resources("kube-system").List(context.TODO(), &pods)
require.NoError(b, err)
}
}
63 changes: 63 additions & 0 deletions examples/benchmark_tests/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2024 The Kubernetes 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 benchmark_test

import (
"context"
"os"
"testing"
"time"

"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/support/kind"
)

var testenv env.Environment

func TestMain(m *testing.M) {
testenv = env.New()
testenv.Setup(
// Step: creates kind cluster, propagate kind cluster object
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
name := envconf.RandomName("my-cluster", 16)
cluster := kind.NewCluster(name)
kubeconfig, err := cluster.Create(ctx)
if err != nil {
return ctx, err
}
// stall a bit to allow most pods to come up
time.Sleep(time.Second * 10)

// update environment with kubecofig file
cfg.WithKubeconfigFile(kubeconfig)
// propagate cluster value
return context.WithValue(ctx, 1, cluster), nil
},
).Finish(
// Teardown func: delete kind cluster
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
cluster := ctx.Value(1).(*kind.Cluster) // nil should be tested
if err := cluster.Destroy(ctx); err != nil {
return ctx, err
}
return ctx, nil
},
)

os.Exit(testenv.Run(m))
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
Expand Down

0 comments on commit 3b3d71e

Please sign in to comment.