-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
55 lines (50 loc) · 1.08 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package debug
import (
"log"
"os"
"runtime"
"runtime/pprof"
)
// go tool pprof ./cpu.pprof
func CpuPprof() func() {
f, err := os.Create("./cpu.pprof")
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
// StartCPUProfile为当前进程开启CPU profile。
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
// StopCPUProfile会停止当前的CPU profile(如果有)
return func() {
pprof.StopCPUProfile()
f.Close()
}
}
func MemPprof(fun func()) func() {
f, err := os.Create("./mem.pprof")
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
runtime.GC()
fun()
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
return func() {
f.Close()
}
}
func Pprof(opt string) func() {
f, err := os.Create("./mem.pprof")
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
runtime.GC()
if err := pprof.Lookup(opt).WriteTo(f, 1); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
return func() {
f.Close()
}
}