/
prof.go
54 lines (49 loc) · 991 Bytes
/
prof.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
package prof
import (
"flag"
"log"
"os"
"runtime"
"runtime/pprof"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var blockprofile = flag.String("blockprofile", "", "write block profile to file")
var memprofile = flag.String("memprofile", "", "write mem profile to file")
type Profile struct {
bpf *os.File
}
func StartProfile() *Profile {
p := &Profile{}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
}
if *blockprofile != "" {
var err error
p.bpf, err = os.Create(*blockprofile)
if err != nil {
log.Fatal(err)
}
runtime.SetBlockProfileRate(1)
}
return p
}
func (p *Profile) Stop() {
if *cpuprofile != "" {
pprof.StopCPUProfile()
}
if *blockprofile != "" {
pprof.Lookup("block").WriteTo(p.bpf, 0)
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
}