-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbenchmark_test.go
69 lines (55 loc) · 1.19 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
package workerpool
import (
"runtime"
"testing"
)
func poolDelegate(b *testing.B, pool Pool) {
for n := 0; n < b.N; n++ {
if err := pool.Delegate(n); err != nil {
b.Fatal(err)
}
}
}
func poolDelegateParallel(b *testing.B, pool Pool) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := pool.Delegate(1); err != nil {
b.Fatal(err)
}
}
})
}
func runBenchmark(b *testing.B, workersAmount int, runInParallel bool) {
pool := New(b.N)
defer pool.Stop()
worker := func(i int) {}
for i := 1; i <= workersAmount; i++ {
if err := pool.AddWorker(worker); err != nil {
b.Fatal(err)
}
}
b.ResetTimer()
if runInParallel {
poolDelegateParallel(b, pool)
} else {
poolDelegate(b, pool)
}
}
func BenchmarkWorker1(b *testing.B) {
runBenchmark(b, 1, false)
}
func BenchmarkWorker1Parallel(b *testing.B) {
runBenchmark(b, 1, true)
}
func BenchmarkWorker100(b *testing.B) {
runBenchmark(b, 100, false)
}
func BenchmarkWorker100Parallel(b *testing.B) {
runBenchmark(b, 100, true)
}
func BenchmarkWorkerNumCPU(b *testing.B) {
runBenchmark(b, runtime.NumCPU()+1, false)
}
func BenchmarkWorkerNumCPUParallel(b *testing.B) {
runBenchmark(b, runtime.NumCPU()+1, true)
}