github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/bench/goroutine/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"sync"
     7  
     8  	"github.com/loov/hrtime"
     9  )
    10  
    11  func main() {
    12  	const P = 128
    13  	const K = 100
    14  	const N = 10000
    15  
    16  	{
    17  		bench := hrtime.NewBenchmarkTSC(K)
    18  		for bench.Next() {
    19  			var wg sync.WaitGroup
    20  			wg.Add(P)
    21  			for k := 0; k < P; k++ {
    22  				go func() {
    23  					for i := 0; i < N; i++ {
    24  						runtime.Gosched()
    25  					}
    26  					wg.Done()
    27  				}()
    28  			}
    29  			wg.Wait()
    30  		}
    31  		hist := bench.Histogram(10)
    32  		hist.Divide(N * P / runtime.NumCPU())
    33  		fmt.Println("gosched\n", hist.StringStats())
    34  	}
    35  
    36  }