1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/*
Copyright 2025 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 cache
import (
"fmt"
"testing"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
func TestListAll(t *testing.T) {
testCases := []struct {
name string
numObjects int
numMatching int
matchingLabels map[string]string
selector labels.Selector
expectedCount int
}{
{
name: "subset match",
numObjects: 100000,
numMatching: 10000,
matchingLabels: map[string]string{"match": "true"},
selector: labels.SelectorFromSet(map[string]string{"match": "true"}),
expectedCount: 10000,
},
{
name: "all match",
numObjects: 1000000,
numMatching: 0,
matchingLabels: map[string]string{},
selector: labels.Everything(),
expectedCount: 1000000,
},
{
name: "no match",
numObjects: 1000000,
numMatching: 0,
matchingLabels: map[string]string{"nomatch": "true"},
selector: labels.SelectorFromSet(map[string]string{"match": "true"}),
expectedCount: 0,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
store := mustCreateStore(tc.numObjects, tc.numMatching, tc.matchingLabels)
var matchingObjects int
appendFn := func(obj interface{}) {
matchingObjects++
}
err := ListAll(store, tc.selector, appendFn)
if err != nil {
t.Fatalf("ListAll returned an error: %v", err)
}
if matchingObjects != tc.expectedCount {
t.Errorf("ListAll returned %d objects, expected %d", matchingObjects, tc.expectedCount)
}
})
}
}
func mustCreateStore(numObjects int, numMatching int, labels map[string]string) Store {
if numMatching > numObjects {
panic("there can not be more matches than objects")
}
store := NewStore(func(obj interface{}) (string, error) {
meta, err := meta.Accessor(obj)
if err != nil {
return "", err
}
return meta.GetName(), nil
})
// add matching objects to the store
for i := 0; i < numObjects; i++ {
obj := &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("obj-%d", i),
Labels: map[string]string{},
},
}
if i < numMatching {
obj.Labels = labels
}
err := store.Add(obj)
if err != nil {
panic("unexpected error")
}
}
return store
}
func benchmarkLister(b *testing.B, numObjects int, numMatching int, label map[string]string) {
store := mustCreateStore(numObjects, numMatching, label)
selector := labels.SelectorFromSet(label)
b.ResetTimer()
for n := 0; n < b.N; n++ {
err := ListAll(store, selector, func(m interface{}) {
})
if err != nil {
b.Fatalf("ListAll returned an error: %v", err)
}
}
}
func BenchmarkLister_Match_1k_100(b *testing.B) {
benchmarkLister(b, 1000, 100, map[string]string{"match": "true"})
}
func BenchmarkLister_Match_10k_100(b *testing.B) {
benchmarkLister(b, 10000, 100, map[string]string{"match": "true"})
}
func BenchmarkLister_Match_100k_100(b *testing.B) {
benchmarkLister(b, 100000, 100, map[string]string{"match": "true"})
}
func BenchmarkLister_Match_1M_100(b *testing.B) {
benchmarkLister(b, 1000000, 100, map[string]string{"match": "true"})
}
|