-
Notifications
You must be signed in to change notification settings - Fork 0
/
workit_bench_test.go
103 lines (87 loc) · 2.32 KB
/
workit_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
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
package workit_test
import (
"errors"
"strconv"
"sync/atomic"
"testing"
"github.com/jsteenb2/workit"
)
func BenchmarkQueue(b *testing.B) {
tests := []int{1, 2, 4, 8, 16, 32, 64, 128, 1024, 10000}
b.Run("unbuffered", func(b *testing.B) {
b.Run("no errFn", func(b *testing.B) {
for _, numWorkers := range tests {
b.Run("workers-"+strconv.Itoa(numWorkers), benchQueue1MillionNoErr(numWorkers))
}
})
b.Run("has errFn", func(b *testing.B) {
for _, numWorkers := range tests {
b.Run("workers-"+strconv.Itoa(numWorkers), benchQueue1MillionWithErr(numWorkers))
}
})
})
b.Run("buffered", func(b *testing.B) {
sizes := []int{1, 2, 4, 8, 16, 32, 64, 128, 1024, 10000}
for _, size := range sizes {
b.Run("size-"+strconv.Itoa(size), func(b *testing.B) {
b.Run("no errFn", func(b *testing.B) {
for _, numWorkers := range tests {
b.Run("workers-"+strconv.Itoa(numWorkers), benchQueue1MillionNoErr(numWorkers))
}
})
b.Run("has errFn", func(b *testing.B) {
for _, numWorkers := range tests {
b.Run("workers-"+strconv.Itoa(numWorkers), benchQueue1MillionWithErr(numWorkers))
}
})
})
}
})
}
func benchQueue1MillionNoErr(numWorkers int, opts ...workit.QueueOptFn) func(*testing.B) {
return func(b *testing.B) {
queue := workit.New(numWorkers, opts...)
queue.Start()
var count int64
expectedCalls := 1000000
for i := 0; i < expectedCalls; i++ {
queue.Add(func() error {
atomic.AddInt64(&count, 1)
return nil
}, nil)
}
err := isCount(&count, expectedCalls)
if err != nil {
b.Errorf("failed call count: got=%s", err)
}
queue.Close()
err = isFinished(queue, 0)
if err != nil {
b.Errorf("failed to finish: got=%s", err)
}
}
}
func benchQueue1MillionWithErr(numWorkers int, opts ...workit.QueueOptFn) func(*testing.B) {
return func(b *testing.B) {
queue := workit.New(numWorkers, opts...)
queue.Start()
var count int64
expectedCalls := 1000000
for i := 0; i < expectedCalls; i++ {
queue.Add(func() error {
return errors.New("an error")
}, func(e error) {
atomic.AddInt64(&count, 1)
})
}
err := isCount(&count, expectedCalls)
if err != nil {
b.Errorf("failed count: got=%s", err)
}
queue.Close()
err = isFinished(queue, 0)
if err != nil {
b.Errorf("failed to finish: got=%s", err)
}
}
}