-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbatcher_bench_test.go
57 lines (52 loc) · 1.13 KB
/
batcher_bench_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
package batchy
import (
"fmt"
"sync"
"testing"
"time"
)
// Batcher should handle millions of jobs efficiently
func BenchmarkBatcher(b *testing.B) {
for _, n := range []int{10, 20, 100, 1000} {
b.Run(fmt.Sprintf("itemLimit_%d", n), func(b *testing.B) {
benchmarkBatcher(b, n)
})
}
}
func benchmarkBatcher(b *testing.B, n int) {
batch := New(n, 10*time.Millisecond, processorFailsEven)
wg := sync.WaitGroup{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(1)
go func(i int) {
batch.Add(i)
wg.Done()
}(i)
}
wg.Wait()
}
// Batcher should handle millions of jobs efficiently even with 100ms latency
func BenchmarkBatcher100ms(b *testing.B) {
for _, n := range []int{10, 20, 100, 1000} {
b.Run(fmt.Sprintf("itemLimit_%d", n), func(b *testing.B) {
benchmarkBatcher100ms(b, n)
})
}
}
func benchmarkBatcher100ms(b *testing.B, n int) {
batch := New(n, 10*time.Millisecond, func(items []interface{}) (resp []error) {
time.Sleep(100 * time.Millisecond)
return
})
wg := sync.WaitGroup{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(1)
go func(i int) {
batch.Add(i)
wg.Done()
}(i)
}
wg.Wait()
}