Last active
March 11, 2020 06:20
-
-
Save peacefixation/1ffe8911c16550d0dd3a13bfc274059f to your computer and use it in GitHub Desktop.
Profile a Go program
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
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