-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_test.go
90 lines (75 loc) · 2.16 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
package rangechain
import (
"testing"
"time"
)
var size10Slice = makeIntSliceOfSize(10)
var size100Slice = makeIntSliceOfSize(100)
var size1000Slice = makeIntSliceOfSize(1000)
var sliceOfSlice = [][]int{size1000Slice, size1000Slice, size1000Slice}
func BenchmarkIntermediate10(b *testing.B) {
benchmarkIntermediate(b, size10Slice)
}
func BenchmarkIntermediate100(b *testing.B) {
benchmarkIntermediate(b, size100Slice)
}
func BenchmarkIntermediate1000(b *testing.B) {
benchmarkIntermediate(b, size1000Slice)
}
func BenchmarkForLoop1000(b *testing.B) {
for runIndex := 0; runIndex < b.N; runIndex++ {
var endingSlice []int
for value, _ := range size1000Slice {
if value%2 != 0 {
break
}
mappedValue := value*2 + 2
endingSlice = append(endingSlice, mappedValue)
}
}
}
func BenchmarkFlatten1000(b *testing.B) {
for runIndex := 0; runIndex < b.N; runIndex++ {
FromSlice(sliceOfSlice).Flatten().Filter(func(value interface{}) (bool, error) {
intValue := value.(int)
return intValue%2 == 0, nil
}).Map(func(value interface{}) (interface{}, error) {
intValue := value.(int)
return intValue*2 + 2, nil
}).Slice()
}
}
func BenchmarkSleepWithSerialMap(b *testing.B) {
for runIndex := 0; runIndex < b.N; runIndex++ {
FromSlice(size10Slice).Map(func(value interface{}) (interface{}, error) {
time.Sleep(100 * time.Millisecond)
return value, nil
}).Slice()
}
}
func BenchmarkSleepWithParallelMap(b *testing.B) {
for runIndex := 0; runIndex < b.N; runIndex++ {
FromSlice(size10Slice).MapParallel(func(value interface{}) (interface{}, error) {
time.Sleep(100 * time.Millisecond)
return value, nil
}).Slice()
}
}
func makeIntSliceOfSize(size int) []int {
slice := make([]int, size)
for index := 0; index < size; index++ {
slice[index] = index
}
return slice
}
func benchmarkIntermediate(b *testing.B, inputSlice []int) {
for runIndex := 0; runIndex < b.N; runIndex++ {
FromSlice(inputSlice).Filter(func(value interface{}) (bool, error) {
intValue := value.(int)
return intValue%2 == 0, nil
}).Map(func(value interface{}) (interface{}, error) {
intValue := value.(int)
return intValue*2 + 2, nil
}).Slice()
}
}