-
Notifications
You must be signed in to change notification settings - Fork 96
/
profile.go
57 lines (51 loc) · 1.04 KB
/
profile.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
56
57
package main
import (
"log"
"os"
"runtime"
"runtime/pprof"
"runtime/trace"
)
func memprofile(mempath string) {
if mempath == "" {
return
}
f, err := os.Create(mempath)
xcheckf(err, "creating memory profile")
defer func() {
if err := f.Close(); err != nil {
log.Printf("closing memory profile: %v", err)
}
}()
runtime.GC() // get up-to-date statistics
err = pprof.WriteHeapProfile(f)
xcheckf(err, "writing memory profile")
}
func profile(cpupath, mempath string) func() {
if cpupath == "" {
return func() {
memprofile(mempath)
}
}
f, err := os.Create(cpupath)
xcheckf(err, "creating CPU profile")
err = pprof.StartCPUProfile(f)
xcheckf(err, "start CPU profile")
return func() {
pprof.StopCPUProfile()
if err := f.Close(); err != nil {
log.Printf("closing cpu profile: %v", err)
}
memprofile(mempath)
}
}
func traceExecution(path string) func() {
f, err := os.Create(path)
xcheckf(err, "create trace file")
trace.Start(f)
return func() {
trace.Stop()
err := f.Close()
xcheckf(err, "close trace file")
}
}