github.com/rsc/tmp@v0.0.0-20240517235954-6deaab19748b/lockskew/rand_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 main
     6  
     7  import (
     8  	"math/rand"
     9  	"testing"
    10  )
    11  
    12  func benchmarkRand(b *testing.B, n int) {
    13  	c := make(chan int, n)
    14  	for i := 0; i < n; i++ {
    15  		go func() {
    16  			sum := 0
    17  			for j := 0; j < b.N/n; j++ {
    18  				sum += rand.Int()
    19  			}
    20  			c <- sum
    21  		}()
    22  	}
    23  	for i := 0; i < n; i++ {
    24  		<-c
    25  	}
    26  }
    27  
    28  func BenchmarkRand1(b *testing.B)   { benchmarkRand(b, 1) }
    29  func BenchmarkRand2(b *testing.B)   { benchmarkRand(b, 2) }
    30  func BenchmarkRand4(b *testing.B)   { benchmarkRand(b, 4) }
    31  func BenchmarkRand8(b *testing.B)   { benchmarkRand(b, 8) }
    32  func BenchmarkRand12(b *testing.B)  { benchmarkRand(b, 12) }
    33  func BenchmarkRand16(b *testing.B)  { benchmarkRand(b, 16) }
    34  func BenchmarkRand32(b *testing.B)  { benchmarkRand(b, 32) }
    35  func BenchmarkRand64(b *testing.B)  { benchmarkRand(b, 64) }
    36  func BenchmarkRand128(b *testing.B) { benchmarkRand(b, 128) }
    37  func BenchmarkRand256(b *testing.B) { benchmarkRand(b, 256) }