forked from yu-xin-c/go-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_benchmark_test.go
More file actions
239 lines (203 loc) · 4.73 KB
/
Copy pathmap_benchmark_test.go
File metadata and controls
239 lines (203 loc) · 4.73 KB
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
package main
import (
"sync"
"testing"
"time"
)
// 测试配置
const (
BenchmarkSize = 10000 // 测试数据规模
BenchmarkWorkers = 10 // 并发工作协程数
ReadRatio = 0.8 // 读操作比例
WriteRatio = 0.1 // 写操作比例
DeleteRatio = 0.1 // 删除操作比例
)
// 普通map+锁实现
type NormalMap struct {
data map[string]string
mu sync.RWMutex
}
func NewNormalMap() *NormalMap {
return &NormalMap{
data: make(map[string]string),
}
}
func (m *NormalMap) Get(key string) (string, bool) {
m.mu.RLock()
defer m.mu.RUnlock()
val, ok := m.data[key]
return val, ok
}
func (m *NormalMap) Set(key, value string) {
m.mu.Lock()
defer m.mu.Unlock()
m.data[key] = value
}
func (m *NormalMap) Delete(key string) {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.data, key)
}
// sync.Map实现
type SyncMap struct {
data sync.Map
}
func NewSyncMap() *SyncMap {
return &SyncMap{}
}
func (m *SyncMap) Get(key string) (string, bool) {
val, ok := m.data.Load(key)
if !ok {
return "", false
}
return val.(string), true
}
func (m *SyncMap) Set(key, value string) {
m.data.Store(key, value)
}
func (m *SyncMap) Delete(key string) {
m.data.Delete(key)
}
// 基准测试:读多写少场景
func BenchmarkMapReadHeavy(b *testing.B) {
// 初始化数据
normalMap := NewNormalMap()
syncMap := NewSyncMap()
// 预填充数据
for i := 0; i < BenchmarkSize; i++ {
key := "key" + string(rune(i))
value := "value" + string(rune(i))
normalMap.Set(key, value)
syncMap.Set(key, value)
}
b.Run("NormalMap", func(b *testing.B) {
benchmarkMap(b, normalMap)
})
b.Run("SyncMap", func(b *testing.B) {
benchmarkMap(b, syncMap)
})
}
// 基准测试:读写均衡场景
func BenchmarkMapBalanced(b *testing.B) {
// 初始化数据
normalMap := NewNormalMap()
syncMap := NewSyncMap()
// 预填充数据
for i := 0; i < BenchmarkSize; i++ {
key := "key" + string(rune(i))
value := "value" + string(rune(i))
normalMap.Set(key, value)
syncMap.Set(key, value)
}
b.Run("NormalMap", func(b *testing.B) {
benchmarkMapBalanced(b, normalMap)
})
b.Run("SyncMap", func(b *testing.B) {
benchmarkMapBalanced(b, syncMap)
})
}
// 通用Map接口
type MapInterface interface {
Get(key string) (string, bool)
Set(key, value string)
Delete(key string)
}
// 读多写少场景测试
func benchmarkMap(b *testing.B, m MapInterface) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
key := "key" + string(rune(i%BenchmarkSize))
value := "value" + string(rune(i%BenchmarkSize))
r := int(time.Now().UnixNano() % 100)
if r < int(ReadRatio*100) {
// 读操作
m.Get(key)
} else if r < int((ReadRatio+WriteRatio)*100) {
// 写操作
m.Set(key, value)
} else {
// 删除操作
m.Delete(key)
}
i++
}
})
}
// 读写均衡场景测试
func benchmarkMapBalanced(b *testing.B, m MapInterface) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
key := "key" + string(rune(i%BenchmarkSize))
value := "value" + string(rune(i%BenchmarkSize))
r := int(time.Now().UnixNano() % 2)
if r == 0 {
// 读操作
m.Get(key)
} else {
// 写操作
m.Set(key, value)
}
i++
}
})
}
// 基准测试:缓存场景模拟
func BenchmarkCacheScenario(b *testing.B) {
// 初始化数据
normalMap := NewNormalMap()
syncMap := NewSyncMap()
// 预填充数据
for i := 0; i < BenchmarkSize; i++ {
key := "cache_key" + string(rune(i))
value := "cache_value" + string(rune(i))
normalMap.Set(key, value)
syncMap.Set(key, value)
}
b.Run("NormalMap", func(b *testing.B) {
benchmarkCacheScenario(b, normalMap)
})
b.Run("SyncMap", func(b *testing.B) {
benchmarkCacheScenario(b, syncMap)
})
}
// 缓存场景模拟测试
func benchmarkCacheScenario(b *testing.B, m MapInterface) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
// 90% 的概率访问已存在的键(缓存命中)
// 10% 的概率访问新键(缓存未命中)
var key string
if int(time.Now().UnixNano()%10) < 9 {
// 缓存命中
key = "cache_key" + string(rune(i%BenchmarkSize))
} else {
// 缓存未命中
key = "cache_key_new" + string(rune(i))
}
// 先尝试读取
_, ok := m.Get(key)
if !ok {
// 缓存未命中,写入数据
m.Set(key, "cache_value"+string(rune(i)))
}
i++
}
})
}
func runMapBenchmarks() {
// 运行基准测试
t := &testing.B{}
println("=== 读多写少场景 ===")
BenchmarkMapReadHeavy(t)
println("\n=== 读写均衡场景 ===")
BenchmarkMapBalanced(t)
println("\n=== 缓存场景模拟 ===")
BenchmarkCacheScenario(t)
println("\n基准测试完成,请使用 'go test -bench=.' 命令运行完整测试")
}