-
Notifications
You must be signed in to change notification settings - Fork 123
/
example_test.go
68 lines (57 loc) · 1.67 KB
/
example_test.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
package profile_test
import (
"flag"
"os"
"github.com/pkg/profile"
)
func ExampleStart() {
// start a simple CPU profile and register
// a defer to Stop (flush) the profiling data.
defer profile.Start().Stop()
}
func ExampleCPUProfile() {
// CPU profiling is the default profiling mode, but you can specify it
// explicitly for completeness.
defer profile.Start(profile.CPUProfile).Stop()
}
func ExampleMemProfile() {
// use memory profiling, rather than the default cpu profiling.
defer profile.Start(profile.MemProfile).Stop()
}
func ExampleMemProfileRate() {
// use memory profiling with custom rate.
defer profile.Start(profile.MemProfileRate(2048)).Stop()
}
func ExampleMemProfileHeap() {
// use heap memory profiling.
defer profile.Start(profile.MemProfileHeap).Stop()
}
func ExampleMemProfileAllocs() {
// use allocs memory profiling.
defer profile.Start(profile.MemProfileAllocs).Stop()
}
func ExampleProfilePath() {
// set the location that the profile will be written to
defer profile.Start(profile.ProfilePath(os.Getenv("HOME"))).Stop()
}
func ExampleNoShutdownHook() {
// disable the automatic shutdown hook.
defer profile.Start(profile.NoShutdownHook).Stop()
}
func ExampleStart_withFlags() {
// use the flags package to selectively enable profiling.
mode := flag.String("profile.mode", "", "enable profiling mode, one of [cpu, mem, mutex, block]")
flag.Parse()
switch *mode {
case "cpu":
defer profile.Start(profile.CPUProfile).Stop()
case "mem":
defer profile.Start(profile.MemProfile).Stop()
case "mutex":
defer profile.Start(profile.MutexProfile).Stop()
case "block":
defer profile.Start(profile.BlockProfile).Stop()
default:
// do nothing
}
}