stats.go 796 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package main
  2. import (
  3. "runtime"
  4. "sync"
  5. "time"
  6. )
  7. var mutexStats sync.RWMutex
  8. var savedStats map[string]uint64
  9. func statsWorker() {
  10. c := time.Tick(1 * time.Second)
  11. for range c {
  12. var stats runtime.MemStats
  13. runtime.ReadMemStats(&stats)
  14. mutexStats.Lock()
  15. savedStats = map[string]uint64{
  16. "timestamp": uint64(time.Now().Unix()),
  17. "HeapInuse": stats.HeapInuse,
  18. "StackInuse": stats.StackInuse,
  19. "NuGoroutines": uint64(runtime.NumGoroutine()),
  20. "Mallocs": stats.Mallocs,
  21. "Frees": stats.Mallocs,
  22. "Inbound": uint64(messages.Get("inbound")),
  23. "Outbound": uint64(messages.Get("outbound")),
  24. }
  25. messages.Reset()
  26. mutexStats.Unlock()
  27. }
  28. }
  29. func Stats() map[string]uint64 {
  30. mutexStats.RLock()
  31. defer mutexStats.RUnlock()
  32. return savedStats
  33. }