diff --git a/staging/src/k8s.io/client-go/tools/cache/thread_safe_store_test.go b/staging/src/k8s.io/client-go/tools/cache/thread_safe_store_test.go index 267ebcbbac6..64296e18670 100644 --- a/staging/src/k8s.io/client-go/tools/cache/thread_safe_store_test.go +++ b/staging/src/k8s.io/client-go/tools/cache/thread_safe_store_test.go @@ -17,6 +17,7 @@ limitations under the License. package cache import ( + "fmt" "testing" ) @@ -90,3 +91,35 @@ func TestThreadSafeStoreAddKeepsNonEmptySetPostDeleteFromIndex(t *testing.T) { t.Errorf("Index backing string set has incorrect length, expect 1. Set length: %d", len(set)) } } + +// BenchmarkIndexer-12 447849 2738 ns/op 242 B/op 4 allocs/op +// PASS +// ok k8s.io/kubernetes/vendor/k8s.io/client-go/tools/cache 2.451s +func BenchmarkIndexer(b *testing.B) { + testIndexer := "testIndexer" + + indexers := Indexers{ + testIndexer: func(obj interface{}) (strings []string, e error) { + indexes := []string{obj.(string)} + return indexes, nil + }, + } + + indices := Indices{} + store := NewThreadSafeStore(indexers, indices).(*threadSafeMap) + + // The following benchmark imitates what is happening in indexes + // used in storage layer, where indexing is mostly static (e.g. + // indexing objects by their (namespace, name)). + // The 5000 number imitates indexing nodes in 5000-node cluster. + objectCount := 5000 + objects := make([]string, 0, 5000) + for i := 0; i < objectCount; i++ { + objects = append(objects, fmt.Sprintf("object-number-%d", i)) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + store.Update(objects[i%objectCount], objects[i%objectCount]) + } +}