1
0

analytic_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package test
  2. import (
  3. "fmt"
  4. "github.com/shirou/gopsutil/v3/cpu"
  5. "github.com/shirou/gopsutil/v3/disk"
  6. "github.com/shirou/gopsutil/v3/load"
  7. "github.com/shirou/gopsutil/v3/mem"
  8. "runtime"
  9. "testing"
  10. "time"
  11. )
  12. func TestGoPsutil(t *testing.T) {
  13. fmt.Println("os:", runtime.GOOS)
  14. fmt.Println("threads:", runtime.GOMAXPROCS(0))
  15. v, _ := mem.VirtualMemory()
  16. loadAvg, _ := load.Avg()
  17. fmt.Println("loadavg", loadAvg.String())
  18. fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)
  19. cpuTimesBefore, _ := cpu.Times(false)
  20. time.Sleep(1000*time.Millisecond)
  21. cpuTimesAfter, _ := cpu.Times(false)
  22. threadNum := runtime.GOMAXPROCS(0)
  23. fmt.Println(cpuTimesBefore[0].String(), "\n", cpuTimesAfter[0].String())
  24. cpuUserUsage := (cpuTimesAfter[0].User - cpuTimesBefore[0].User) / (float64(1000*threadNum) / 1000)
  25. cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
  26. fmt.Printf("%.2f, %.2f\n", cpuUserUsage*100, cpuSystemUsage*100)
  27. diskUsage, _ := disk.Usage(".")
  28. fmt.Println(diskUsage.String())
  29. }