github.com/lingyao2333/mo-zero@v1.4.1/core/prof/runtime.go (about)

     1  package prof
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"time"
     7  )
     8  
     9  const (
    10  	defaultInterval = time.Second * 5
    11  	mega            = 1024 * 1024
    12  )
    13  
    14  // DisplayStats prints the goroutine, memory, GC stats with given interval, default to 5 seconds.
    15  func DisplayStats(interval ...time.Duration) {
    16  	duration := defaultInterval
    17  	for _, val := range interval {
    18  		duration = val
    19  	}
    20  
    21  	go func() {
    22  		ticker := time.NewTicker(duration)
    23  		defer ticker.Stop()
    24  		for range ticker.C {
    25  			var m runtime.MemStats
    26  			runtime.ReadMemStats(&m)
    27  			fmt.Printf("Goroutines: %d, Alloc: %vm, TotalAlloc: %vm, Sys: %vm, NumGC: %v\n",
    28  				runtime.NumGoroutine(), m.Alloc/mega, m.TotalAlloc/mega, m.Sys/mega, m.NumGC)
    29  		}
    30  	}()
    31  }