-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdevelopment.go
More file actions
60 lines (50 loc) · 1.38 KB
/
development.go
File metadata and controls
60 lines (50 loc) · 1.38 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
package clio
import (
"fmt"
"strings"
"github.com/pkg/profile"
"github.com/anchore/fangs"
)
type Profile string
type DevelopmentConfig struct {
Profile Profile `yaml:"profile" json:"profile" mapstructure:"profile"`
}
func (d *DevelopmentConfig) DescribeFields(set fangs.FieldDescriptionSet) {
set.Add(&d.Profile, "capture resource profiling data (available: [cpu, mem, ...])")
}
func (d *DevelopmentConfig) PostLoad() error {
if d.Profile != "" {
p := parseProfile(d.Profile)
if p == nil {
return fmt.Errorf("invalid profile: %q", d.Profile)
}
}
return nil
}
func parseProfile(p Profile) func() func() {
profiler := profileFunc(p)
if profiler == nil {
return nil
}
return func() func() {
return profile.Start(profiler).Stop
}
}
func profileFunc(p Profile) func(*profile.Profile) {
return profilers()[strings.ToLower(strings.TrimSpace(string(p)))]
}
func profilers() map[string]func(*profile.Profile) {
return map[string]func(*profile.Profile){
"cpu": profile.CPUProfile,
"mem": profile.MemProfile,
"memory": profile.MemProfile,
"allocs": profile.MemProfileAllocs,
"heap": profile.MemProfileHeap,
"threads": profile.ThreadcreationProfile,
"mutex": profile.MutexProfile,
"block": profile.BlockProfile,
"clock": profile.ClockProfile,
"goroutine": profile.GoroutineProfile,
"trace": profile.TraceProfile,
}
}