Skip to content

Instantly share code, notes, and snippets.

@felixge

felixge/main.go Secret

Created February 9, 2021 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixge/6f9701dd26707dbe7d351805860a4608 to your computer and use it in GitHub Desktop.
Save felixge/6f9701dd26707dbe7d351805860a4608 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"runtime"
"runtime/pprof"
"sync"
"time"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
var (
fastEventDuration = 1 * time.Millisecond
slowEventDuration = 10 * fastEventDuration
)
func run() error {
runtime.SetBlockProfileRate(int(slowEventDuration.Nanoseconds()))
var (
done = make(chan struct{})
wg = &sync.WaitGroup{}
)
wg.Add(1)
go func() {
defer wg.Done()
slowEvent(done)
}()
wg.Add(1)
go func() {
defer wg.Done()
fastEvent(done)
}()
time.Sleep(1 * time.Second)
close(done)
wg.Wait()
f, err := os.Create("block.pb.gz")
if err != nil {
return err
}
defer f.Close()
if err := pprof.Lookup("block").WriteTo(f, 0); err != nil {
return err
}
return nil
}
func slowEvent(done chan struct{}) error {
return simulateBlockEvents(slowEventDuration, done)
}
func fastEvent(done chan struct{}) error {
return simulateBlockEvents(fastEventDuration, done)
}
func simulateBlockEvents(duration time.Duration, done chan struct{}) error {
ticker := time.NewTicker(duration)
defer ticker.Stop()
for {
select {
case <-ticker.C:
// do nothing
case <-done:
return nil
}
}
}
$ go run .
$ go tool pprof -raw block.pb.gz
PeriodType: contentions count
Period: 1
Time: 2021-02-09 20:50:23.389267 +0100 CET
Samples:
contentions/count delay/nanoseconds
100 999180603: 1 2 3
93 93165410: 1 2 4
Locations
1: 0x10453af M=1 runtime.selectgo /usr/local/Cellar/go/1.15.7_1/libexec/src/runtime/select.go:511 s=0
2: 0x10ced3c M=1 main.simulateBlockEvents /Users/felix.geisendoerfer/go/src/github.com/felixge/go-profiler-notes/examples/block-bias/main.go:68 s=0
3: 0x10cee24 M=1 main.slowEvent /Users/felix.geisendoerfer/go/src/github.com/felixge/go-profiler-notes/examples/block-bias/main.go:57 s=0
main.run.func1 /Users/felix.geisendoerfer/go/src/github.com/felixge/go-profiler-notes/examples/block-bias/main.go:34 s=0
4: 0x10ceec4 M=1 main.fastEvent /Users/felix.geisendoerfer/go/src/github.com/felixge/go-profiler-notes/examples/block-bias/main.go:61 s=0
main.run.func2 /Users/felix.geisendoerfer/go/src/github.com/felixge/go-profiler-notes/examples/block-bias/main.go:39 s=0
Mappings
1: 0x0/0x0/0x0 [FN]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment