mirror of
https://github.com/MedzikUser/go-utils.git
synced 2024-08-15 03:26:56 +00:00
feat(stats): add cpu and memory stats
This commit is contained in:
parent
eb4238e89c
commit
55d57c703f
5 changed files with 90 additions and 0 deletions
1
go.mod
1
go.mod
|
@ -7,5 +7,6 @@ require (
|
||||||
github.com/blang/semver/v4 v4.0.0
|
github.com/blang/semver/v4 v4.0.0
|
||||||
github.com/sirupsen/logrus v1.8.1
|
github.com/sirupsen/logrus v1.8.1
|
||||||
github.com/stretchr/testify v1.4.0 // indirect
|
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
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
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 h1:iiDhRitByXAEyjgBqsKi9QU4o2TNtv9kPP3RgPgXBPw=
|
||||||
github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE=
|
github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE=
|
||||||
github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
|
github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
|
||||||
|
|
40
stats/cpu.go
Normal file
40
stats/cpu.go
Normal file
|
@ -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
|
||||||
|
}
|
29
stats/memory.go
Normal file
29
stats/memory.go
Normal file
|
@ -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
|
||||||
|
}
|
18
utils/bytes.go
Normal file
18
utils/bytes.go
Normal file
|
@ -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])
|
||||||
|
}
|
Loading…
Reference in a new issue