This commit is contained in:
Jacky 2021-08-31 12:13:12 +08:00
parent ad421905a8
commit dd6e19657a
162 changed files with 15071 additions and 932 deletions

View file

@ -1,62 +1,36 @@
package test
import (
"fmt"
humanize "github.com/dustin/go-humanize"
"github.com/mackerelio/go-osstat/cpu"
"github.com/mackerelio/go-osstat/disk"
"github.com/mackerelio/go-osstat/memory"
"os"
"runtime"
"testing"
"time"
"fmt"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
"runtime"
"testing"
"time"
)
func TestGetArch(t *testing.T) {
fmt.Println("os:", runtime.GOOS)
fmt.Println("threads:", runtime.GOMAXPROCS(0))
func TestGoPsutil(t *testing.T) {
fmt.Println("os:", runtime.GOOS)
fmt.Println("threads:", runtime.GOMAXPROCS(0))
memoryStat, err := memory.Get()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return
}
fmt.Println("memory total:", humanize.Bytes(memoryStat.Total))
fmt.Println("memory used:", humanize.Bytes(memoryStat.Used))
fmt.Println("memory cached:", humanize.Bytes(memoryStat.Cached))
fmt.Println("memory free:", humanize.Bytes(memoryStat.Free))
v, _ := mem.VirtualMemory()
before, err := cpu.Get()
if err != nil {
fmt.Println(err)
}
time.Sleep(time.Duration(1) * time.Second)
after, err := cpu.Get()
if err != nil {
fmt.Println(err)
}
total := float64(after.Total - before.Total)
fmt.Printf("cpu user: %f %%\n", float64(after.User-before.User)/total*100)
fmt.Printf("cpu system: %f %%\n", float64(after.System-before.System)/total*100)
fmt.Printf("cpu idle: %f %%\n", float64(after.Idle-before.Idle)/total*100)
loadAvg, _ := load.Avg()
err = diskUsage(".")
fmt.Println("loadavg", loadAvg.String())
if err != nil {
fmt.Println(err)
}
}
func diskUsage(path string) error {
di, err := disk.GetInfo(path)
if err != nil {
return err
}
percentage := (float64(di.Total-di.Free) / float64(di.Total)) * 100
fmt.Printf("%s of %s disk space used (%0.2f%%)\n",
humanize.Bytes(di.Total-di.Free),
humanize.Bytes(di.Total),
percentage,
)
return nil
fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)
cpuTimesBefore, _ := cpu.Times(false)
time.Sleep(1000*time.Millisecond)
cpuTimesAfter, _ := cpu.Times(false)
threadNum := runtime.GOMAXPROCS(0)
fmt.Println(cpuTimesBefore[0].String(), "\n", cpuTimesAfter[0].String())
cpuUserUsage := (cpuTimesAfter[0].User - cpuTimesBefore[0].User) / (float64(1000*threadNum) / 1000)
cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
fmt.Printf("%.2f, %.2f\n", cpuUserUsage*100, cpuSystemUsage*100)
diskUsage, _ := disk.Usage(".")
fmt.Println(diskUsage.String())
}