forked from bep/debounce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebounce_bench_test.go
More file actions
311 lines (250 loc) · 6.53 KB
/
debounce_bench_test.go
File metadata and controls
311 lines (250 loc) · 6.53 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package debounce
import (
"fmt"
"runtime"
"sync"
"testing"
"time"
)
// BenchmarkNew measures the cost of creating a new debounced function
func BenchmarkNew(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = New(100 * time.Millisecond)
}
}
// BenchmarkNewWithOptions measures the cost of creating with options
func BenchmarkNewWithOptions(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = New(100*time.Millisecond, WithMaxCalls(5), WithMaxWait(1*time.Second))
}
}
// BenchmarkSingleCall measures the cost of a single debounced call
func BenchmarkSingleCall(b *testing.B) {
debounced := New(100 * time.Millisecond)
fn := func() {}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
debounced(fn)
}
}
// BenchmarkMultipleCalls measures the cost of multiple rapid calls
func BenchmarkMultipleCalls(b *testing.B) {
debounced := New(100 * time.Millisecond)
fn := func() {}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// Make 10 calls per iteration to simulate rapid calling
for j := 0; j < 10; j++ {
debounced(fn)
}
}
}
// BenchmarkCallLimitTrigger measures performance when call limit is reached
func BenchmarkCallLimitTrigger(b *testing.B) {
debounced := New(1*time.Second, WithMaxCalls(5))
fn := func() {}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// Make exactly 5 calls to trigger immediate execution
for j := 0; j < 5; j++ {
debounced(fn)
}
}
}
// BenchmarkTimeLimitTrigger measures performance when time limit is reached
func BenchmarkTimeLimitTrigger(b *testing.B) {
var wg sync.WaitGroup
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
debounced := New(1*time.Second, WithMaxWait(1*time.Millisecond))
wg.Add(1)
fn := func() {
wg.Done()
}
debounced(fn)
wg.Wait()
}
}
// BenchmarkConcurrentCalls measures performance under concurrent access
func BenchmarkConcurrentCalls(b *testing.B) {
debounced := New(100 * time.Millisecond)
fn := func() {}
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
debounced(fn)
}
})
}
// BenchmarkConcurrentCallsWithLimits measures concurrent performance with limits
func BenchmarkConcurrentCallsWithLimits(b *testing.B) {
debounced := New(100*time.Millisecond, WithMaxCalls(10), WithMaxWait(50*time.Millisecond))
fn := func() {}
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
debounced(fn)
}
})
}
// BenchmarkMemoryUsage measures memory allocation patterns
func BenchmarkMemoryUsage(b *testing.B) {
var m1, m2 runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m1)
debounced := New(100 * time.Millisecond)
fn := func() {}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
debounced(fn)
}
runtime.GC()
runtime.ReadMemStats(&m2)
b.ReportMetric(float64(m2.TotalAlloc-m1.TotalAlloc)/float64(b.N), "bytes/op")
}
// BenchmarkTimerCreation measures the cost of timer creation and cancellation
func BenchmarkTimerCreation(b *testing.B) {
debounced := New(1 * time.Millisecond)
fn := func() {}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// Each call creates and potentially cancels a timer
debounced(fn)
debounced(fn) // This will cancel the previous timer
}
}
// BenchmarkDifferentFunctions measures cost when different functions are passed
func BenchmarkDifferentFunctions(b *testing.B) {
debounced := New(100 * time.Millisecond)
functions := []func(){
func() { _ = 1 + 1 },
func() { _ = "hello" + "world" },
func() { _ = make([]int, 10) },
func() { _ = map[string]int{"test": 1} },
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
debounced(functions[i%len(functions)])
}
}
// BenchmarkScaling tests performance with different numbers of goroutines
func BenchmarkScaling(b *testing.B) {
goroutineCounts := []int{1, 2, 4, 8, 16, 32, 64, 128}
for _, numGoroutines := range goroutineCounts {
b.Run(fmt.Sprintf("goroutines-%d", numGoroutines), func(b *testing.B) {
debounced := New(100 * time.Millisecond)
fn := func() {}
b.ResetTimer()
b.ReportAllocs()
var wg sync.WaitGroup
callsPerGoroutine := b.N / numGoroutines
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < callsPerGoroutine; j++ {
debounced(fn)
}
}()
}
wg.Wait()
})
}
}
// BenchmarkWithVariousDurations tests performance with different debounce durations
func BenchmarkWithVariousDurations(b *testing.B) {
durations := []time.Duration{
1 * time.Nanosecond,
1 * time.Microsecond,
1 * time.Millisecond,
10 * time.Millisecond,
100 * time.Millisecond,
1 * time.Second,
}
for _, duration := range durations {
b.Run(fmt.Sprintf("duration-%v", duration), func(b *testing.B) {
debounced := New(duration)
fn := func() {}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
debounced(fn)
}
})
}
}
// BenchmarkCallLimitVariations tests performance with different call limits
func BenchmarkCallLimitVariations(b *testing.B) {
limits := []int{1, 2, 5, 10, 50, 100, -1}
for _, limit := range limits {
name := fmt.Sprintf("limit-%d", limit)
if limit == -1 {
name = "limit-none"
}
b.Run(name, func(b *testing.B) {
debounced := New(100*time.Millisecond, WithMaxCalls(limit))
fn := func() {}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
debounced(fn)
}
})
}
}
// BenchmarkReset measures the cost of counter and timer reset after execution
func BenchmarkReset(b *testing.B) {
debounced := New(1*time.Nanosecond, WithMaxCalls(1)) // Immediate execution
var wg sync.WaitGroup
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
wg.Add(1)
debounced(func() {
wg.Done()
})
wg.Wait() // Wait for execution and reset
}
}
// BenchmarkHighFrequency simulates high-frequency scenarios
func BenchmarkHighFrequency(b *testing.B) {
debounced := New(1*time.Millisecond, WithMaxCalls(1000))
fn := func() {}
b.ResetTimer()
b.ReportAllocs()
// Simulate burst of calls
for i := 0; i < b.N; i++ {
for j := 0; j < 100; j++ { // 100 rapid calls per iteration
debounced(fn)
}
}
}
// BenchmarkComparison compares debounced vs direct function calls
func BenchmarkComparison(b *testing.B) {
fn := func() { _ = 1 + 1 }
b.Run("direct-call", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
fn()
}
})
b.Run("debounced-call", func(b *testing.B) {
debounced := New(100 * time.Millisecond)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
debounced(fn)
}
})
}