-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgoroutine_test.go
228 lines (220 loc) · 4.59 KB
/
goroutine_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//go:build exclude
package benchmark
import (
"runtime"
"sync"
"testing"
)
// BenchmarkGoroutineNew
func BenchmarkGoroutineNew(b *testing.B) {
vals := make([]int64, b.N)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
wg := &sync.WaitGroup{}
wg.Add(b.N)
cnt := int64(b.N)
for n := int64(0); n < cnt; n++ {
go calc(n, cnt, vals, wg)
}
wg.Wait()
}
})
}
func calc(n, cnt int64, vals []int64, wg *sync.WaitGroup) {
vals[n] = n % cnt
wg.Done()
}
// BenchmarkGoroutineChan
type (
grp struct {
vals []int64
valsC chan int64
wg *sync.WaitGroup
cnt int64
}
)
func newGrp(N int64, valChanCnt int64, loopCnt int64) (r *grp) {
r = &grp{
vals: make([]int64, N),
valsC: make(chan int64, valChanCnt),
wg: &sync.WaitGroup{},
cnt: N,
}
for l := int64(0); l < loopCnt; l++ {
go r.loop()
}
return r
}
func (g *grp) loop() {
for n := range g.valsC {
g.vals[n] = n % g.cnt
g.wg.Done()
}
}
func BenchmarkGoroutineChan1RteCPU(b *testing.B) {
cnt := int64(b.N)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
grp := newGrp(cnt, 1, int64(runtime.NumCPU()))
grp.wg.Add(b.N)
for n := int64(0); n < cnt; n++ {
grp.valsC <- n
}
grp.wg.Wait()
}
})
}
func BenchmarkGoroutineChan10RteCPU(b *testing.B) {
cnt := int64(b.N)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
grp := newGrp(cnt, 10, int64(runtime.NumCPU()))
grp.wg.Add(b.N)
for n := int64(0); n < cnt; n++ {
grp.valsC <- n
}
grp.wg.Wait()
}
})
}
func BenchmarkGoroutineChanCPURteCPU(b *testing.B) {
cnt := int64(b.N)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
grp := newGrp(cnt, int64(runtime.NumCPU()), int64(runtime.NumCPU()))
grp.wg.Add(b.N)
for n := int64(0); n < cnt; n++ {
grp.valsC <- n
}
grp.wg.Wait()
}
})
}
func BenchmarkGoroutineChan100RteCPU(b *testing.B) {
cnt := int64(b.N)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
grp := newGrp(cnt, 100, int64(runtime.NumCPU()))
grp.wg.Add(b.N)
for n := int64(0); n < cnt; n++ {
grp.valsC <- n
}
grp.wg.Wait()
}
})
}
func BenchmarkGoroutineChan10000RteCPU(b *testing.B) {
cnt := int64(b.N)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
grp := newGrp(cnt, 10000, int64(runtime.NumCPU()))
grp.wg.Add(b.N)
for n := int64(0); n < cnt; n++ {
grp.valsC <- n
}
grp.wg.Wait()
}
})
}
func BenchmarkGoroutineChan10Rte10(b *testing.B) {
cnt := int64(b.N)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
grp := newGrp(cnt, 10, 10)
grp.wg.Add(b.N)
for n := int64(0); n < cnt; n++ {
grp.valsC <- n
}
grp.wg.Wait()
}
})
}
func BenchmarkGoroutineChan100Rte100(b *testing.B) {
cnt := int64(b.N)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
grp := newGrp(cnt, 100, 100)
grp.wg.Add(b.N)
for n := int64(0); n < cnt; n++ {
grp.valsC <- n
}
grp.wg.Wait()
}
})
}
func BenchmarkGoroutineChan10000Rte10000(b *testing.B) {
cnt := int64(b.N)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
grp := newGrp(cnt, 10000, 10000)
grp.wg.Add(b.N)
for n := int64(0); n < cnt; n++ {
grp.valsC <- n
}
grp.wg.Wait()
}
})
}
// // BenchmarkGoroutineCond
// type (
// grpWithCond struct {
// vals []int64
// idx uint32
// rtes []*rte
// wg *sync.WaitGroup
// cnt int64
// }
// rte struct {
// n int64
// cond *sync.Cond
// g *grpWithCond
// }
// )
// func newGrpWithCond(N int64, loopCnt int64) (r *grpWithCond) {
// r = &grpWithCond{
// vals: make([]int64, N),
// rtes: make([]*rte, int64(runtime.NumCPU())),
// wg: &sync.WaitGroup{},
// cnt: N,
// }
// for l := int64(0); l < loopCnt; l++ {
// r.rtes[l] = &rte{
// n: math.MaxInt64,
// cond: &sync.Cond{L: &sync.Mutex{}},
// g: r,
// }
// go r.rtes[l].loop()
// }
// return r
// }
// func (r *rte) loop() {
// for {
// r.cond.L.Lock()
// for r.n == math.MaxInt64 {
// r.cond.Wait()
// }
// //fmt.Printf("=-=-=- COMPUTING n:%v \n", r.n)
// r.g.vals[r.n] = r.n % r.g.cnt
// atomic.StoreInt64(&r.n, math.MaxInt64)
// r.g.wg.Done()
// r.cond.L.Unlock()
// }
// }
// func BenchmarkGoroutineCondGosched(b *testing.B) {
// cnt := int64(b.N)
// loopCnt := int64(runtime.NumCPU())
// b.RunParallel(func(pb *testing.PB) {
// for pb.Next() {
// grp := newGrpWithCond(cnt, loopCnt)
// grp.wg.Add(b.N)
// for n := int64(0); n < cnt; n++ {
// runtime.Gosched()
// for !atomic.CompareAndSwapInt64(&grp.rtes[n%loopCnt].n, math.MaxInt64, n) {
// runtime.Gosched()
// }
// grp.rtes[n%loopCnt].cond.Signal()
// }
// grp.wg.Wait()
// }
// })
// }