-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_benchmark_test.go
74 lines (60 loc) · 1.49 KB
/
cache_benchmark_test.go
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
package cache
import (
"fmt"
"testing"
"time"
)
func BenchmarkCache_Set(b *testing.B) {
cache, _ := NewCacheStore(time.Second * 1)
defer cache.CloseCacheStore()
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := fmt.Sprintf("key%d", i)
value := fmt.Sprintf("value%d", i)
duration := time.Second * 5 // Example expiration duration
b.Run(fmt.Sprintf("Set-%d", i), func(b *testing.B) {
for j := 0; j < b.N; j++ {
cache.Set(key, value, duration)
}
})
}
}
func BenchmarkCache_Get(b *testing.B) {
cache, _ := NewCacheStore(time.Second * 1)
defer cache.CloseCacheStore()
key := "key"
value := "value"
duration := time.Second * 5
cache.Set(key, value, duration)
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.Run(fmt.Sprintf("Get-%d", i), func(b *testing.B) {
for j := 0; j < b.N; j++ {
_, _, _ = cache.Get(key)
}
})
}
}
func BenchmarkCache_Iterate(b *testing.B) {
cache, _ := NewCacheStore(time.Second * 1)
defer cache.CloseCacheStore()
// Populate the cache with some items for iteration
for i := 0; i < 1000; i++ {
key := fmt.Sprintf("key%d", i)
value := fmt.Sprintf("value%d", i)
cache.Set(key, value, 0)
}
b.ResetTimer()
// Run the benchmark for Iterate
b.Run("Iterate", func(b *testing.B) {
for i := 0; i < b.N; i++ {
err := cache.Iterate(func(key, value interface{}) bool {
// Do nothing in the callback function for benchmarking purposes
return true
})
if err != nil {
b.Fatalf("Error during iteration: %v", err)
}
}
})
}