-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprofiling.go
More file actions
66 lines (57 loc) · 1.52 KB
/
profiling.go
File metadata and controls
66 lines (57 loc) · 1.52 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
57
58
59
60
61
62
63
64
65
66
package main
import (
"flag"
"fmt"
"os"
"runtime/pprof"
"github.com/pkg/errors"
)
// Holds the paths requested by the user for the profiles to be saved
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var memprofile = flag.String("memprofile", "", "write memory profile to this file")
// HandleProfiling is the introduction function for all profiling related functionality
func HandleProfiling() error {
fmt.Println("wtf")
flag.Parse()
if *cpuprofile != "" {
err := cpuProfile()
if err != nil {
return errors.Wrap(err, "failed to start CPU profiling")
}
//fmt.Println("Finished CPU profiling")
}
if *memprofile != "" {
err := ramProfile()
if err != nil {
return errors.Wrap(err, "failed to start memory profiling")
}
//fmt.Println("Finished memory profiling")
}
return nil
}
// Handles situation if cpu profiling is enabled
func cpuProfile() error {
f, err := os.Create(*cpuprofile)
if err != nil {
return errors.Wrap(err, "failed to create specified cpu profile file")
}
err = pprof.StartCPUProfile(f)
if err != nil {
return errors.Wrap(err, "failed to start cpu profiling")
}
return nil
}
var memFile *os.File
// Handles situation if memory profiling is enabled
func ramProfile() error {
var err error
memFile, err = os.Create(*memprofile)
if err != nil {
return errors.Wrap(err, "Failed to create specified memory profile file")
}
err = pprof.WriteHeapProfile(memFile)
if err != nil {
return errors.Wrap(err, "Failed to start memory profiling")
}
return nil
}