k8s.io/apiserver@v0.31.1/pkg/authentication/token/cache/cache_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cache
    18  
    19  import (
    20  	"fmt"
    21  	"math/rand"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/google/uuid"
    26  
    27  	"k8s.io/apiserver/pkg/authentication/authenticator"
    28  	"k8s.io/apiserver/pkg/authentication/user"
    29  	"k8s.io/utils/clock"
    30  )
    31  
    32  func TestSimpleCache(t *testing.T) {
    33  	testCache(newSimpleCache(clock.RealClock{}), t)
    34  }
    35  
    36  // Note: the performance profile of this benchmark may not match that in the production.
    37  // When making change to SimpleCache, run test with and without concurrency to better understand the impact.
    38  // This is a tool to test and measure high concurrency of the cache in isolation and not to the Kubernetes usage of the Cache.
    39  func BenchmarkCacheContentions(b *testing.B) {
    40  	for _, numKeys := range []int{1 << 8, 1 << 12, 1 << 16} {
    41  		b.Run(fmt.Sprintf("Simple/keys=%d", numKeys), func(b *testing.B) {
    42  			benchmarkCache(newSimpleCache(clock.RealClock{}), b, numKeys)
    43  		})
    44  		b.Run(fmt.Sprintf("Striped/keys=%d", numKeys), func(b *testing.B) {
    45  			benchmarkCache(newStripedCache(32, fnvHashFunc, func() cache { return newSimpleCache(clock.RealClock{}) }), b, numKeys)
    46  		})
    47  	}
    48  }
    49  
    50  func TestStripedCache(t *testing.T) {
    51  	testCache(newStripedCache(32, fnvHashFunc, func() cache { return newSimpleCache(clock.RealClock{}) }), t)
    52  }
    53  
    54  func benchmarkCache(cache cache, b *testing.B, numKeys int) {
    55  	keys := []string{}
    56  	for i := 0; i < numKeys; i++ {
    57  		key := uuid.New().String()
    58  		keys = append(keys, key)
    59  	}
    60  
    61  	b.ResetTimer()
    62  
    63  	b.SetParallelism(500)
    64  	b.RunParallel(func(pb *testing.PB) {
    65  		for pb.Next() {
    66  			key := keys[rand.Intn(numKeys)]
    67  			_, ok := cache.get(key)
    68  			if ok {
    69  				cache.remove(key)
    70  			} else {
    71  				cache.set(key, &cacheRecord{}, time.Second)
    72  			}
    73  		}
    74  	})
    75  }
    76  
    77  func testCache(cache cache, t *testing.T) {
    78  	if result, ok := cache.get("foo"); ok || result != nil {
    79  		t.Errorf("Expected null, false, got %#v, %v", result, ok)
    80  	}
    81  
    82  	record1 := &cacheRecord{resp: &authenticator.Response{User: &user.DefaultInfo{Name: "bob"}}}
    83  	record2 := &cacheRecord{resp: &authenticator.Response{User: &user.DefaultInfo{Name: "alice"}}}
    84  
    85  	// when empty, record is stored
    86  	cache.set("foo", record1, time.Hour)
    87  	if result, ok := cache.get("foo"); !ok || result != record1 {
    88  		t.Errorf("Expected %#v, true, got %#v, %v", record1, result, ok)
    89  	}
    90  
    91  	// newer record overrides
    92  	cache.set("foo", record2, time.Hour)
    93  	if result, ok := cache.get("foo"); !ok || result != record2 {
    94  		t.Errorf("Expected %#v, true, got %#v, %v", record2, result, ok)
    95  	}
    96  
    97  	// removing the current value removes
    98  	cache.remove("foo")
    99  	if result, ok := cache.get("foo"); ok || result != nil {
   100  		t.Errorf("Expected null, false, got %#v, %v", result, ok)
   101  	}
   102  }