github.com/rsc/tmp@v0.0.0-20240517235954-6deaab19748b/gomaxprocs/chain/chain_test.go (about)

     1  // Copyright 2015 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.
     4  
     5  package chain_test
     6  
     7  import "testing"
     8  
     9  const goroutines = 100
    10  
    11  var (
    12  	cin  chan<- int
    13  	cout <-chan int
    14  
    15  	cinbuf  chan<- int
    16  	coutbuf <-chan int
    17  )
    18  
    19  func init() {
    20  	c := make(chan int)
    21  	cin = c
    22  	for i := 0; i < goroutines; i++ {
    23  		next := make(chan int)
    24  		go copy1(c, next)
    25  		c = next
    26  	}
    27  	cout = c
    28  
    29  	c = make(chan int, 1)
    30  	cinbuf = c
    31  	for i := 0; i < goroutines; i++ {
    32  		next := make(chan int, 1)
    33  		go copy1(c, next)
    34  		c = next
    35  	}
    36  	coutbuf = c
    37  
    38  }
    39  
    40  func copy1(from, to chan int) {
    41  	for {
    42  		to <- <-from
    43  	}
    44  }
    45  
    46  func BenchmarkChain(b *testing.B) {
    47  	for i := 0; i < b.N; i++ {
    48  		cin <- 1
    49  		<-cout
    50  	}
    51  }
    52  
    53  func BenchmarkChainBuf(b *testing.B) {
    54  	for i := 0; i < b.N; i++ {
    55  		cinbuf <- 1
    56  		<-coutbuf
    57  	}
    58  }