-
Notifications
You must be signed in to change notification settings - Fork 25
/
gopool_benchmark_test.go
110 lines (93 loc) · 1.84 KB
/
gopool_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
package gopool
import (
"sync"
"testing"
"time"
"github.com/alitto/pond"
"github.com/daniel-hutao/spinlock"
"github.com/panjf2000/ants/v2"
)
const (
PoolSize = 1e4
TaskNum = 1e6
)
func BenchmarkGoPool(b *testing.B) {
pool := NewGoPool(PoolSize)
defer pool.Release()
taskFunc := func() (interface{}, error) {
time.Sleep(10 * time.Millisecond)
return nil, nil
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for num := 0; num < TaskNum; num++ {
pool.AddTask(taskFunc)
}
pool.Wait()
}
b.StopTimer()
}
func BenchmarkGoPoolWithSpinLock(b *testing.B) {
pool := NewGoPool(PoolSize, WithLock(new(spinlock.SpinLock)))
defer pool.Release()
taskFunc := func() (interface{}, error) {
time.Sleep(10 * time.Millisecond)
return nil, nil
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for num := 0; num < TaskNum; num++ {
pool.AddTask(taskFunc)
}
pool.Wait()
}
b.StopTimer()
}
func BenchmarkGoroutines(b *testing.B) {
var wg sync.WaitGroup
var taskNum = int(TaskNum)
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(taskNum)
for num := 0; num < taskNum; num++ {
go func() (interface{}, error) {
time.Sleep(10 * time.Millisecond)
wg.Done()
return nil, nil
}()
}
wg.Wait()
}
}
func BenchmarkPond(b *testing.B) {
pool := pond.New(PoolSize, 0, pond.MinWorkers(PoolSize))
taskFunc := func() {
time.Sleep(10 * time.Millisecond)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := 0; i < TaskNum; i++ {
pool.Submit(taskFunc)
}
pool.StopAndWait()
}
b.StopTimer()
}
func BenchmarkAnts(b *testing.B) {
var wg sync.WaitGroup
p, _ := ants.NewPool(PoolSize)
defer p.Release()
taskFunc := func() {
time.Sleep(10 * time.Millisecond)
wg.Done()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := 0; i < TaskNum; i++ {
wg.Add(1)
_ = p.Submit(taskFunc)
}
wg.Wait()
}
b.StopTimer()
}