-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
76 lines (63 loc) · 1.69 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright © 2016 Yoshiki Shibata. All rights reserved.
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
)
var cpuProfileFile string
var memProfileFile string
var cpuprofile *bool
var memprofile *bool
func init() {
cwd := getCWD()
cpuProfileFile = cwd + PS + "oak.cpu.prof"
memProfileFile = cwd + PS + "oak.mem.prof"
cpuprofile = flag.Bool("cpuprofile", false, fmt.Sprintf("write cpu profie as %q", cpuProfileFile))
memprofile = flag.Bool("memprofile", false, fmt.Sprintf("write memory profile as %q", memProfileFile))
}
func startCPUProfile() *os.File {
if !*cpuprofile {
return nil
}
f, err := os.Create(cpuProfileFile)
if err != nil {
log.Fatal("could not create CPU profile:", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
f.Close()
log.Fatal("could not start CPU profile:", err)
}
return f
}
func stopCPUProfile(f *os.File) {
if f == nil {
return
}
pprof.StopCPUProfile()
f.Close()
fmt.Printf("CPU profile is stored as %q.\n", filepath.Base(cpuProfileFile))
fmt.Printf("To view the file, run the following command:\n")
fmt.Printf("\tgo tool pprof %s\n\n", filepath.Base(cpuProfileFile))
}
func saveMemProfile() {
if !*memprofile {
return
}
f, err := os.Create(memProfileFile)
if err != nil {
log.Fatal("could not create Mem profile:", err)
}
defer f.Close()
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile:", err)
}
fmt.Printf("Mem profile is stored as %q.\n", filepath.Base(memProfileFile))
fmt.Printf("To view the file, run the following command:\n")
fmt.Printf("\tgo tool pprof %s\n\n", filepath.Base(memProfileFile))
}