-
Notifications
You must be signed in to change notification settings - Fork 15
/
benchmark_test.go
77 lines (71 loc) · 1.62 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
package stream
import (
"fmt"
"testing"
)
func BenchmarkShortCircuiting(b *testing.B) {
tests := []struct {
name string
count int
}{
{count: 100},
{count: 200},
{count: 300},
{count: 400},
{count: 500},
{count: 1000},
{count: 2000},
}
for _, tt := range tests {
b.Run(fmt.Sprintf("%s(%d)", "count:", tt.count), func(b *testing.B) {
s := newArray(tt.count)
s[0] = 101
b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = NewSlice(s).
Filter(func(v int) bool { return true }).
Map(func(v int) int {
return v
}).
AllMatch(func(v int) bool { return v < 100 })
}
})
}
}
var funcVal = mapperFunc
func mapperFunc(v int) int { return v * 2 }
// TODO optimize func variable 3.287 ns/op, other(normal,anonymous) 1.553 ns/op
func BenchmarkPipelineFuncVariable(b *testing.B) {
tests := []struct {
name string
goroutines int
stage func(index int, e int) (isReturn bool, isComplete bool, ret int)
}{
{
name: "variable func",
stage: func(index int, e int) (isReturn bool, isComplete bool, ret int) {
return true, false, funcVal(e)
},
},
{
name: "normal func",
stage: func(index int, e int) (isReturn bool, isComplete bool, ret int) {
return true, false, mapperFunc(e)
},
},
{
name: "anonymous func",
stage: func(index int, e int) (isReturn bool, isComplete bool, ret int) {
return true, false, func(v int) int { return v * 2 }(e)
},
},
}
for _, tt := range tests {
b.Run(fmt.Sprintf("%s(%d)", tt.name, tt.goroutines), func(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
tt.stage(0, 0)
}
})
}
}