Created
December 26, 2023 08:48
-
-
Save gokhanm/ac62b9c716b69184d0ec5c69f7603c1e to your computer and use it in GitHub Desktop.
timing and benchmarking for goroutine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://github.com/egonelbre/exp/blob/main/bench/goroutine/main.go | |
| package main | |
| import ( | |
| "fmt" | |
| "runtime" | |
| "sync" | |
| "github.com/loov/hrtime" | |
| ) | |
| func main() { | |
| const P = 128 | |
| const K = 100 | |
| const N = 10000 | |
| { | |
| bench := hrtime.NewBenchmarkTSC(K) | |
| for bench.Next() { | |
| var wg sync.WaitGroup | |
| wg.Add(P) | |
| for k := 0; k < P; k++ { | |
| go func() { | |
| for i := 0; i < N; i++ { | |
| runtime.Gosched() | |
| } | |
| wg.Done() | |
| }() | |
| } | |
| wg.Wait() | |
| } | |
| hist := bench.Histogram(10) | |
| hist.Divide(N * P / runtime.NumCPU()) | |
| fmt.Println("gosched\n", hist.StringStats()) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment