Optimize indexer

This commit is contained in:
wojtekt 2021-09-24 14:46:56 +02:00
parent 6c45f6e32b
commit 27c94a49c8

View File

@ -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])
}
}