generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #453 from crandles/benchmarks
feat: add benchmark and skipping features example
- Loading branch information
Showing
5 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 TestListPods(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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
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 TestListPods(t *testing.T) { | ||
client, err := testenv.EnvConf().NewClient() | ||
require.NoError(t, err) | ||
var pods corev1.PodList | ||
err = client.Resources("kube-system").List(context.TODO(), &pods) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters