| 1 | package gocache |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "math/rand" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func BenchmarkMap_Get(b *testing.B) { |
| 12 | m := make(map[string]any) |
| 13 | for n := 0; n < b.N; n++ { |
| 14 | _, _ = m[strconv.Itoa(n)] |
| 15 | } |
| 16 | b.ReportAllocs() |
| 17 | } |
| 18 | |
| 19 | func BenchmarkMap_Set(b *testing.B) { |
| 20 | values := map[string]string{ |
| 21 | "small": "a", |
| 22 | "medium": strings.Repeat("a", 1024), |
| 23 | "large": strings.Repeat("a", 1024*100), |
| 24 | } |
| 25 | for name, value := range values { |
| 26 | b.Run(fmt.Sprintf("%s value", name), func(b *testing.B) { |
| 27 | m := make(map[string]any) |
| 28 | for n := 0; n < b.N; n++ { |
| 29 | m[strconv.Itoa(n)] = value |
| 30 | } |
| 31 | b.ReportAllocs() |
| 32 | }) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func BenchmarkCache_Get(b *testing.B) { |
| 37 | evictionPolicies := []EvictionPolicy{FirstInFirstOut, LeastRecentlyUsed} |
| 38 | for _, evictionPolicy := range evictionPolicies { |
| 39 | cache := NewCache().WithMaxSize(NoMaxSize).WithMaxMemoryUsage(NoMaxMemoryUsage) |
| 40 | b.Run(string(evictionPolicy), func(b *testing.B) { |
| 41 | for n := 0; n < b.N; n++ { |
| 42 | cache.Get(strconv.Itoa(n)) |
| 43 | } |
| 44 | b.ReportAllocs() |
| 45 | }) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func BenchmarkCache_Set(b *testing.B) { |
| 50 | values := map[string]string{ |
| 51 | "small": "a", |
| 52 | "medium": strings.Repeat("a", 1024), |
| 53 | "large": strings.Repeat("a", 1024*100), |
| 54 | } |
| 55 | evictionPolicies := []EvictionPolicy{FirstInFirstOut, LeastRecentlyUsed} |
| 56 | for _, evictionPolicy := range evictionPolicies { |
| 57 | for name, value := range values { |
| 58 | b.Run(fmt.Sprintf("%s %s value", evictionPolicy, name), func(b *testing.B) { |
| 59 | cache := NewCache().WithMaxSize(NoMaxSize).WithMaxMemoryUsage(NoMaxMemoryUsage).WithEvictionPolicy(evictionPolicy) |
| 60 | for n := 0; n < b.N; n++ { |
| 61 | cache.Set(strconv.Itoa(n), value) |
| 62 | } |
| 63 | b.ReportAllocs() |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // BenchmarkCache_SetUsingMaxMemoryUsage does NOT test evictions, it tests the overhead of the extra work |
| 70 | // automatically performed when using MaxMemoryUsage |
| 71 | func BenchmarkCache_SetUsingMaxMemoryUsage(b *testing.B) { |
| 72 | values := map[string]string{ |
| 73 | "small": "a", |
| 74 | "medium": strings.Repeat("a", 1024), |
| 75 | "large": strings.Repeat("a", 1024*100), |
| 76 | } |
| 77 | for name, value := range values { |
| 78 | b.Run(fmt.Sprintf("%s value", name), func(b *testing.B) { |
| 79 | cache := NewCache().WithMaxSize(NoMaxSize).WithMaxMemoryUsage(999 * Gigabyte) |
| 80 | for n := 0; n < b.N; n++ { |
| 81 | cache.Set(strconv.Itoa(n), value) |
| 82 | } |
| 83 | b.ReportAllocs() |
| 84 | }) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func BenchmarkCache_SetWithMaxSize(b *testing.B) { |
| 89 | values := map[string]string{ |
| 90 | "small": "a", |
| 91 | "medium": strings.Repeat("a", 1024), |
| 92 | "large": strings.Repeat("a", 1024*100), |
| 93 | } |
| 94 | maxSizes := []int{100, 10000, 100000} |
| 95 | for name, value := range values { |
| 96 | for _, maxSize := range maxSizes { |
| 97 | b.Run(fmt.Sprintf("%d %s value", maxSize, name), func(b *testing.B) { |
| 98 | cache := NewCache().WithMaxSize(maxSize) |
| 99 | for n := 0; n < b.N; n++ { |
| 100 | cache.Set(strconv.Itoa(n), value) |
| 101 | } |
| 102 | b.ReportAllocs() |
| 103 | }) |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func BenchmarkCache_SetWithMaxSizeAndLRU(b *testing.B) { |
| 109 | values := map[string]string{ |
| 110 | "small": "a", |
| 111 | "medium": strings.Repeat("a", 1024), |
| 112 | "large": strings.Repeat("a", 1024*100), |
| 113 | } |
| 114 | maxSizes := []int{100, 10000, 100000} |
| 115 | for name, value := range values { |
| 116 | for _, maxSize := range maxSizes { |
| 117 | b.Run(fmt.Sprintf("%d %s value", maxSize, name), func(b *testing.B) { |
| 118 | cache := NewCache().WithMaxSize(maxSize).WithEvictionPolicy(LeastRecentlyUsed) |
| 119 | for n := 0; n < b.N; n++ { |
| 120 | cache.Set(strconv.Itoa(n), value) |
| 121 | } |
| 122 | b.ReportAllocs() |
| 123 | }) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func BenchmarkCache_GetSetMultipleConcurrent(b *testing.B) { |
| 129 | data := map[string]string{ |
| 130 | "k1": "v1", |
| 131 | "k2": "v2", |
| 132 | "k3": "v3", |
| 133 | "k4": "v4", |
| 134 | "k5": "v5", |
| 135 | "k6": "v6", |
| 136 | "k7": "v7", |
| 137 | "k8": "v8", |
| 138 | } |
| 139 | cache := NewCache().WithMaxSize(NoMaxSize) |
| 140 | |
| 141 | b.RunParallel(func(pb *testing.PB) { |
| 142 | for pb.Next() { |
| 143 | for k, v := range data { |
| 144 | cache.Set(k, v) |
| 145 | cache.Get(k) |
| 146 | } |
| 147 | } |
| 148 | }) |
| 149 | b.ReportAllocs() |
| 150 | } |
| 151 | |
| 152 | func BenchmarkCache_GetSetConcurrentWithFrequentEviction(b *testing.B) { |
| 153 | value := strings.Repeat("a", 256) |
| 154 | evictionPolicies := []EvictionPolicy{FirstInFirstOut, LeastRecentlyUsed} |
| 155 | for _, evictionPolicy := range evictionPolicies { |
| 156 | b.Run(string(evictionPolicy), func(b *testing.B) { |
| 157 | cache := NewCache().WithEvictionPolicy(LeastRecentlyUsed).WithMaxSize(3).WithMaxMemoryUsage(NoMaxMemoryUsage) |
| 158 | b.RunParallel(func(pb *testing.PB) { |
| 159 | for pb.Next() { |
| 160 | k := strconv.Itoa(rand.Intn(15)) |
| 161 | cache.Set(k, value) |
| 162 | _, _ = cache.Get(k) |
| 163 | } |
| 164 | }) |
| 165 | b.ReportAllocs() |
| 166 | }) |
| 167 | |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func BenchmarkCache_GetConcurrently(b *testing.B) { |
| 172 | value := strings.Repeat("a", 256) |
| 173 | for _, evictionPolicy := range []EvictionPolicy{FirstInFirstOut, LeastRecentlyUsed} { |
| 174 | b.Run(string(evictionPolicy), func(b *testing.B) { |
| 175 | cache := NewCache().WithMaxSize(100000) |
| 176 | for i := 0; i < 100000; i++ { |
| 177 | cache.Set(strconv.Itoa(i), value) |
| 178 | } |
| 179 | b.RunParallel(func(pb *testing.PB) { |
| 180 | for pb.Next() { |
| 181 | key := strconv.Itoa(rand.Intn(100000)) |
| 182 | val, ok := cache.Get(key) |
| 183 | if !ok { |
| 184 | b.Errorf("key: %v; value: %v", key, val) |
| 185 | } |
| 186 | if val != value { |
| 187 | b.Errorf("expected: %v; got: %v", val, value) |
| 188 | } |
| 189 | } |
| 190 | }) |
| 191 | b.ReportAllocs() |
| 192 | }) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Note: The default value for Cache.forceNilInterfaceOnNilPointer is true |
| 197 | func BenchmarkCache_WithForceNilInterfaceOnNilPointer(b *testing.B) { |
| 198 | const ( |
| 199 | Min = 10000 |
| 200 | Max = 99999 |
| 201 | ) |
| 202 | type Struct struct { |
| 203 | Value string |
| 204 | } |
| 205 | forceNilInterfaceOnNilPointerValues := []bool{true, false} |
| 206 | values := []*Struct{nil, {Value: "value"}} |
| 207 | for _, forceNilInterfaceOnNilPointer := range forceNilInterfaceOnNilPointerValues { |
| 208 | for _, value := range values { |
| 209 | name := fmt.Sprintf("%v", forceNilInterfaceOnNilPointer) |
| 210 | if value == nil { |
| 211 | name += " with nil struct pointer" |
| 212 | } |
| 213 | b.Run(name, func(b *testing.B) { |
| 214 | cache := NewCache().WithMaxSize(NoMaxSize).WithMaxMemoryUsage(NoMaxMemoryUsage).WithForceNilInterfaceOnNilPointer(forceNilInterfaceOnNilPointer) |
| 215 | for n := 0; n < b.N; n++ { |
| 216 | cache.Set(strconv.Itoa(rand.Intn(Max-Min)+Min), value) |
| 217 | } |
| 218 | b.ReportAllocs() |
| 219 | }) |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | func BenchmarkCache_WithForceNilInterfaceOnNilPointerWithConcurrency(b *testing.B) { |
| 225 | const ( |
| 226 | Min = 10000 |
| 227 | Max = 99999 |
| 228 | ) |
| 229 | type Struct struct { |
| 230 | Value string |
| 231 | } |
| 232 | forceNilInterfaceOnNilPointerValues := []bool{true, false} |
| 233 | values := []*Struct{nil, {Value: "value"}} |
| 234 | for _, forceNilInterfaceOnNilPointer := range forceNilInterfaceOnNilPointerValues { |
| 235 | for _, value := range values { |
| 236 | name := fmt.Sprintf("%v", forceNilInterfaceOnNilPointer) |
| 237 | if value == nil { |
| 238 | name += " with nil struct pointer" |
| 239 | } |
| 240 | b.Run(name, func(b *testing.B) { |
| 241 | cache := NewCache().WithMaxSize(NoMaxSize).WithMaxMemoryUsage(NoMaxMemoryUsage).WithForceNilInterfaceOnNilPointer(forceNilInterfaceOnNilPointer) |
| 242 | b.RunParallel(func(pb *testing.PB) { |
| 243 | for pb.Next() { |
| 244 | cache.Set(strconv.Itoa(rand.Intn(Max-Min)+Min), value) |
| 245 | } |
| 246 | }) |
| 247 | b.ReportAllocs() |
| 248 | }) |
| 249 | } |
| 250 | } |
| 251 | } |