-
Notifications
You must be signed in to change notification settings - Fork 16
/
benchmark_test.go
64 lines (59 loc) · 1.53 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
package stream
import (
"fmt"
"sort"
"testing"
"time"
)
func BenchmarkParallelByCPU(b *testing.B) {
tests := []struct {
name string
goroutines int
action func(int, int)
}{
{name: "no Parallel", goroutines: 0},
{name: "Goroutines", goroutines: 2},
{name: "Goroutines", goroutines: 4},
{name: "Goroutines", goroutines: 6},
{name: "Goroutines", goroutines: 8},
{name: "Goroutines", goroutines: 10},
}
s := newArray(100)
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++ {
NewSlice(s).Parallel(tt.goroutines).ForEach(func(i int, v int) {
sort.Ints(newArray(1000)) // Simulate time-consuming CPU operations
})
}
})
}
}
func BenchmarkParallelByIO(b *testing.B) {
tests := []struct {
name string
goroutines int
action func(int, int)
}{
{name: "no Parallel", goroutines: 0},
{name: "Goroutines", goroutines: 2},
{name: "Goroutines", goroutines: 4},
{name: "Goroutines", goroutines: 6},
{name: "Goroutines", goroutines: 8},
{name: "Goroutines", goroutines: 10},
{name: "Goroutines", goroutines: 50},
{name: "Goroutines", goroutines: 100},
}
s := newArray(100)
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++ {
NewSlice(s).Parallel(tt.goroutines).ForEach(func(i int, v int) {
time.Sleep(time.Millisecond) // Simulate time-consuming IO operations
})
}
})
}
}