-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.go
138 lines (108 loc) · 2.48 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package pprofs
import (
"io"
"runtime"
"runtime/pprof"
"time"
)
// CpuProfile captures the CPU profile.
func CpuProfile() cpuProfile {
return cpuProfile{
duration: 15 * time.Second,
}
}
// WithDuration specifies the duration of the CPU profile.
func (p cpuProfile) WithDuration(d time.Duration) cpuProfile {
p.duration = d
return p
}
// HeapProfile captures the heap profile.
func HeapProfile() heapProfile {
return heapProfile{}
}
// MutexProfile captures the mutex profile.
func MutexProfile() mutexProfile {
return mutexProfile{}
}
// BlockProfile captures the block profile.
func BlockProfile() blockProfile {
return blockProfile{
rate: 1,
}
}
// WithRate specifies the rate of the block profile.
func (p blockProfile) WithRate(rate int) blockProfile {
p.rate = rate
return p
}
// GoroutineProfile captures the goroutine profile.
func GoroutineProfile() goroutineProfile {
return goroutineProfile{}
}
// ThreadcreateProfile captures the threadcreate profile.
func ThreadcreateProfile() threadcreateProfile {
return threadcreateProfile{}
}
type profile interface {
name() string
capture(io.Writer) error
}
type cpuProfile struct {
duration time.Duration
}
func (p cpuProfile) name() string {
return "cpu"
}
func (p cpuProfile) capture(w io.Writer) error {
dur := p.duration
if dur <= 0 {
dur = 15 * time.Second
}
if err := pprof.StartCPUProfile(w); err != nil {
return err
}
time.Sleep(dur)
pprof.StopCPUProfile()
return nil
}
type heapProfile struct{}
func (p heapProfile) name() string {
return "heap"
}
func (p heapProfile) capture(w io.Writer) error {
return captureProfile(w, p.name())
}
type mutexProfile struct{}
func (p mutexProfile) name() string {
return "mutex"
}
func (p mutexProfile) capture(w io.Writer) error {
return captureProfile(w, p.name())
}
type blockProfile struct {
rate int
}
func (p blockProfile) name() string {
return "block"
}
func (p blockProfile) capture(w io.Writer) error {
runtime.SetBlockProfileRate(p.rate)
return captureProfile(w, p.name())
}
type goroutineProfile struct{}
func (p goroutineProfile) name() string {
return "goroutine"
}
func (p goroutineProfile) capture(w io.Writer) error {
return captureProfile(w, p.name())
}
type threadcreateProfile struct{}
func (p threadcreateProfile) name() string {
return "threadcreate"
}
func (p threadcreateProfile) capture(w io.Writer) error {
return captureProfile(w, p.name())
}
func captureProfile(w io.Writer, name string) error {
return pprof.Lookup(name).WriteTo(w, 0)
}