-
Notifications
You must be signed in to change notification settings - Fork 13
/
prof.go
49 lines (44 loc) · 947 Bytes
/
prof.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
package config
import (
log "github.com/sirupsen/logrus"
"runtime"
"runtime/debug"
"time"
)
func PrintMemUsage() {
bToMb := func(b uint64) uint64 {
return b / 1024 / 1024
}
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
log.WithField("prof", true).Warnf("Alloc = %v MiB\tTotalAlloc = %v MiB\tSys = %v MiB\tGoroutines = %v\tNumGC = %v",
bToMb(m.Alloc),
bToMb(m.TotalAlloc),
bToMb(m.Sys),
runtime.NumGoroutine(),
m.NumGC)
}
func InitProfiling() {
go func() {
ticker := time.NewTicker(time.Minute * 1)
for {
PrintMemUsage()
<-ticker.C
}
}()
go func() {
ticker := time.NewTicker(time.Millisecond * 3000)
for {
runtime.GC()
<-ticker.C
}
}()
ticker := time.NewTicker(time.Second * 3)
for {
start := time.Now()
debug.FreeOSMemory()
log.WithField("prof", true).Debugf("scvg use %s", time.Now().Sub(start))
<-ticker.C
}
}