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-14 16:16:00 +00:00
|
|
|
|
|
|
|
"github.com/manucorporat/stats"
|
2015-05-13 00:35:16 +00:00
|
|
|
)
|
|
|
|
|
2015-05-14 18:25:55 +00:00
|
|
|
var ips = stats.New()
|
2015-05-14 16:16:00 +00:00
|
|
|
var messages = stats.New()
|
|
|
|
var users = stats.New()
|
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)
|
2015-05-14 16:16:00 +00:00
|
|
|
var lastMallocs uint64 = 0
|
|
|
|
var lastFrees uint64 = 0
|
2015-05-13 18:54:54 +00:00
|
|
|
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{
|
2015-05-14 16:16:00 +00:00
|
|
|
"timestamp": uint64(time.Now().Unix()),
|
|
|
|
"HeapInuse": stats.HeapInuse,
|
|
|
|
"StackInuse": stats.StackInuse,
|
|
|
|
"Mallocs": (stats.Mallocs - lastMallocs),
|
|
|
|
"Frees": (stats.Frees - lastFrees),
|
|
|
|
"Inbound": uint64(messages.Get("inbound")),
|
|
|
|
"Outbound": uint64(messages.Get("outbound")),
|
|
|
|
"Connected": connectedUsers(),
|
2015-05-13 18:54:54 +00:00
|
|
|
}
|
2015-05-14 16:16:00 +00:00
|
|
|
lastMallocs = stats.Mallocs
|
|
|
|
lastFrees = stats.Frees
|
2015-05-14 15:01:02 +00:00
|
|
|
messages.Reset()
|
2015-05-13 18:54:54 +00:00
|
|
|
mutexStats.Unlock()
|
2015-05-13 00:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-13 18:54:54 +00:00
|
|
|
|
2015-05-14 16:16:00 +00:00
|
|
|
func connectedUsers() uint64 {
|
|
|
|
connected := users.Get("connected") - users.Get("disconnected")
|
|
|
|
if connected < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return uint64(connected)
|
|
|
|
}
|
|
|
|
|
2015-05-13 18:54:54 +00:00
|
|
|
func Stats() map[string]uint64 {
|
|
|
|
mutexStats.RLock()
|
|
|
|
defer mutexStats.RUnlock()
|
|
|
|
|
|
|
|
return savedStats
|
|
|
|
}
|