Skip to content

Commit 30de989

Browse files
authored
Merge pull request kubernetes#129542 from serathius/watchcache-benchmark-namespace
Add benchmarking of namespace index
2 parents a32f695 + 13a21d5 commit 30de989

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func TestListResourceVersionMatch(t *testing.T) {
257257
}
258258

259259
func TestNamespaceScopedList(t *testing.T) {
260-
ctx, cacher, terminate := testSetup(t, withSpecNodeNameIndexerFuncs)
260+
ctx, cacher, terminate := testSetup(t, withNodeNameAndNamespaceIndex)
261261
t.Cleanup(terminate)
262262
storagetesting.RunTestNamespaceScopedList(ctx, t, cacher)
263263
}
@@ -355,13 +355,13 @@ func TestWatchInitializationSignal(t *testing.T) {
355355
}
356356

357357
func TestClusterScopedWatch(t *testing.T) {
358-
ctx, cacher, terminate := testSetup(t, withClusterScopedKeyFunc, withSpecNodeNameIndexerFuncs)
358+
ctx, cacher, terminate := testSetup(t, withClusterScopedKeyFunc, withNodeNameAndNamespaceIndex)
359359
t.Cleanup(terminate)
360360
storagetesting.RunTestClusterScopedWatch(ctx, t, cacher)
361361
}
362362

363363
func TestNamespaceScopedWatch(t *testing.T) {
364-
ctx, cacher, terminate := testSetup(t, withSpecNodeNameIndexerFuncs)
364+
ctx, cacher, terminate := testSetup(t, withNodeNameAndNamespaceIndex)
365365
t.Cleanup(terminate)
366366
storagetesting.RunTestNamespaceScopedWatch(ctx, t, cacher)
367367
}
@@ -432,7 +432,8 @@ func withClusterScopedKeyFunc(options *setupOptions) {
432432
}
433433
}
434434

435-
func withSpecNodeNameIndexerFuncs(options *setupOptions) {
435+
// mirror indexer configuration from pkg/registry/core/pod/strategy.go
436+
func withNodeNameAndNamespaceIndex(options *setupOptions) {
436437
options.indexerFuncs = map[string]storage.IndexerFunc{
437438
"spec.nodeName": func(obj runtime.Object) string {
438439
pod, ok := obj.(*example.Pod)
@@ -447,6 +448,10 @@ func withSpecNodeNameIndexerFuncs(options *setupOptions) {
447448
pod := obj.(*example.Pod)
448449
return []string{pod.Spec.NodeName}, nil
449450
},
451+
"f:metadata.namespace": func(obj interface{}) ([]string, error) {
452+
pod := obj.(*example.Pod)
453+
return []string{pod.ObjectMeta.Namespace}, nil
454+
},
450455
}
451456
}
452457

@@ -571,7 +576,7 @@ func BenchmarkStoreCreateList(b *testing.B) {
571576
b.Run(fmt.Sprintf("Indexed=%v", useIndex), func(b *testing.B) {
572577
opts := []setupOption{}
573578
if useIndex {
574-
opts = append(opts, withSpecNodeNameIndexerFuncs)
579+
opts = append(opts, withNodeNameAndNamespaceIndex)
575580
}
576581
ctx, cacher, _, terminate := testSetupWithEtcdServer(b, opts...)
577582
b.Cleanup(terminate)
@@ -627,7 +632,7 @@ func BenchmarkStoreList(b *testing.B) {
627632
for _, store := range storeOptions {
628633
b.Run(fmt.Sprintf("Store=%s", store.name), func(b *testing.B) {
629634
featuregatetesting.SetFeatureGateDuringTest(b, utilfeature.DefaultFeatureGate, features.BtreeWatchCache, store.btreeEnabled)
630-
ctx, cacher, _, terminate := testSetupWithEtcdServer(b, withSpecNodeNameIndexerFuncs)
635+
ctx, cacher, _, terminate := testSetupWithEtcdServer(b, withNodeNameAndNamespaceIndex)
631636
b.Cleanup(terminate)
632637
var out example.Pod
633638
for _, pod := range data.Pods {

staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2945,7 +2945,7 @@ func forceRequestWatchProgressSupport(t *testing.T) {
29452945
}
29462946

29472947
func TestListIndexer(t *testing.T) {
2948-
ctx, cacher, terminate := testSetup(t, withSpecNodeNameIndexerFuncs)
2948+
ctx, cacher, terminate := testSetup(t, withNodeNameAndNamespaceIndex)
29492949
t.Cleanup(terminate)
29502950
tests := []struct {
29512951
name string

staging/src/k8s.io/apiserver/pkg/storage/testing/store_benchmarks.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/apimachinery/pkg/runtime"
3030
"k8s.io/apimachinery/pkg/util/rand"
3131
"k8s.io/apiserver/pkg/apis/example"
32+
"k8s.io/apiserver/pkg/endpoints/request"
3233
"k8s.io/apiserver/pkg/storage"
3334
)
3435

@@ -149,6 +150,11 @@ func runBenchmarkStoreList(ctx context.Context, b *testing.B, store storage.Inte
149150
objectCount.Add(uint64(objects))
150151
pageCount.Add(uint64(pages))
151152
case namespace:
153+
ctx := ctx
154+
if useIndex {
155+
opts.Predicate.IndexFields = []string{"metadata.namespace"}
156+
ctx = request.WithRequestInfo(ctx, &request.RequestInfo{Namespace: namespaceName})
157+
}
152158
objects, pages := paginateList(ctx, store, "/pods/"+namespaceName, opts)
153159
objectCount.Add(uint64(objects))
154160
pageCount.Add(uint64(pages))

0 commit comments

Comments
 (0)