-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiler.go
58 lines (51 loc) · 906 Bytes
/
profiler.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
package main
import (
"os"
"runtime/pprof"
"runtime/trace"
"go.starlark.net/starlark"
)
type profiler struct {
cpuPath string
starPath string
tracePath string
cpu *os.File
star *os.File
trace *os.File
}
func (p *profiler) start() (err error) {
if p.cpuPath != "" {
if p.cpu, err = os.Create(p.cpuPath); err != nil {
return err
}
pprof.StartCPUProfile(p.cpu)
}
if p.starPath != "" {
if p.star, err = os.Create(p.starPath); err != nil {
return err
}
starlark.StartProfile(p.star)
}
if p.tracePath != "" {
if p.trace, err = os.Create(p.tracePath); err != nil {
return err
}
trace.Start(p.trace)
}
return nil
}
func (p *profiler) stop() error {
if p.cpuPath != "" {
pprof.StopCPUProfile()
p.cpu.Close()
}
if p.starPath != "" {
starlark.StopProfile()
p.star.Close()
}
if p.tracePath != "" {
trace.Stop()
p.trace.Close()
}
return nil
}