You've already forked go-profiler-notes
mirror of
https://github.com/DataDog/go-profiler-notes.git
synced 2025-07-12 23:50:13 +02:00
Add simple block bias example
This commit is contained in:
BIN
examples/block-bias/block.pb.gz
Normal file
BIN
examples/block-bias/block.pb.gz
Normal file
Binary file not shown.
5
examples/block-bias/go.mod
Normal file
5
examples/block-bias/go.mod
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module github.com/felixge/go-profiler-notes/examples/block-bias
|
||||||
|
|
||||||
|
go 1.15
|
||||||
|
|
||||||
|
require golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
|
2
examples/block-bias/go.sum
Normal file
2
examples/block-bias/go.sum
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
|
||||||
|
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
75
examples/block-bias/main.go
Normal file
75
examples/block-bias/main.go
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
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 = 1 * 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user