Skip to content

Instantly share code, notes, and snippets.

@gokhanm
Created December 26, 2023 08:48
Show Gist options
  • Save gokhanm/ac62b9c716b69184d0ec5c69f7603c1e to your computer and use it in GitHub Desktop.
Save gokhanm/ac62b9c716b69184d0ec5c69f7603c1e to your computer and use it in GitHub Desktop.
timing and benchmarking for goroutine
// 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