pprof is finished but not review
parent
ed2d2c6466
commit
acf877b8d8
Binary file not shown.
|
@ -17,39 +17,46 @@ func logicCode() {
|
|||
case v := <-c: //阻塞
|
||||
fmt.Printf("recv from chan, value:%v\n", v)
|
||||
default:
|
||||
//time.Sleep(time.Millisecond * 500) //优化
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
var isCPUPprof bool
|
||||
var isMemPprof bool
|
||||
var isCPUPprof bool //是否开启内存profile的标志位
|
||||
var isMemPprof bool //是否开启内存profile的标志位
|
||||
|
||||
flag.BoolVar(&isCPUPprof, "cpu", false, "turn cpu pprof on")
|
||||
flag.BoolVar(&isMemPprof, "mem", false, "turn mem pprof on")
|
||||
flag.Parse()
|
||||
|
||||
if isCPUPprof {
|
||||
file, err := os.Create("./cpu.pprof")
|
||||
f1, err := os.Create("./cpu.pprof") //在当前目录创建cpu.pprof文件
|
||||
if err != nil {
|
||||
fmt.Printf("create cpu pprof failed, err:%v\n", err)
|
||||
return
|
||||
}
|
||||
pprof.StartCPUProfile(file)
|
||||
defer pprof.StopCPUProfile()
|
||||
pprof.StartCPUProfile(f1) //往文件中记录cpu的信息
|
||||
defer func() {
|
||||
pprof.StopCPUProfile()
|
||||
f1.Close()
|
||||
}() //main函数退出之后结束
|
||||
}
|
||||
for i := 0; i < 8; i++ {
|
||||
go logicCode()
|
||||
}
|
||||
time.Sleep(20 * time.Second)
|
||||
if isMemPprof {
|
||||
file, err := os.Create("./mem.pprof")
|
||||
f2, err := os.Create("./mem.pprof")
|
||||
if err != nil {
|
||||
fmt.Printf("create mem pprof failed, err:%v\n", err)
|
||||
return
|
||||
}
|
||||
pprof.WriteHeapProfile(file)
|
||||
file.Close()
|
||||
pprof.WriteHeapProfile(f2)
|
||||
f2.Close()
|
||||
}
|
||||
}
|
||||
|
||||
//生成文件之后用 go tool pprof cpu.pprof
|
||||
//list logicCode 查看哪一行执行时间长
|
||||
|
|
Loading…
Reference in New Issue