-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathprofile_enabled.go
More file actions
56 lines (48 loc) · 1.31 KB
/
Copy pathprofile_enabled.go
File metadata and controls
56 lines (48 loc) · 1.31 KB
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
//go:build profile
// +build profile
package main
import (
"flag"
"fmt"
"os"
"runtime/pprof"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var memprofile = flag.String("memprofile", "", "write memory profile to file")
func startProfiling() func() {
flag.Parse()
var cpuFile *os.File
if *cpuprofile != "" {
var err error
cpuFile, err = os.Create(*cpuprofile)
if err != nil {
fmt.Fprintf(os.Stderr, "could not create CPU profile: %v\n", err)
os.Exit(1)
}
if err := pprof.StartCPUProfile(cpuFile); err != nil {
fmt.Fprintf(os.Stderr, "could not start CPU profile: %v\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "[profile] CPU profiling enabled, writing to %s\n", *cpuprofile)
}
return func() {
if cpuFile != nil {
pprof.StopCPUProfile()
cpuFile.Close()
fmt.Fprintf(os.Stderr, "[profile] CPU profile written to %s\n", *cpuprofile)
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
fmt.Fprintf(os.Stderr, "could not create memory profile: %v\n", err)
return
}
defer f.Close()
if err := pprof.WriteHeapProfile(f); err != nil {
fmt.Fprintf(os.Stderr, "could not write memory profile: %v\n", err)
return
}
fmt.Fprintf(os.Stderr, "[profile] Memory profile written to %s\n", *memprofile)
}
}
}