-
Notifications
You must be signed in to change notification settings - Fork 0
/
gocache_benchmark_test.go
80 lines (66 loc) · 1.43 KB
/
gocache_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
75
76
77
78
79
80
package gocache
import (
"fmt"
"strconv"
"sync"
"testing"
"time"
)
func BenchmarkCache_Set(b *testing.B) {
cache := NewCache(time.Minute)
for i := 0; i < b.N; i++ {
key := strconv.Itoa(i)
value := fmt.Sprintf("value%d", i)
cache.Set(key, value, time.Minute)
}
}
func BenchmarkCache_Get(b *testing.B) {
cache := NewCache(time.Minute)
for i := 0; i < b.N; i++ {
key := strconv.Itoa(i)
value := fmt.Sprintf("value%d", i)
cache.Set(key, value, time.Minute)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := strconv.Itoa(i)
cache.Get(key)
}
}
func BenchmarkCache_Delete(b *testing.B) {
cache := NewCache(time.Minute)
for i := 0; i < b.N; i++ {
key := strconv.Itoa(i)
value := fmt.Sprintf("value%d", i)
cache.Set(key, value, time.Minute)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := strconv.Itoa(i)
cache.Delete(key)
}
}
func BenchmarkCache_ConcurrentAccess(b *testing.B) {
cache := NewCache(time.Minute)
concurrency := 100
numOperations := b.N
// Populate the cache with initial data
for i := 0; i < concurrency; i++ {
key := strconv.Itoa(i)
value := fmt.Sprintf("value%d", i)
cache.Set(key, value, time.Minute)
}
// Run concurrent access to the cache
var wg sync.WaitGroup
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
go func() {
defer wg.Done()
for j := 0; j < numOperations; j++ {
key := strconv.Itoa(j % concurrency)
cache.Get(key)
}
}()
}
wg.Wait()
}