From 55d57c703fdd85a5a8b33cefaf5e5291dcc7c614 Mon Sep 17 00:00:00 2001 From: MedzikUser <87065584+MedzikUser@users.noreply.github.com> Date: Sat, 14 Aug 2021 23:19:37 +0200 Subject: [PATCH] feat(stats): add cpu and memory stats --- go.mod | 1 + go.sum | 2 ++ stats/cpu.go | 40 ++++++++++++++++++++++++++++++++++++++++ stats/memory.go | 29 +++++++++++++++++++++++++++++ utils/bytes.go | 18 ++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 stats/cpu.go create mode 100644 stats/memory.go create mode 100644 utils/bytes.go diff --git a/go.mod b/go.mod index d32eae7..f8250c0 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,6 @@ require ( github.com/blang/semver/v4 v4.0.0 github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.4.0 // indirect + github.com/struCoder/pidusage v0.2.1 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect ) diff --git a/go.sum b/go.sum index cb7abd7..92fe873 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/struCoder/pidusage v0.2.1 h1:dFiEgUDkubeIj0XA1NpQ6+8LQmKrLi7NiIQl86E6BoY= +github.com/struCoder/pidusage v0.2.1/go.mod h1:bewtP2KUA1TBUyza5+/PCpSQ6sc/H6jJbIKAzqW86BA= github.com/tcnksm/go-gitconfig v0.1.2 h1:iiDhRitByXAEyjgBqsKi9QU4o2TNtv9kPP3RgPgXBPw= github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE= github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I= diff --git a/stats/cpu.go b/stats/cpu.go new file mode 100644 index 0000000..b8d796f --- /dev/null +++ b/stats/cpu.go @@ -0,0 +1,40 @@ +package stats + +import ( + "fmt" + "math" + "os" + "runtime" + + "github.com/struCoder/pidusage" +) + +type StatCPU struct { + // Percent of CPU Usage + Usage string + // Number of CPUs + Num int + // CPU Arch + Arch string + // Process ID + pid int +} + +// Get CPU stats +func CPU() (*StatCPU, error) { + pid := os.Getpid() + + sysInfo, err := pidusage.GetStat(pid) + if err != nil { + return nil, err + } + + stat := StatCPU{ + Usage: fmt.Sprint(math.Round(sysInfo.CPU*100)/100, "%"), + Num: runtime.NumCPU(), + Arch: runtime.GOARCH, + pid: pid, + } + + return &stat, nil +} diff --git a/stats/memory.go b/stats/memory.go new file mode 100644 index 0000000..6484814 --- /dev/null +++ b/stats/memory.go @@ -0,0 +1,29 @@ +package stats + +import ( + "runtime" + + "github.com/MedzikUser/go-utils/utils" +) + +type StatMemory struct { + Alloc string + TotalAlloc string + Sys string + NumGC uint32 +} + +// Get Memory stats +func Memory() StatMemory { + var m runtime.MemStats + runtime.ReadMemStats(&m) + + stat := StatMemory{ + Alloc: utils.Bytes(m.Alloc), + TotalAlloc: utils.Bytes(m.TotalAlloc), + Sys: utils.Bytes(m.Sys), + NumGC: m.NumGC, + } + + return stat +} diff --git a/utils/bytes.go b/utils/bytes.go new file mode 100644 index 0000000..c0ab171 --- /dev/null +++ b/utils/bytes.go @@ -0,0 +1,18 @@ +package utils + +import "fmt" + +func Bytes(b uint64) string { + const unit = 1000 + if b < unit { + return fmt.Sprintf("%d B", b) + } + + div, exp := int64(unit), 0 + for n := b / unit; n >= unit; n /= unit { + div *= unit + exp++ + } + + return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp]) +}