-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiling.go
88 lines (75 loc) · 2.48 KB
/
profiling.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
package main
import (
"fmt"
"runtime"
"time"
"github.com/pkg/profile"
)
// see https://flaviocopes.com/golang-profiling/
/*
INSTRUCTIONS:
. set go-modules
export GO111MODULE=on
. build
go build
. run
go run profiling.go
. stop
ctrl + c
. install graphviz (https://www.graphviz.org)
. generate the analysis using 'go tool pprof'
go tool pprof --{format} -source_path . *.pprof > {file_name}.{format}
e.g.
go tool pprof --pdf -source_path . mem.pprof > mem.pdf
go tool pprof --txt -source_path . mem.pprof > mem.txt
. open {file_name}.{format}
. analyse!
*/
func main() {
// CPU profiling by default
defer profile.Start(profile.ProfilePath(".")).Stop()
// WARN: you can run just ONE PROFILER at a time
// Available profilers
// defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop()
// defer profile.Start(profile.MemProfile, profile.ProfilePath(".")).Stop()
// defer profile.Start(profile.BlockProfile, profile.ProfilePath(".")).Stop()
// defer profile.Start(profile.GoroutineProfile, profile.ProfilePath(".")).Stop()
// defer profile.Start(profile.MutexProfile, profile.ProfilePath(".")).Stop()
// defer profile.Start(profile.TraceProfile, profile.ProfilePath(".")).Stop()
// defer profile.Start(profile.ThreadcreationProfile, profile.ProfilePath(".")).Stop()
doStuffs()
}
func doStuffs() {
PrintMemUsage()
var overall [][]int
for i := 0; i < 4; i++ {
// Allocate memory using make() and append to overall (so it doesn't get
// garbage collected). This is to create an ever increasing memory usage
// which we can track. We're just using []int as an example.
a := make([]int, 0, 999999)
overall = append(overall, a)
// Print our memory usage at each interval
PrintMemUsage()
time.Sleep(time.Second)
}
// Clear our memory and print usage, unless the GC has run 'Alloc' will remain the same
overall = nil
PrintMemUsage()
// Force GC to clear up, should see a memory drop
runtime.GC()
PrintMemUsage()
}
// PrintMemUsage outputs the current, total and OS memory being used. As well as the number
// of garage collection cycles completed.
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}