cpu, trace, memory, block and goroutine
cw, err := os.Getwd()
if err != nil {
panic(err)
}
f, err := os.Create(path.Join(cw, "cpu.prof"))
if err != nil {
panic(err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
tf, err := os.Create(path.Join(cw, "trace.out"))
if err != nil {
panic(err)
}
defer tf.Close()
if err := trace.Start(tf); err != nil {
log.Fatalf("profile: could not start trace: %v", err)
}
defer trace.Stop()
old := runtime.MemProfileRate
runtime.MemProfileRate = 4096
defer func() {
cw, err := os.Getwd()
if err != nil {
panic(err)
}
f, err := os.Create(path.Join(cw, "mem.prof"))
if err != nil {
panic(err)
}
defer f.Close()
defer func() {
runtime.MemProfileRate = old
}()
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}()
runtime.SetBlockProfileRate(1)
defer func() {
cw, err := os.Getwd()
if err != nil {
panic(err)
}
f, err := os.Create(path.Join(cw, "block.prof"))
if err != nil {
panic(err)
}
defer f.Close()
runtime.SetBlockProfileRate(1)
defer func() {
pprof.Lookup("block").WriteTo(f, 0)
runtime.SetBlockProfileRate(0)
}()
}()
defer func() {
cw, err := os.Getwd()
if err != nil {
panic(err)
}
f, err := os.Create(path.Join(cw, "goroutine.prof"))
if err != nil {
panic(err)
}
defer f.Close()
runtime.SetBlockProfileRate(1)
defer func() {
if mp := pprof.Lookup("goroutine"); mp != nil {
mp.WriteTo(f, 0)
}
}()
}()