-
Notifications
You must be signed in to change notification settings - Fork 19
/
benchmark_test.go
140 lines (121 loc) · 2.78 KB
/
benchmark_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
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
package rill
import (
"testing"
"time"
"github.com/destel/rill/internal/th"
)
const benchmarkInputSize = 100000
// code called on each benchmark iteration
func benchmarkIteration() {
busySleep(1 * time.Microsecond)
//time.Sleep(1 * time.Microsecond)
//busySleep(10 * time.Microsecond)
//time.Sleep(10 * time.Microsecond)
}
func busySleep(d time.Duration) {
if d == 0 {
return
}
start := time.Now()
for time.Since(start) < d {
}
}
func runBenchmark(b *testing.B, name string, body func(in <-chan Try[int])) {
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
in := make(chan Try[int])
done := make(chan struct{})
go func() {
defer close(done)
body(in)
}()
// Give body a some time to spawn goroutines
time.Sleep(100 * time.Millisecond)
b.StartTimer()
// write to input
for k := 0; k < benchmarkInputSize; k++ {
in <- Try[int]{Value: k}
}
close(in)
// wait for body to finish
<-done
b.StopTimer()
}
})
}
// Benchmarks below are commented out to remove dependency on errgroup
//// This benchmark uses classic goroutine-per-item + semaphore pattern.
//func BenchmarkErrGroupWithSetLimit(b *testing.B) {
// for _, n := range []int{1, 2, 4, 8} {
// runBenchmark(b, th.Name(n), func(in <-chan Try[int]) {
// var eg errgroup.Group
// eg.SetLimit(n)
//
// for x := range in {
// x := x
// eg.Go(func() error {
// if err := x.Error; err != nil {
// return err
// }
// benchmarkIteration()
// return nil
// })
// }
//
// _ = eg.Wait()
// })
// }
//}
//
//// This benchmark uses much less common worker pool pattern.
//func BenchmarkErrGroupWithWorkerPool(b *testing.B) {
// for _, n := range []int{1, 2, 4, 8} {
// runBenchmark(b, th.Name(n), func(in <-chan Try[int]) {
// var eg errgroup.Group
// for i := 0; i < n; i++ {
// eg.Go(func() error {
// for x := range in {
// if err := x.Error; err != nil {
// return err
// }
// benchmarkIteration()
// }
// return nil
// })
// }
// _ = eg.Wait()
// })
// }
//}
func BenchmarkForEach(b *testing.B) {
for _, n := range []int{1, 2, 4, 8} {
runBenchmark(b, th.Name(n), func(in <-chan Try[int]) {
_ = ForEach(in, n, func(x int) error {
benchmarkIteration()
return nil
})
})
}
}
func BenchmarkMapAndDrain(b *testing.B) {
for _, n := range []int{1, 2, 4, 8} {
runBenchmark(b, th.Name(n), func(in <-chan Try[int]) {
out := Map(in, n, func(x int) (int, error) {
benchmarkIteration()
return x, nil
})
Drain(out)
})
}
}
func BenchmarkReduce(b *testing.B) {
for _, n := range []int{1, 2, 4, 8} {
runBenchmark(b, th.Name(n), func(in <-chan Try[int]) {
_, _, _ = Reduce(in, n, func(x, y int) (int, error) {
benchmarkIteration()
return x, nil
})
})
}
}