-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_bench_test.go
178 lines (162 loc) · 2.96 KB
/
go_bench_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package threading
import (
"go.uber.org/atomic"
"sync"
"testing"
"time"
)
const (
Concurrency = 10
executeTime = 10000
goCount = 1000
)
var add atomic.Int64
func Job() {
add.Add(1)
time.Sleep(time.Millisecond * 50)
for i := 0; i < 10000; i++ {
i++
}
}
func TestMain(m *testing.M) {
m.Run()
}
func BenchmarkCurrent1Reuse(b *testing.B) {
for i := 0; i < b.N; i++ {
WithReuse(Concurrency)
}
}
func BenchmarkCurrent10Reuse(b *testing.B) {
for i := 0; i < b.N; i++ {
WithReuse(Concurrency * 10)
}
}
func BenchmarkCurrent100Reuse(b *testing.B) {
for i := 0; i < b.N; i++ {
WithReuse(Concurrency * 100)
}
}
func BenchmarkCurrent1kReuse(b *testing.B) {
for i := 0; i < b.N; i++ {
WithReuse(Concurrency * 1000)
}
}
func BenchmarkCurrent10kReuse(b *testing.B) {
StartPool(SetMaxIdleWorkerDuration(time.Second*30), SetMinWorkCount(goCount))
for i := 0; i < b.N; i++ {
gs := New(Config{
GoCount: goCount,
})
for j := 0; j < goCount; j++ {
_ = gs.Go(func() error {
Job()
return nil
})
}
_ = gs.Wait()
}
}
//ant
//func BenchmarkCurrent10kReuseAnt(b *testing.B) {
// var p, _ = ants.NewPool(goCount)
// for i := 0; i < b.N; i++ {
// var wg sync.WaitGroup
// for j := 0; j < goCount; j++ {
// wg.Add(1)
// _ = p.Submit(func() {
// Job()
// wg.Done()
// })
// }
// wg.Wait()
// }
// p.Release()
//
//}
//factory
//func BenchmarkCurrent10kReuseFactory(b *testing.B) {
// var master = factory.NewMaster(goCount, goCount)
// for i := 0; i < b.N; i++ {
// var line1 = master.AddLine(func(args interface{}) {
// Job()
// })
// for j := 0; j < goCount; j++ {
// line1.Submit(j)
// }
// line1.Wait()
// }
//
//}
func BenchmarkCurrent1NotReuse1(b *testing.B) {
for i := 0; i < b.N; i++ {
WithoutReuse1(Concurrency * 1)
}
}
func BenchmarkCurrent10NotReuse1(b *testing.B) {
for i := 0; i < b.N; i++ {
WithoutReuse1(Concurrency * 10)
}
}
func BenchmarkCurrent100NotReuse1(b *testing.B) {
for i := 0; i < b.N; i++ {
WithoutReuse1(Concurrency * 100)
}
}
func BenchmarkCurrent10kNotReuse1(b *testing.B) {
for i := 0; i < b.N; i++ {
WithoutReuse1(Concurrency * 10000)
}
}
func BenchmarkCurrent1NotReuse2(b *testing.B) {
for i := 0; i < b.N; i++ {
WithoutReuse2(Concurrency * 1)
}
}
func BenchmarkCurrent10NotReuse2(b *testing.B) {
for i := 0; i < b.N; i++ {
WithoutReuse2(Concurrency * 10)
}
}
func BenchmarkCurrent10kNotReuse2(b *testing.B) {
for i := 0; i < b.N; i++ {
WithoutReuse2(goCount)
}
}
func WithReuse(c int) {
gs := New(Config{
GoCount: c,
Wait: true,
})
for j := 0; j < c; j++ {
_ = gs.Go(func() error {
Job()
return nil
})
}
_ = gs.Wait()
}
func WithoutReuse1(c int) {
gs := New(Config{
GoCount: c,
Wait: true,
NotReuse: true,
})
for j := 0; j < c; j++ {
_ = gs.Go(func() error {
Job()
return nil
})
}
_ = gs.Wait()
}
func WithoutReuse2(c int) {
wp := sync.WaitGroup{}
for j := 0; j < c; j++ {
wp.Add(1)
go func() {
Job()
wp.Done()
}()
}
wp.Wait()
}