forked from keymetrics/pm2-io-apm-go
/
profling.go
45 lines (40 loc) · 862 Bytes
/
profling.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
package features
import (
"io/ioutil"
"runtime/pprof"
)
var currentPath string
// HeapDump return a binary string of a pprof
func HeapDump() ([]byte, error) {
f, err := ioutil.TempFile("/tmp", "heapdump")
if err != nil {
return nil, err
}
err = pprof.WriteHeapProfile(f)
if err != nil {
return nil, err
}
r, err := ioutil.ReadFile(f.Name())
if err != nil {
return nil, err
}
return r, nil
}
// StartCPUProfile start a CPU profiling and write it to file
func StartCPUProfile() error {
f, err := ioutil.TempFile("/tmp", "cpuprofile")
if err != nil {
return err
}
currentPath = f.Name()
return pprof.StartCPUProfile(f)
}
// StopCPUProfile stop the profiling and read the file
func StopCPUProfile() ([]byte, error) {
pprof.StopCPUProfile()
r, err := ioutil.ReadFile(currentPath)
if err != nil {
return nil, err
}
return r, nil
}