gin/examples/realtime-advanced/stats.go

39 lines
777 B
Go
Raw Normal View History

2015-05-13 00:35:16 +00:00
package main
import (
"runtime"
2015-05-13 18:54:54 +00:00
"sync"
2015-05-13 00:35:16 +00:00
"time"
)
2015-05-13 18:54:54 +00:00
var mutexStats sync.RWMutex
var savedStats map[string]uint64
func statsWorker() {
c := time.Tick(1 * time.Second)
for range c {
var stats runtime.MemStats
runtime.ReadMemStats(&stats)
2015-05-13 00:35:16 +00:00
2015-05-13 18:54:54 +00:00
mutexStats.Lock()
savedStats = map[string]uint64{
"timestamp": uint64(time.Now().Unix()),
"HeapInuse": stats.HeapInuse,
"StackInuse": stats.StackInuse,
"NuGoroutines": uint64(runtime.NumGoroutine()),
"Mallocs": stats.Mallocs,
"Frees": stats.Mallocs,
"Inbound": uint64(messages.Get("inbound")),
"Outbound": uint64(messages.Get("outbound")),
}
mutexStats.Unlock()
2015-05-13 00:35:16 +00:00
}
}
2015-05-13 18:54:54 +00:00
func Stats() map[string]uint64 {
mutexStats.RLock()
defer mutexStats.RUnlock()
return savedStats
}