From 5f9d101bc654b460f80944f7112d2e6c4bc3610d Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 1 Feb 2022 01:34:25 +0800 Subject: [PATCH] feat: add runtime stats monitor (#1496) --- .gitignore | 3 ++- core/prof/runtime.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 core/prof/runtime.go diff --git a/.gitignore b/.gitignore index ea6551f2..4d34eb8d 100644 --- a/.gitignore +++ b/.gitignore @@ -16,7 +16,8 @@ **/logs # for test purpose -adhoc +**/adhoc +**/testdata # gitlab ci .cache diff --git a/core/prof/runtime.go b/core/prof/runtime.go new file mode 100644 index 00000000..085440ea --- /dev/null +++ b/core/prof/runtime.go @@ -0,0 +1,30 @@ +package prof + +import ( + "fmt" + "runtime" + "time" +) + +const ( + defaultInterval = time.Second * 5 + mega = 1024 * 1024 +) + +func DisplayStats(interval ...time.Duration) { + duration := defaultInterval + for _, val := range interval { + duration = val + } + + go func() { + ticker := time.NewTicker(duration) + defer ticker.Stop() + for range ticker.C { + var m runtime.MemStats + runtime.ReadMemStats(&m) + fmt.Printf("Goroutines: %d, Alloc: %vm, TotalAlloc: %vm, Sys: %vm, NumGC: %v\n", + runtime.NumGoroutine(), m.Alloc/mega, m.TotalAlloc/mega, m.Sys/mega, m.NumGC) + } + }() +}