analytic_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package test
  2. import (
  3. "fmt"
  4. humanize "github.com/dustin/go-humanize"
  5. "github.com/mackerelio/go-osstat/cpu"
  6. "github.com/mackerelio/go-osstat/memory"
  7. "github.com/minio/minio/pkg/disk"
  8. "os"
  9. "runtime"
  10. "testing"
  11. "time"
  12. )
  13. func TestGetArch(t *testing.T) {
  14. fmt.Println("os:", runtime.GOOS)
  15. fmt.Println("threads:", runtime.GOMAXPROCS(0))
  16. memoryStat, err := memory.Get()
  17. if err != nil {
  18. fmt.Fprintf(os.Stderr, "%s\n", err)
  19. return
  20. }
  21. fmt.Println("memory total:", humanize.Bytes(memoryStat.Total))
  22. fmt.Println("memory used:", humanize.Bytes(memoryStat.Used))
  23. fmt.Println("memory cached:", humanize.Bytes(memoryStat.Cached))
  24. fmt.Println("memory free:", humanize.Bytes(memoryStat.Free))
  25. before, err := cpu.Get()
  26. if err != nil {
  27. fmt.Println(err)
  28. }
  29. time.Sleep(time.Duration(1) * time.Second)
  30. after, err := cpu.Get()
  31. if err != nil {
  32. fmt.Println(err)
  33. }
  34. total := float64(after.Total - before.Total)
  35. fmt.Printf("cpu user: %f %%\n", float64(after.User-before.User)/total*100)
  36. fmt.Printf("cpu system: %f %%\n", float64(after.System-before.System)/total*100)
  37. fmt.Printf("cpu idle: %f %%\n", float64(after.Idle-before.Idle)/total*100)
  38. err = diskUsage(".")
  39. if err != nil {
  40. fmt.Println(err)
  41. }
  42. }
  43. func diskUsage(path string) error {
  44. di, err := disk.GetInfo(path)
  45. if err != nil {
  46. return err
  47. }
  48. percentage := (float64(di.Total-di.Free) / float64(di.Total)) * 100
  49. fmt.Printf("%s of %s disk space used (%0.2f%%)\n",
  50. humanize.Bytes(di.Total-di.Free),
  51. humanize.Bytes(di.Total),
  52. percentage,
  53. )
  54. return nil
  55. }