1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 func TestChanSendInterface(t
*testing
.T
) {
17 c
:= make(chan interface{}, 1)
30 func TestPseudoRandomSend(t
*testing
.T
) {
37 for i
:= 0; i
< n
; i
++ {
43 for i
:= 0; i
< n
; i
++ {
55 if n0
> n
/10 && n1
> n
/10 {
59 t
.Errorf("Want pseudo random, got %d zeros and %d ones", n0
, n1
)
62 func TestMultiConsumer(t
*testing
.T
) {
66 pn
:= []int{2, 3, 7, 11, 13, 17, 19, 23, 27, 31}
68 q
:= make(chan int, nwork
*3)
69 r
:= make(chan int, nwork
*3)
73 for i
:= 0; i
< nwork
; i
++ {
77 // mess with the fifo-ish nature of range
78 if pn
[w%len
(pn
)] == v
{
90 for i
:= 0; i
< niter
; i
++ {
95 close(q
) // no more work
96 wg
.Wait() // workers done
97 close(r
) // ... so there can be no more results
107 if n
!= niter || s
!= expect
{
108 t
.Errorf("Expected sum %d (got %d) from %d iter (saw %d)",
113 func BenchmarkSelectUncontended(b
*testing
.B
) {
114 const CallsPerSched
= 1000
115 procs
:= runtime
.GOMAXPROCS(-1)
116 N
:= int32(b
.N
/ CallsPerSched
)
117 c
:= make(chan bool, procs
)
118 for p
:= 0; p
< procs
; p
++ {
120 myc1
:= make(chan int, 1)
121 myc2
:= make(chan int, 1)
123 for atomic
.AddInt32(&N
, -1) >= 0 {
124 for g
:= 0; g
< CallsPerSched
; g
++ {
136 for p
:= 0; p
< procs
; p
++ {
141 func BenchmarkSelectContended(b
*testing
.B
) {
142 const CallsPerSched
= 1000
143 procs
:= runtime
.GOMAXPROCS(-1)
144 N
:= int32(b
.N
/ CallsPerSched
)
145 c
:= make(chan bool, procs
)
146 myc1
:= make(chan int, procs
)
147 myc2
:= make(chan int, procs
)
148 for p
:= 0; p
< procs
; p
++ {
151 for atomic
.AddInt32(&N
, -1) >= 0 {
152 for g
:= 0; g
< CallsPerSched
; g
++ {
164 for p
:= 0; p
< procs
; p
++ {
169 func BenchmarkSelectNonblock(b
*testing
.B
) {
170 const CallsPerSched
= 1000
171 procs
:= runtime
.GOMAXPROCS(-1)
172 N
:= int32(b
.N
/ CallsPerSched
)
173 c
:= make(chan bool, procs
)
174 for p
:= 0; p
< procs
; p
++ {
176 myc1
:= make(chan int)
177 myc2
:= make(chan int)
178 myc3
:= make(chan int, 1)
179 myc4
:= make(chan int, 1)
180 for atomic
.AddInt32(&N
, -1) >= 0 {
181 for g
:= 0; g
< CallsPerSched
; g
++ {
203 for p
:= 0; p
< procs
; p
++ {
208 func BenchmarkChanUncontended(b
*testing
.B
) {
209 const CallsPerSched
= 1000
210 procs
:= runtime
.GOMAXPROCS(-1)
211 N
:= int32(b
.N
/ CallsPerSched
)
212 c
:= make(chan bool, procs
)
213 for p
:= 0; p
< procs
; p
++ {
215 myc
:= make(chan int, CallsPerSched
)
216 for atomic
.AddInt32(&N
, -1) >= 0 {
217 for g
:= 0; g
< CallsPerSched
; g
++ {
220 for g
:= 0; g
< CallsPerSched
; g
++ {
227 for p
:= 0; p
< procs
; p
++ {
232 func BenchmarkChanContended(b
*testing
.B
) {
233 const CallsPerSched
= 1000
234 procs
:= runtime
.GOMAXPROCS(-1)
235 N
:= int32(b
.N
/ CallsPerSched
)
236 c
:= make(chan bool, procs
)
237 myc
:= make(chan int, procs
*CallsPerSched
)
238 for p
:= 0; p
< procs
; p
++ {
240 for atomic
.AddInt32(&N
, -1) >= 0 {
241 for g
:= 0; g
< CallsPerSched
; g
++ {
244 for g
:= 0; g
< CallsPerSched
; g
++ {
251 for p
:= 0; p
< procs
; p
++ {
256 func BenchmarkChanSync(b
*testing
.B
) {
257 const CallsPerSched
= 1000
259 N
:= int32(b
.N
/ CallsPerSched
/ procs
* procs
)
260 c
:= make(chan bool, procs
)
261 myc
:= make(chan int)
262 for p
:= 0; p
< procs
; p
++ {
265 i
:= atomic
.AddInt32(&N
, -1)
269 for g
:= 0; g
< CallsPerSched
; g
++ {
282 for p
:= 0; p
< procs
; p
++ {
287 func benchmarkChanProdCons(b
*testing
.B
, chanSize
, localWork
int) {
288 const CallsPerSched
= 1000
289 procs
:= runtime
.GOMAXPROCS(-1)
290 N
:= int32(b
.N
/ CallsPerSched
)
291 c
:= make(chan bool, 2*procs
)
292 myc
:= make(chan int, chanSize
)
293 for p
:= 0; p
< procs
; p
++ {
296 for atomic
.AddInt32(&N
, -1) >= 0 {
297 for g
:= 0; g
< CallsPerSched
; g
++ {
298 for i
:= 0; i
< localWork
; i
++ {
315 for i
:= 0; i
< localWork
; i
++ {
323 for p
:= 0; p
< procs
; p
++ {
329 func BenchmarkChanProdCons0(b
*testing
.B
) {
330 benchmarkChanProdCons(b
, 0, 0)
333 func BenchmarkChanProdCons10(b
*testing
.B
) {
334 benchmarkChanProdCons(b
, 10, 0)
337 func BenchmarkChanProdCons100(b
*testing
.B
) {
338 benchmarkChanProdCons(b
, 100, 0)
341 func BenchmarkChanProdConsWork0(b
*testing
.B
) {
342 benchmarkChanProdCons(b
, 0, 100)
345 func BenchmarkChanProdConsWork10(b
*testing
.B
) {
346 benchmarkChanProdCons(b
, 10, 100)
349 func BenchmarkChanProdConsWork100(b
*testing
.B
) {
350 benchmarkChanProdCons(b
, 100, 100)
353 func BenchmarkChanCreation(b
*testing
.B
) {
354 const CallsPerSched
= 1000
355 procs
:= runtime
.GOMAXPROCS(-1)
356 N
:= int32(b
.N
/ CallsPerSched
)
357 c
:= make(chan bool, procs
)
358 for p
:= 0; p
< procs
; p
++ {
360 for atomic
.AddInt32(&N
, -1) >= 0 {
361 for g
:= 0; g
< CallsPerSched
; g
++ {
362 myc
:= make(chan int, 1)
370 for p
:= 0; p
< procs
; p
++ {