commit 38951777fcbb5242ca276b4453fdf8f0f7f8477b Author: Evan Chen Date: Thu Nov 11 11:02:53 2021 +0800 update diff --git a/README.md b/README.md new file mode 100644 index 0000000..a64d915 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# stat + +get simple stat for current go command and stat of a pid \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2554adb --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module kumoly.io/lib/stat + +go 1.17 diff --git a/stat.go b/stat.go new file mode 100644 index 0000000..b384591 --- /dev/null +++ b/stat.go @@ -0,0 +1,26 @@ +package stat + +import "runtime" + +type Profile struct { + Alloc uint64 + TotalAlloc uint64 + SysMem uint64 + NumGC uint32 +} + +func GetProfile() *Profile { + var m runtime.MemStats + runtime.ReadMemStats(&m) + + return &Profile{ + Alloc: bToMb(m.Alloc), + TotalAlloc: bToMb(m.TotalAlloc), + SysMem: bToMb(m.Sys), + NumGC: m.NumGC, + } +} + +func bToMb(b uint64) uint64 { + return b / 1024 / 1024 +} diff --git a/stat_test.go b/stat_test.go new file mode 100644 index 0000000..9dceed5 --- /dev/null +++ b/stat_test.go @@ -0,0 +1,18 @@ +package stat + +import ( + "fmt" + "testing" +) + +func TestStat(t *testing.T) { + fmt.Printf("%+v\n", GetProfile()) + buf := make([][]int, 1024) + for i := range buf { + buf[i] = make([]int, 1024) + for j := 0; j < len(buf[i]); j++ { + buf[i][j] = 20 + } + } + fmt.Printf("%+v\n", GetProfile()) +}