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