|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/redis/go-redis/v9" |
| 10 | +) |
| 11 | + |
| 12 | +// --- Basic Cache Benchmarks --- |
| 13 | + |
| 14 | +func BenchmarkCache_Set(b *testing.B) { |
| 15 | + c := New[int](DefaultExpiration, 0) |
| 16 | + b.ResetTimer() |
| 17 | + for i := 0; i < b.N; i++ { |
| 18 | + c.Set("key", i, DefaultExpiration) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func BenchmarkCache_Get(b *testing.B) { |
| 23 | + c := New[int](DefaultExpiration, 0) |
| 24 | + c.Set("key", 1, DefaultExpiration) |
| 25 | + b.ResetTimer() |
| 26 | + for i := 0; i < b.N; i++ { |
| 27 | + _, _ = c.Get("key") |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func BenchmarkShardedCache_Set(b *testing.B) { |
| 32 | + c := NewShardedCache[int](32, DefaultExpiration, 0) |
| 33 | + b.ResetTimer() |
| 34 | + b.RunParallel(func(pb *testing.PB) { |
| 35 | + i := 0 |
| 36 | + for pb.Next() { |
| 37 | + c.Set(fmt.Sprintf("key-%d", i), i, DefaultExpiration) |
| 38 | + i++ |
| 39 | + } |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +func BenchmarkShardedCache_Get(b *testing.B) { |
| 44 | + c := NewShardedCache[int](32, DefaultExpiration, 0) |
| 45 | + for i := 0; i < 1000; i++ { |
| 46 | + c.Set(fmt.Sprintf("key-%d", i), i, DefaultExpiration) |
| 47 | + } |
| 48 | + b.ResetTimer() |
| 49 | + b.RunParallel(func(pb *testing.PB) { |
| 50 | + i := 0 |
| 51 | + for pb.Next() { |
| 52 | + _, _ = c.Get(fmt.Sprintf("key-%d", i%1000)) |
| 53 | + i++ |
| 54 | + } |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +func BenchmarkCache_WithCapacity_Set(b *testing.B) { |
| 59 | + c := New[int](DefaultExpiration, 0) |
| 60 | + c.WithCapacity(1000) |
| 61 | + b.ResetTimer() |
| 62 | + for i := 0; i < b.N; i++ { |
| 63 | + c.Set(fmt.Sprintf("key-%d", i), i, DefaultExpiration) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// --- Numeric Cache Benchmarks --- |
| 68 | + |
| 69 | +func BenchmarkNumericCache_Incr(b *testing.B) { |
| 70 | + nc := &NumericCache[int]{New[int](DefaultExpiration, 0).cache} |
| 71 | + nc.Set("counter", 0, DefaultExpiration) |
| 72 | + b.ResetTimer() |
| 73 | + for i := 0; i < b.N; i++ { |
| 74 | + _, _ = nc.Incr("counter", 1) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// --- Set Cache Benchmarks --- |
| 79 | + |
| 80 | +func BenchmarkSetCache_Add(b *testing.B) { |
| 81 | + sc := NewSetCache(DefaultExpiration, 0) |
| 82 | + b.ResetTimer() |
| 83 | + for i := 0; i < b.N; i++ { |
| 84 | + sc.AddMember("key", fmt.Sprintf("mem-%d", i%100), time.Minute, time.Minute) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func BenchmarkSetCache_Count_100(b *testing.B) { |
| 89 | + sc := NewSetCache(DefaultExpiration, 0) |
| 90 | + for i := 0; i < 100; i++ { |
| 91 | + sc.AddMember("key", fmt.Sprintf("mem-%d", i), time.Minute, time.Minute) |
| 92 | + } |
| 93 | + b.ResetTimer() |
| 94 | + for i := 0; i < b.N; i++ { |
| 95 | + _ = sc.Count("key") |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// --- Redis Integrated Benchmarks --- |
| 100 | + |
| 101 | +func BenchmarkRedisCache_Set_Async(b *testing.B) { |
| 102 | + rdb := getRedisClientForBench() |
| 103 | + if rdb == nil { |
| 104 | + b.Skip("Redis not available") |
| 105 | + } |
| 106 | + c := New[int](DefaultExpiration, 0) |
| 107 | + c.WithRedis(rdb) |
| 108 | + defer c.Close() |
| 109 | + |
| 110 | + b.ResetTimer() |
| 111 | + for i := 0; i < b.N; i++ { |
| 112 | + c.Set("bench_key", i, DefaultExpiration) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func BenchmarkRedisCache_Get_Fallback(b *testing.B) { |
| 117 | + rdb := getRedisClientForBench() |
| 118 | + if rdb == nil { |
| 119 | + b.Skip("Redis not available") |
| 120 | + } |
| 121 | + c := New[int](DefaultExpiration, 0) |
| 122 | + c.WithRedis(rdb) |
| 123 | + defer c.Close() |
| 124 | + |
| 125 | + key := "bench_fallback_key" |
| 126 | + c.Set(key, 123, DefaultExpiration) |
| 127 | + time.Sleep(100 * time.Millisecond) // Wait for async write |
| 128 | + |
| 129 | + b.ResetTimer() |
| 130 | + for i := 0; i < b.N; i++ { |
| 131 | + c.Flush() // Clear local to force fallback |
| 132 | + _, _ = c.Get(key) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func BenchmarkRedisNumeric_Incr_Sync(b *testing.B) { |
| 137 | + rdb := getRedisClientForBench() |
| 138 | + if rdb == nil { |
| 139 | + b.Skip("Redis not available") |
| 140 | + } |
| 141 | + nc := &NumericCache[int]{New[int](DefaultExpiration, 0).cache} |
| 142 | + nc.WithRedis(rdb) |
| 143 | + defer nc.Close() |
| 144 | + |
| 145 | + key := "bench_nc_sync" |
| 146 | + nc.Set(key, 0, DefaultExpiration) |
| 147 | + time.Sleep(100 * time.Millisecond) |
| 148 | + |
| 149 | + b.ResetTimer() |
| 150 | + for i := 0; i < b.N; i++ { |
| 151 | + _, _ = nc.Incr(key, 1) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +// --- Helpers --- |
| 156 | + |
| 157 | +func getRedisClientForBench() RedisClient { |
| 158 | + rdb := redis.NewClient(&redis.Options{ |
| 159 | + Addr: "localhost:6379", |
| 160 | + }) |
| 161 | + ctx := context.Background() |
| 162 | + if err := rdb.Ping(ctx).Err(); err != nil { |
| 163 | + return nil |
| 164 | + } |
| 165 | + rdb.FlushDB(ctx) |
| 166 | + return &RedisAdapter{client: rdb} |
| 167 | +} |
0 commit comments