-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
224 lines (165 loc) · 5.51 KB
/
benchmark_test.go
File metadata and controls
224 lines (165 loc) · 5.51 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
package tempuscache
import (
"fmt"
"testing"
"time"
)
/*
BenchmarkSet measures the performance of the Set() operation
under repeated overwrites of the same key.
================================================================================
OBJECTIVE
================================================================================
This benchmark evaluates the steady-state cost of:
- Expiration timestamp calculation (time.Now + ttl)
- Mutex Lock()/Unlock() overhead
- Map update (existing key path)
- LRU move-to-front operation
================================================================================
BENCHMARK MODEL
================================================================================
- The same key is repeatedly overwritten.
- Map size remains constant.
- No eviction pressure.
- Minimal memory growth.
This isolates the hot write path without structural expansion effects.
================================================================================
GO BENCHMARK MECHANICS
================================================================================
The testing framework dynamically determines b.N
to achieve stable timing results.
Use:
go test -bench=. -benchmem
to observe:
- ns/op
- B/op
- allocs/op
================================================================================
INTERPRETATION
================================================================================
This benchmark reflects ideal write throughput
in a stable cache state.
*/
func BenchmarkSet(b *testing.B) {
cache := New()
for i := 0; i < b.N; i++ {
cache.Set("key", "value", 5*time.Second)
}
}
/*
BenchmarkSetUnique measures write performance
when inserting unique keys.
================================================================================
OBJECTIVE
================================================================================
Unlike BenchmarkSet, this benchmark:
- Forces map growth.
- Exercises LRU insert path.
- Tests capacity-bound behavior.
With WithMaxEntries(b.N + 1),
evictions are avoided to measure pure growth cost.
================================================================================
WHAT IT CAPTURES
================================================================================
- Map allocation behavior
- Linked list node allocation
- Memory growth impact
- Lock overhead under expanding state
This benchmark represents a more realistic
write-heavy workload scenario.
*/
func BenchmarkSetUnique(b *testing.B) {
cache := New(WithMaxEntries(b.N + 1))
for i := 0; i < b.N; i++ {
cache.Set(fmt.Sprintf("key%d", i), i, 0)
}
}
/*
BenchmarkGet measures the performance of the read path.
================================================================================
OBJECTIVE
================================================================================
Evaluates the cost of:
- Map lookup
- Expiration check
- LRU move-to-front operation
- Mutex overhead
- Stats increment
================================================================================
SCENARIO
================================================================================
- A single key is preloaded.
- Repeated Get() calls simulate high read locality.
- No expiration or eviction occurs.
This benchmark reflects hot-cache read performance.
*/
func BenchmarkGet(b *testing.B) {
cache := New()
cache.Set("key", "value", 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
cache.Get("key")
}
}
/*
BenchmarkParallelGet measures read performance
under concurrent access.
================================================================================
OBJECTIVE
================================================================================
Simulates high-concurrency read workloads
using b.RunParallel.
Evaluates:
- Lock contention behavior
- Throughput scaling across CPU cores
- Stability under parallel goroutines
================================================================================
WHY THIS MATTERS
================================================================================
Caches are typically read-heavy systems.
Understanding concurrent read scalability
is critical for backend performance analysis.
Run with:
go test -bench=. -cpu=1,2,4,8
to observe scaling behavior.
*/
func BenchmarkParallelGet(b *testing.B) {
cache := New()
cache.Set("key", "value", 0)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
cache.Get("key")
}
})
}
/*
BenchmarkEviction measures write performance
under constant eviction pressure.
================================================================================
SCENARIO
================================================================================
- Cache capacity is limited (WithMaxEntries(100)).
- Continuous unique inserts exceed capacity.
- LRU eviction is triggered repeatedly.
================================================================================
WHAT IT EVALUATES
================================================================================
- O(1) eviction behavior
- removeElement() efficiency
- Map delete performance
- List removal cost
- Stats increment overhead
================================================================================
SYSTEM INSIGHT
================================================================================
This benchmark reflects memory-bounded
production scenarios where eviction
is part of the steady-state workload.
*/
func BenchmarkEviction(b *testing.B) {
cache := New(WithMaxEntries(100))
for i := 0; i < b.N; i++ {
cache.Set(fmt.Sprintf("key%d", i), i, 0)
}
}