pprof is finished but not review

main
Your Name 2024-07-16 00:42:15 +08:00
parent ed2d2c6466
commit acf877b8d8
2 changed files with 15 additions and 8 deletions

BIN
pprof/cpu.pprof Normal file

Binary file not shown.

View File

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