|
| 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 | + "math/rand" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "k8s.io/apimachinery/pkg/util/clock" |
| 25 | + "k8s.io/apiserver/pkg/authentication/user" |
| 26 | + |
| 27 | + "github.com/pborman/uuid" |
| 28 | +) |
| 29 | + |
| 30 | +func TestSimpleCache(t *testing.T) { |
| 31 | + testCache(newSimpleCache(4096, clock.RealClock{}), t) |
| 32 | +} |
| 33 | + |
| 34 | +func BenchmarkSimpleCache(b *testing.B) { |
| 35 | + benchmarkCache(newSimpleCache(4096, clock.RealClock{}), b) |
| 36 | +} |
| 37 | + |
| 38 | +func TestStripedCache(t *testing.T) { |
| 39 | + testCache(newStripedCache(32, fnvKeyFunc, func() cache { return newSimpleCache(128, clock.RealClock{}) }), t) |
| 40 | +} |
| 41 | + |
| 42 | +func BenchmarkStripedCache(b *testing.B) { |
| 43 | + benchmarkCache(newStripedCache(32, fnvKeyFunc, func() cache { return newSimpleCache(128, clock.RealClock{}) }), b) |
| 44 | +} |
| 45 | + |
| 46 | +func benchmarkCache(cache cache, b *testing.B) { |
| 47 | + keys := []string{} |
| 48 | + for i := 0; i < b.N; i++ { |
| 49 | + key := uuid.NewRandom().String() |
| 50 | + keys = append(keys, key) |
| 51 | + } |
| 52 | + |
| 53 | + b.ResetTimer() |
| 54 | + |
| 55 | + b.SetParallelism(500) |
| 56 | + b.RunParallel(func(pb *testing.PB) { |
| 57 | + for pb.Next() { |
| 58 | + key := keys[rand.Intn(b.N)] |
| 59 | + _, ok := cache.get(key) |
| 60 | + if ok { |
| 61 | + cache.remove(key) |
| 62 | + } else { |
| 63 | + cache.set(key, &cacheRecord{}, time.Second) |
| 64 | + } |
| 65 | + } |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +func testCache(cache cache, t *testing.T) { |
| 70 | + if result, ok := cache.get("foo"); ok || result != nil { |
| 71 | + t.Errorf("Expected null, false, got %#v, %v", result, ok) |
| 72 | + } |
| 73 | + |
| 74 | + record1 := &cacheRecord{user: &user.DefaultInfo{Name: "bob"}} |
| 75 | + record2 := &cacheRecord{user: &user.DefaultInfo{Name: "alice"}} |
| 76 | + |
| 77 | + // when empty, record is stored |
| 78 | + cache.set("foo", record1, time.Hour) |
| 79 | + if result, ok := cache.get("foo"); !ok || result != record1 { |
| 80 | + t.Errorf("Expected %#v, true, got %#v, %v", record1, ok) |
| 81 | + } |
| 82 | + |
| 83 | + // newer record overrides |
| 84 | + cache.set("foo", record2, time.Hour) |
| 85 | + if result, ok := cache.get("foo"); !ok || result != record2 { |
| 86 | + t.Errorf("Expected %#v, true, got %#v, %v", record2, ok) |
| 87 | + } |
| 88 | + |
| 89 | + // removing the current value removes |
| 90 | + cache.remove("foo") |
| 91 | + if result, ok := cache.get("foo"); ok || result != nil { |
| 92 | + t.Errorf("Expected null, false, got %#v, %v", result, ok) |
| 93 | + } |
| 94 | +} |
0 commit comments