Skip to content

Instantly share code, notes, and snippets.

@peacefixation
Last active March 11, 2020 06:20
Show Gist options
  • Save peacefixation/1ffe8911c16550d0dd3a13bfc274059f to your computer and use it in GitHub Desktop.
Save peacefixation/1ffe8911c16550d0dd3a13bfc274059f to your computer and use it in GitHub Desktop.
Profile a Go program
import (
"fmt"
"os"
"github.com/pkg/profile"
)
func main() {
p := profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook)
defer p.Stop()
// catch ^C and stop the profiler
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
// sig is a ^C, handle it
fmt.Println(sig.String())
p.Stop()
os.Exit(1)
}
}()
}
// # read the pprof file
// # show top 10 nodes
// go tool pprof cpu.pprof
// (pprof) top
// output ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment