From 38951777fcbb5242ca276b4453fdf8f0f7f8477b Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 11 Nov 2021 11:02:53 +0800 Subject: [PATCH] update --- README.md | 3 +++ go.mod | 3 +++ stat.go | 26 ++++++++++++++++++++++++++ stat_test.go | 18 ++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 stat.go create mode 100644 stat_test.go 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()) +}